2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-01 14:57:55 -04:00
|
|
|
|
2024-01-07 17:20:02 -05:00
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
|
|
|
const {
|
|
|
|
isPromise,
|
|
|
|
} = core;
|
2024-01-26 17:46:46 -05:00
|
|
|
import { op_cron_create, op_cron_next } from "ext:core/ops";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
NumberPrototypeToString,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
2023-11-01 14:57:55 -04:00
|
|
|
|
2023-11-30 16:51:56 -05:00
|
|
|
export function formatToCronSchedule(
|
|
|
|
value?: number | { exact: number | number[] } | {
|
|
|
|
start?: number;
|
|
|
|
end?: number;
|
|
|
|
every?: number;
|
|
|
|
},
|
|
|
|
): string {
|
|
|
|
if (value === undefined) {
|
|
|
|
return "*";
|
|
|
|
} else if (typeof value === "number") {
|
2024-01-07 17:20:02 -05:00
|
|
|
return NumberPrototypeToString(value);
|
2023-11-30 16:51:56 -05:00
|
|
|
} else {
|
|
|
|
const { exact } = value as { exact: number | number[] };
|
|
|
|
if (exact === undefined) {
|
|
|
|
const { start, end, every } = value as {
|
|
|
|
start?: number;
|
|
|
|
end?: number;
|
|
|
|
every?: number;
|
|
|
|
};
|
|
|
|
if (start !== undefined && end !== undefined && every !== undefined) {
|
|
|
|
return start + "-" + end + "/" + every;
|
|
|
|
} else if (start !== undefined && end !== undefined) {
|
|
|
|
return start + "-" + end;
|
|
|
|
} else if (start !== undefined && every !== undefined) {
|
|
|
|
return start + "/" + every;
|
|
|
|
} else if (start !== undefined) {
|
|
|
|
return start + "/1";
|
|
|
|
} else if (end === undefined && every !== undefined) {
|
|
|
|
return "*/" + every;
|
|
|
|
} else {
|
|
|
|
throw new TypeError("Invalid cron schedule");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeof exact === "number") {
|
2024-01-07 17:20:02 -05:00
|
|
|
return NumberPrototypeToString(exact);
|
2023-11-30 16:51:56 -05:00
|
|
|
} else {
|
2024-01-07 17:20:02 -05:00
|
|
|
return ArrayPrototypeJoin(exact, ",");
|
2023-11-30 16:51:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseScheduleToString(
|
|
|
|
schedule: string | Deno.CronSchedule,
|
|
|
|
): string {
|
|
|
|
if (typeof schedule === "string") {
|
|
|
|
return schedule;
|
|
|
|
} else {
|
2024-01-23 19:45:11 -05:00
|
|
|
let {
|
2023-11-30 16:51:56 -05:00
|
|
|
minute,
|
|
|
|
hour,
|
|
|
|
dayOfMonth,
|
|
|
|
month,
|
|
|
|
dayOfWeek,
|
|
|
|
} = schedule;
|
|
|
|
|
2024-01-23 19:45:11 -05:00
|
|
|
// Automatically override unspecified values for convenience. For example,
|
|
|
|
// to run every 2 hours, `{ hour: { every: 2 } }` can be specified without
|
2024-04-20 19:54:07 -04:00
|
|
|
// explicitly specifying `minute`.
|
2024-01-23 19:45:11 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:51:56 -05:00
|
|
|
return formatToCronSchedule(minute) +
|
|
|
|
" " + formatToCronSchedule(hour) +
|
|
|
|
" " + formatToCronSchedule(dayOfMonth) +
|
|
|
|
" " + formatToCronSchedule(month) +
|
|
|
|
" " + formatToCronSchedule(dayOfWeek);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 14:57:55 -04:00
|
|
|
function cron(
|
|
|
|
name: string,
|
2023-11-30 16:51:56 -05:00
|
|
|
schedule: string | Deno.CronSchedule,
|
2023-11-16 17:19:00 -05:00
|
|
|
handlerOrOptions1:
|
|
|
|
| (() => Promise<void> | void)
|
|
|
|
| ({ backoffSchedule?: number[]; signal?: AbortSignal }),
|
2024-01-23 07:07:54 -05:00
|
|
|
handler2?: () => Promise<void> | void,
|
2023-11-01 14:57:55 -04:00
|
|
|
) {
|
|
|
|
if (name === undefined) {
|
|
|
|
throw new TypeError("Deno.cron requires a unique name");
|
|
|
|
}
|
|
|
|
if (schedule === undefined) {
|
|
|
|
throw new TypeError("Deno.cron requires a valid schedule");
|
|
|
|
}
|
2023-11-16 17:19:00 -05:00
|
|
|
|
2023-11-30 16:51:56 -05:00
|
|
|
schedule = parseScheduleToString(schedule);
|
|
|
|
|
2023-11-16 17:19:00 -05:00
|
|
|
let handler: () => Promise<void> | void;
|
2024-01-23 07:07:54 -05:00
|
|
|
let options:
|
|
|
|
| { backoffSchedule?: number[]; signal?: AbortSignal }
|
|
|
|
| undefined = undefined;
|
2023-11-16 17:19:00 -05:00
|
|
|
|
|
|
|
if (typeof handlerOrOptions1 === "function") {
|
|
|
|
handler = handlerOrOptions1;
|
2024-01-23 07:07:54 -05:00
|
|
|
if (handler2 !== undefined) {
|
|
|
|
throw new TypeError("Deno.cron requires a single handler");
|
2023-11-16 17:19:00 -05:00
|
|
|
}
|
2024-01-23 07:07:54 -05:00
|
|
|
} else if (typeof handler2 === "function") {
|
|
|
|
handler = handler2;
|
2023-11-16 17:19:00 -05:00
|
|
|
options = handlerOrOptions1;
|
|
|
|
} else {
|
2023-11-01 14:57:55 -04:00
|
|
|
throw new TypeError("Deno.cron requires a handler");
|
|
|
|
}
|
|
|
|
|
2024-01-07 17:20:02 -05:00
|
|
|
const rid = op_cron_create(
|
2023-11-01 14:57:55 -04:00
|
|
|
name,
|
|
|
|
schedule,
|
|
|
|
options?.backoffSchedule,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (options?.signal) {
|
|
|
|
const signal = options?.signal;
|
|
|
|
signal.addEventListener(
|
|
|
|
"abort",
|
|
|
|
() => {
|
|
|
|
core.close(rid);
|
|
|
|
},
|
|
|
|
{ once: true },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (async () => {
|
|
|
|
let success = true;
|
|
|
|
while (true) {
|
2023-12-26 20:30:26 -05:00
|
|
|
const r = await op_cron_next(rid, success);
|
2023-11-01 14:57:55 -04:00
|
|
|
if (r === false) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const result = handler();
|
2024-01-07 17:20:02 -05:00
|
|
|
const _res = isPromise(result) ? (await result) : result;
|
2023-11-01 14:57:55 -04:00
|
|
|
success = true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Exception in cron handler ${name}`, error);
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
2023-12-26 20:30:26 -05:00
|
|
|
// For testing
|
|
|
|
internals.formatToCronSchedule = formatToCronSchedule;
|
|
|
|
internals.parseScheduleToString = parseScheduleToString;
|
|
|
|
|
2023-11-01 14:57:55 -04:00
|
|
|
export { cron };
|