From 96f59653decff658bbd8c604767ed20e3c0a4c4f Mon Sep 17 00:00:00 2001 From: Tony Holdstock-Brown Date: Tue, 29 Oct 2024 06:45:00 -0700 Subject: [PATCH] Add timeouts to function configuration (#733) This adds to parameters: - `timeouts.start` - `timeouts.finish` If either of these timeouts are hit, the function run will be cancelled. - [x] Added a [docs PR](https://github.com/inngest/website) that references this PR - [x] Added unit/integration tests - [x] Added changesets if applicable --------- Co-authored-by: Jack Williams <1736957+jpwilliams@users.noreply.github.com> --- .changeset/proud-pets-stare.md | 5 +++ .../inngest/src/components/InngestFunction.ts | 35 +++++++++++++++++++ packages/inngest/src/types.ts | 12 +++++++ 3 files changed, 52 insertions(+) create mode 100644 .changeset/proud-pets-stare.md diff --git a/.changeset/proud-pets-stare.md b/.changeset/proud-pets-stare.md new file mode 100644 index 000000000..185e70400 --- /dev/null +++ b/.changeset/proud-pets-stare.md @@ -0,0 +1,5 @@ +--- +"inngest": minor +--- + +Add timeouts as function config diff --git a/packages/inngest/src/components/InngestFunction.ts b/packages/inngest/src/components/InngestFunction.ts index 9f31a5a7a..6744f4cee 100644 --- a/packages/inngest/src/components/InngestFunction.ts +++ b/packages/inngest/src/components/InngestFunction.ts @@ -123,6 +123,7 @@ export class InngestFunction< throttle, concurrency, debounce, + timeouts, priority, } = this.opts; @@ -165,6 +166,7 @@ export class InngestFunction< concurrency, debounce, priority, + timeouts, }; if (cancelOn) { @@ -480,6 +482,39 @@ export namespace InngestFunction { run?: string; }; + /** + * Configure timeouts for the function. If any of the timeouts are hit, the + * function run will be cancelled. + */ + timeouts?: { + /** + * Start represents the timeout for starting a function. If the time + * between scheduling and starting a function exceeds this value, the + * function will be cancelled. + * + * This is, essentially, the amount of time that a function sits in the + * queue before starting. + * + * A function may exceed this duration because of concurrency limits, + * throttling, etc. + */ + start?: TimeStr; + + /** + * Finish represents the time between a function starting and the function + * finishing. If a function takes longer than this time to finish, the + * function is marked as cancelled. + * + * The start time is taken from the time that the first successful + * function request begins, and does not include the time spent in the + * queue before the function starts. + * + * Note that if the final request to a function begins before this + * timeout, and completes after this timeout, the function will succeed. + */ + finish?: TimeStr; + }; + cancelOn?: Cancellation>[]; /** diff --git a/packages/inngest/src/types.ts b/packages/inngest/src/types.ts index c15647802..719e95d33 100644 --- a/packages/inngest/src/types.ts +++ b/packages/inngest/src/types.ts @@ -1169,6 +1169,18 @@ export const functionConfigSchema = z.strictObject({ .optional(), }) .optional(), + timeouts: z + .strictObject({ + start: z + .string() + .transform((x) => x as TimeStr) + .optional(), + finish: z + .string() + .transform((x) => x as TimeStr) + .optional(), + }) + .optional(), priority: z .strictObject({ run: z.string().optional(),