Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING(unstable): remove Deno.cron() overload #22035

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cli/tests/unit/cron_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ Deno.test(async function retriesWithBackoffScheduleOldApi() {

let count = 0;
const ac = new AbortController();
const c = Deno.cron("abc2", "*/20 * * * *", async () => {
const c = Deno.cron("abc2", "*/20 * * * *", {
signal: ac.signal,
backoffSchedule: [10, 20],
}, async () => {
count += 1;
await sleep(10);
throw new TypeError("cron error");
}, {
signal: ac.signal,
backoffSchedule: [10, 20],
});

try {
Expand Down Expand Up @@ -380,3 +380,14 @@ Deno.test("Parse schedule to string - string", () => {
const result = parseScheduleToString("* * * * *");
assertEquals(result, "* * * * *");
});

Deno.test("error on two handlers", () => {
assertThrows(
() => {
// @ts-ignore test
Deno.cron("abc", "* * * * *", () => {}, () => {});
},
TypeError,
"Deno.cron requires a single handler",
);
});
31 changes: 0 additions & 31 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,37 +1320,6 @@ declare namespace Deno {
handler: () => Promise<void> | void,
): Promise<void>;

/** **UNSTABLE**: New API, yet to be vetted.
*
* Create a cron job that will periodically execute the provided handler
* callback based on the specified schedule.
*
* `schedule` can be a string in the Unix cron format or in JSON format
* as specified by interface {@linkcode CronSchedule}, where time is specified
* using UTC time zone.
*
* ```ts
* Deno.cron("sample cron", "20 * * * *", () => {
* console.log("cron job executed");
* });
* ```
* `backoffSchedule` option can be used to specify the retry policy for failed
* executions. Each element in the array represents the number of milliseconds
* to wait before retrying the execution. For example, `[1000, 5000, 10000]`
* means that a failed execution will be retried at most 3 times, with 1
* second, 5 seconds, and 10 seconds delay between each retry.
*
* @category Cron
* @deprecated Use other {@linkcode cron} overloads instead. This overload
* will be removed in the future.
*/
export function cron(
name: string,
schedule: string | CronSchedule,
handler: () => Promise<void> | void,
options: { backoffSchedule?: number[]; signal?: AbortSignal },
): Promise<void>;

/** **UNSTABLE**: New API, yet to be vetted.
*
* A key to be persisted in a {@linkcode Deno.Kv}. A key is a sequence
Expand Down
17 changes: 8 additions & 9 deletions ext/cron/01_cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ function cron(
handlerOrOptions1:
| (() => Promise<void> | void)
| ({ backoffSchedule?: number[]; signal?: AbortSignal }),
handlerOrOptions2?:
| (() => Promise<void> | void)
| ({ backoffSchedule?: number[]; signal?: AbortSignal }),
handler2?: () => Promise<void> | void,
) {
if (name === undefined) {
throw new TypeError("Deno.cron requires a unique name");
Expand All @@ -98,16 +96,17 @@ function cron(
schedule = parseScheduleToString(schedule);

let handler: () => Promise<void> | void;
let options: { backoffSchedule?: number[]; signal?: AbortSignal } | undefined;
let options:
| { backoffSchedule?: number[]; signal?: AbortSignal }
| undefined = undefined;

if (typeof handlerOrOptions1 === "function") {
handler = handlerOrOptions1;
if (typeof handlerOrOptions2 === "function") {
throw new TypeError("options must be an object");
if (handler2 !== undefined) {
throw new TypeError("Deno.cron requires a single handler");
}
options = handlerOrOptions2;
} else if (typeof handlerOrOptions2 === "function") {
handler = handlerOrOptions2;
} else if (typeof handler2 === "function") {
handler = handler2;
options = handlerOrOptions1;
} else {
throw new TypeError("Deno.cron requires a handler");
Expand Down