|
1 | 1 | /** |
2 | 2 | * This is the "standard library" of steps that we make available to all workflow users. |
3 | | - * The can be imported like so: `import { sleep, fetch } from 'workflow'`. and used in workflow. |
| 3 | + * The can be imported like so: `import { fetch } from 'workflow'`. and used in workflow. |
4 | 4 | * The need to be exported directly in this package and cannot live in `core` to prevent |
5 | 5 | * circular dependencies post-compilation. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { RetryableError } from '@workflow/errors'; |
9 | | -import ms, { type StringValue } from 'ms'; |
10 | | -import { getStepMetadata } from './index.js'; |
11 | | - |
12 | | -// vqs has a max message visibility lifespan, the workflow sleep function |
13 | | -// will retry repeatedly until the user requested duration is reached. |
14 | | -// (Eventually make this configurable based on the queue backend adapter) |
15 | | -const MAX_SLEEP_DURATION_SECONDS = ms('23h') / 1000; |
16 | | - |
17 | | -/** |
18 | | - * Sleep within a workflow for a given duration. |
19 | | - * |
20 | | - * @param duration - The duration to sleep for, this is a string in the format |
21 | | - * of `"1000ms"`, `"1s"`, `"1m"`, `"1h"`, or `"1d"`. |
22 | | - * @overload |
23 | | - * @returns A promise that resolves when the sleep is complete. |
24 | | - */ |
25 | | -export async function sleep(duration: StringValue): Promise<void>; |
26 | | - |
27 | | -/** |
28 | | - * Sleep within a workflow until a specific date. |
29 | | - * |
30 | | - * @param date - The date to sleep until, this must be a future date. |
31 | | - * @overload |
32 | | - * @returns A promise that resolves when the sleep is complete. |
33 | | - */ |
34 | | -export async function sleep(date: Date): Promise<void>; |
35 | | - |
36 | | -export async function sleep(param: StringValue | Date): Promise<void> { |
37 | | - 'use step'; |
38 | | - const { stepStartedAt } = getStepMetadata(); |
39 | | - const durationMs = |
40 | | - typeof param === 'string' |
41 | | - ? ms(param) |
42 | | - : param.getTime() - Number(stepStartedAt); |
43 | | - |
44 | | - if (typeof durationMs !== 'number' || durationMs < 0) { |
45 | | - const message = |
46 | | - param instanceof Date |
47 | | - ? `Invalid sleep date: "${param}". Expected a future date.` |
48 | | - : `Invalid sleep duration: "${param}". Expected a valid duration string like "1s", "1m", "1h", etc.`; |
49 | | - throw new Error(message); |
50 | | - } |
51 | | - |
52 | | - const endAt = +stepStartedAt + durationMs; |
53 | | - const now = Date.now(); |
54 | | - if (now < endAt) { |
55 | | - const remainingSeconds = (endAt - now) / 1000; |
56 | | - const retryAfter = Math.min(remainingSeconds, MAX_SLEEP_DURATION_SECONDS); |
57 | | - throw new RetryableError( |
58 | | - `Sleeping for ${ms(retryAfter * 1000, { long: true })}`, |
59 | | - { |
60 | | - retryAfter, |
61 | | - } |
62 | | - ); |
63 | | - } |
64 | | -} |
65 | | -sleep.maxRetries = Infinity; |
66 | | - |
67 | 8 | /** |
68 | 9 | * A hoisted `fetch()` function that is executed as a "step" function, |
69 | 10 | * for use within workflow functions. |
|
0 commit comments