2023-11-01 14:57:55 -04:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// @ts-ignore internal api
|
|
|
|
const core = Deno.core;
|
|
|
|
|
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") {
|
|
|
|
return value.toString();
|
|
|
|
} 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") {
|
|
|
|
return exact.toString();
|
|
|
|
} else {
|
|
|
|
return exact.join(",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseScheduleToString(
|
|
|
|
schedule: string | Deno.CronSchedule,
|
|
|
|
): string {
|
|
|
|
if (typeof schedule === "string") {
|
|
|
|
return schedule;
|
|
|
|
} else {
|
|
|
|
const {
|
|
|
|
minute,
|
|
|
|
hour,
|
|
|
|
dayOfMonth,
|
|
|
|
month,
|
|
|
|
dayOfWeek,
|
|
|
|
} = schedule;
|
|
|
|
|
|
|
|
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 }),
|
|
|
|
handlerOrOptions2?:
|
|
|
|
| (() => Promise<void> | void)
|
|
|
|
| ({ backoffSchedule?: number[]; signal?: AbortSignal }),
|
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;
|
|
|
|
let options: { backoffSchedule?: number[]; signal?: AbortSignal } | undefined;
|
|
|
|
|
|
|
|
if (typeof handlerOrOptions1 === "function") {
|
|
|
|
handler = handlerOrOptions1;
|
|
|
|
if (typeof handlerOrOptions2 === "function") {
|
|
|
|
throw new TypeError("options must be an object");
|
|
|
|
}
|
|
|
|
options = handlerOrOptions2;
|
|
|
|
} else if (typeof handlerOrOptions2 === "function") {
|
|
|
|
handler = handlerOrOptions2;
|
|
|
|
options = handlerOrOptions1;
|
|
|
|
} else {
|
2023-11-01 14:57:55 -04:00
|
|
|
throw new TypeError("Deno.cron requires a handler");
|
|
|
|
}
|
|
|
|
|
|
|
|
const rid = core.ops.op_cron_create(
|
|
|
|
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) {
|
|
|
|
const r = await core.opAsync("op_cron_next", rid, success);
|
|
|
|
if (r === false) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const result = handler();
|
|
|
|
const _res = result instanceof Promise ? (await result) : result;
|
|
|
|
success = true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Exception in cron handler ${name}`, error);
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
export { cron };
|