summaryrefslogtreecommitdiff
path: root/ext/cron/01_cron.ts
diff options
context:
space:
mode:
authorIgor Zinkovsky <igor@deno.com>2024-01-23 16:45:11 -0800
committerGitHub <noreply@github.com>2024-01-24 00:45:11 +0000
commit46b91eceb15c3941bf1f8f62b98026ad3fde3e9f (patch)
tree8458bb0a709925c73bb9c7aa212a7cb831e774ef /ext/cron/01_cron.ts
parent47620641e7455a0f9df82d17ad0405693e4427a4 (diff)
fix(ext/cron): automatically override unspecified values (#22042)
Fixes #22041
Diffstat (limited to 'ext/cron/01_cron.ts')
-rw-r--r--ext/cron/01_cron.ts21
1 files changed, 20 insertions, 1 deletions
diff --git a/ext/cron/01_cron.ts b/ext/cron/01_cron.ts
index 84e5c7fff..c2a0c9cd1 100644
--- a/ext/cron/01_cron.ts
+++ b/ext/cron/01_cron.ts
@@ -62,7 +62,7 @@ export function parseScheduleToString(
if (typeof schedule === "string") {
return schedule;
} else {
- const {
+ let {
minute,
hour,
dayOfMonth,
@@ -70,6 +70,25 @@ export function parseScheduleToString(
dayOfWeek,
} = schedule;
+ // Automatically override unspecified values for convenience. For example,
+ // to run every 2 hours, `{ hour: { every: 2 } }` can be specified without
+ // explicitely specifying `minute`.
+ if (minute !== undefined) {
+ // Nothing to override.
+ } else if (hour !== undefined) {
+ // Override minute to 0 since it's not specified.
+ minute = 0;
+ } else if (dayOfMonth !== undefined || dayOfWeek !== undefined) {
+ // Override minute and hour to 0 since they're not specified.
+ minute = 0;
+ hour = 0;
+ } else if (month !== undefined) {
+ // Override minute and hour to 0, and dayOfMonth to 1 since they're not specified.
+ minute = 0;
+ hour = 0;
+ dayOfMonth = 1;
+ }
+
return formatToCronSchedule(minute) +
" " + formatToCronSchedule(hour) +
" " + formatToCronSchedule(dayOfMonth) +