From b572abfcb3fadbfdd3ce671a27463d67bcb77534 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Thu, 16 Nov 2023 14:19:00 -0800 Subject: feat(ext/cron) modify Deno.cron API to make handler arg last (#21225) This PR changes the `Deno.cron` API: * Marks the existing function as deprecated * Introduces 2 new overloads, where the handler arg is always last: ```ts Deno.cron( name: string, schedule: string, handler: () => Promise | void, ) Deno.cron( name: string, schedule: string, options?: { backoffSchedule?: number[]; signal?: AbortSignal }, handler: () => Promise | void, ) ``` This PR also fixes a bug, when other crons continue execution after one of the crons was closed using `signal`. --- cli/tsc/dts/lib.deno.unstable.d.ts | 57 +++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'cli/tsc') diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts index 3ca8a212a..5cba27a5e 100644 --- a/cli/tsc/dts/lib.deno.unstable.d.ts +++ b/cli/tsc/dts/lib.deno.unstable.d.ts @@ -1334,12 +1334,67 @@ declare namespace Deno { * 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, handler: () => Promise | void, - options?: { backoffSchedule?: number[]; signal?: AbortSignal }, + options: { backoffSchedule?: number[]; signal?: AbortSignal }, + ): Promise; + + /** **UNSTABLE**: New API, yet to be vetted. + * + * Create a cron job that will periodically execute the provided handler + * callback based on the specified schedule. + * + * ```ts + * Deno.cron("sample cron", "20 * * * *", () => { + * console.log("cron job executed"); + * }); + * ``` + * + * `schedule` is a Unix cron format expression, where time is specified + * using UTC time zone. + * + * @category Cron + */ + export function cron( + name: string, + schedule: string, + handler: () => Promise | void, + ): Promise; + + /** **UNSTABLE**: New API, yet to be vetted. + * + * Create a cron job that will periodically execute the provided handler + * callback based on the specified schedule. + * + * ```ts + * Deno.cron("sample cron", "20 * * * *", { + * backoffSchedule: [10, 20] + * }, () => { + * console.log("cron job executed"); + * }); + * ``` + * + * `schedule` is a Unix cron format expression, where time is specified + * using UTC time zone. + * + * `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 + */ + export function cron( + name: string, + schedule: string, + options: { backoffSchedule?: number[]; signal?: AbortSignal }, + handler: () => Promise | void, ): Promise; /** **UNSTABLE**: New API, yet to be vetted. -- cgit v1.2.3