1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/ext/cron/01_cron.ts
Asher Gomez d6d437c4dc
BREAKING(unstable): remove Deno.cron() overload (#22035)
This change removes the currently deprecated `Deno.cron()` overload with
`options` as a potential last argument.

This might be fine to do now, in a major release, as `Deno.cron()` is an
unstable API. I thought of doing this while working on #22021. If this
is not ready to remove, I can instead set the removal version of this
overload for Deno v2.

Note: this overload was deprecated in Deno v1.38.2 (#21225). So it's
been deprecated for over 2 months.
2024-01-23 17:37:54 +05:30

155 lines
3.8 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { core, internals, primordials } from "ext:core/mod.js";
const {
isPromise,
} = core;
const {
op_cron_create,
op_cron_next,
} = core.ensureFastOps();
const {
ArrayPrototypeJoin,
NumberPrototypeToString,
TypeError,
} = primordials;
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 NumberPrototypeToString(value);
} 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 NumberPrototypeToString(exact);
} else {
return ArrayPrototypeJoin(exact, ",");
}
}
}
}
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);
}
}
function cron(
name: string,
schedule: string | Deno.CronSchedule,
handlerOrOptions1:
| (() => Promise<void> | void)
| ({ backoffSchedule?: number[]; signal?: AbortSignal }),
handler2?: () => Promise<void> | void,
) {
if (name === undefined) {
throw new TypeError("Deno.cron requires a unique name");
}
if (schedule === undefined) {
throw new TypeError("Deno.cron requires a valid schedule");
}
schedule = parseScheduleToString(schedule);
let handler: () => Promise<void> | void;
let options:
| { backoffSchedule?: number[]; signal?: AbortSignal }
| undefined = undefined;
if (typeof handlerOrOptions1 === "function") {
handler = handlerOrOptions1;
if (handler2 !== undefined) {
throw new TypeError("Deno.cron requires a single handler");
}
} else if (typeof handler2 === "function") {
handler = handler2;
options = handlerOrOptions1;
} else {
throw new TypeError("Deno.cron requires a handler");
}
const rid = 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 op_cron_next(rid, success);
if (r === false) {
break;
}
try {
const result = handler();
const _res = isPromise(result) ? (await result) : result;
success = true;
} catch (error) {
console.error(`Exception in cron handler ${name}`, error);
success = false;
}
}
})();
}
// For testing
internals.formatToCronSchedule = formatToCronSchedule;
internals.parseScheduleToString = parseScheduleToString;
export { cron };