workflow-cron-sleep 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +137 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dave Daly
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# workflow-cron-sleep
|
|
2
|
+
|
|
3
|
+
Sleep until the next cron expression occurrence in [Vercel Workflows](https://vercel.com/docs/workflow).
|
|
4
|
+
|
|
5
|
+
## What This Does
|
|
6
|
+
|
|
7
|
+
`cronSleep` pauses your workflow until the **next single occurrence** of a cron expression. It is **not** a recurring cron job scheduler.
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// This sleeps ONCE until the next 9 AM, then continues execution
|
|
11
|
+
await cronSleep("0 9 * * *", { timezone: "America/New_York" })
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
If you need recurring behavior, you'll need to structure your workflows to handle that (see [Recurring Workflows](#recurring-workflows) below).
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install workflow-cron-sleep
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { cronSleep } from "workflow-cron-sleep"
|
|
26
|
+
|
|
27
|
+
async function myWorkflow() {
|
|
28
|
+
"use workflow"
|
|
29
|
+
|
|
30
|
+
// Sleep until the next 9 AM Eastern Time
|
|
31
|
+
await cronSleep("0 9 * * *", { timezone: "America/New_York" })
|
|
32
|
+
|
|
33
|
+
// This runs once at 9 AM, then the workflow ends
|
|
34
|
+
console.log("It's 9 AM!")
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Timezone Behavior
|
|
39
|
+
|
|
40
|
+
**Important:** When no timezone is specified, `cronSleep` uses the **local system timezone** of the server running your workflow. On Vercel, this depends on the region your function is deployed to.
|
|
41
|
+
|
|
42
|
+
For predictable behavior, **always specify a timezone**:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
// Recommended: Explicit timezone
|
|
46
|
+
await cronSleep("0 9 * * *", { timezone: "America/New_York" })
|
|
47
|
+
|
|
48
|
+
// UTC for timezone-agnostic scheduling
|
|
49
|
+
await cronSleep("0 14 * * *", { timezone: "UTC" })
|
|
50
|
+
|
|
51
|
+
// Not recommended: Server's local timezone (varies by region)
|
|
52
|
+
await cronSleep("0 9 * * *")
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
### `cronSleep(cronExpression, options?)`
|
|
58
|
+
|
|
59
|
+
Sleeps until the **next occurrence** of a cron expression.
|
|
60
|
+
|
|
61
|
+
#### Parameters
|
|
62
|
+
|
|
63
|
+
| Parameter | Type | Description |
|
|
64
|
+
|-----------|------|-------------|
|
|
65
|
+
| `cronExpression` | `string` | A valid cron expression (e.g., `"0 9 * * *"`) |
|
|
66
|
+
| `options.timezone` | `string` (optional) | IANA timezone (e.g., `"America/New_York"`, `"UTC"`) |
|
|
67
|
+
|
|
68
|
+
#### Returns
|
|
69
|
+
|
|
70
|
+
`Promise<void>` - Resolves when the workflow resumes at the scheduled time.
|
|
71
|
+
|
|
72
|
+
#### Throws
|
|
73
|
+
|
|
74
|
+
Throws an error if the cron expression is invalid or has no future occurrences.
|
|
75
|
+
|
|
76
|
+
## Cron Expression Reference
|
|
77
|
+
|
|
78
|
+
| Expression | Description |
|
|
79
|
+
|------------|-------------|
|
|
80
|
+
| `0 9 * * *` | Every day at 9:00 AM |
|
|
81
|
+
| `30 8 * * 1` | Every Monday at 8:30 AM |
|
|
82
|
+
| `0 0 1 * *` | First day of every month at midnight |
|
|
83
|
+
| `0 */2 * * *` | Every 2 hours |
|
|
84
|
+
| `0 9 * * 1-5` | Weekdays at 9:00 AM |
|
|
85
|
+
| `59 23 L * *` | Last day of every month at 11:59 PM |
|
|
86
|
+
|
|
87
|
+
## Recurring Workflows
|
|
88
|
+
|
|
89
|
+
Since `cronSleep` only sleeps until the **next** occurrence, you need to design your workflow architecture to handle recurring schedules.
|
|
90
|
+
|
|
91
|
+
### Pattern: Separate Scheduler and Processor Workflows
|
|
92
|
+
|
|
93
|
+
Separate your scheduling logic from your business logic by keeping workflows and steps in different files:
|
|
94
|
+
|
|
95
|
+
**`workflows/report-scheduler.ts`** - The scheduler workflow
|
|
96
|
+
```typescript
|
|
97
|
+
import { cronSleep } from "workflow-cron-sleep"
|
|
98
|
+
import { startReportProcessor } from "./steps"
|
|
99
|
+
|
|
100
|
+
interface ReportSchedulerInput {
|
|
101
|
+
reportId: string
|
|
102
|
+
cronExpression: string
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function reportScheduler(input: ReportSchedulerInput) {
|
|
106
|
+
"use workflow"
|
|
107
|
+
|
|
108
|
+
const { reportId, cronExpression } = input
|
|
109
|
+
|
|
110
|
+
// Wait for the next scheduled time
|
|
111
|
+
await cronSleep(cronExpression, { timezone: "America/New_York" })
|
|
112
|
+
|
|
113
|
+
// Trigger the processor workflow
|
|
114
|
+
await startReportProcessor(reportId)
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**`workflows/steps.ts`** - Step functions in a separate file
|
|
119
|
+
```typescript
|
|
120
|
+
import { start } from "workflow"
|
|
121
|
+
import { reportProcessorWorkflow } from "./report-processor"
|
|
122
|
+
|
|
123
|
+
export async function startReportProcessor(reportId: string) {
|
|
124
|
+
"use step"
|
|
125
|
+
await start(reportProcessorWorkflow, [{ reportId }])
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
This pattern keeps your scheduling and business logic decoupled, making each workflow easier to test and maintain.
|
|
130
|
+
|
|
131
|
+
## Requirements
|
|
132
|
+
|
|
133
|
+
- `workflow` package (Vercel Workflows SDK)
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for cronSleep function
|
|
3
|
+
*/
|
|
4
|
+
export interface CronSleepOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Timezone for the cron expression (e.g., "America/New_York", "Europe/London")
|
|
7
|
+
* If not specified, uses the system's local timezone
|
|
8
|
+
*/
|
|
9
|
+
timezone?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Sleeps until the next occurrence of a cron expression.
|
|
13
|
+
*
|
|
14
|
+
* This function parses the provided cron expression, calculates when it would
|
|
15
|
+
* next trigger, and suspends the workflow until that time.
|
|
16
|
+
*
|
|
17
|
+
* @param cronExpression - A valid cron expression (e.g., "0 9 * * *" for 9 AM daily)
|
|
18
|
+
* @param options - Optional configuration including timezone
|
|
19
|
+
* @returns A promise that resolves when the workflow resumes at the scheduled time
|
|
20
|
+
* @throws Error if the cron expression is invalid or has no future runs
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Sleep until the next 9 AM
|
|
24
|
+
* await cronSleep("0 9 * * *")
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Sleep until the next 9 AM in New York timezone
|
|
28
|
+
* await cronSleep("0 9 * * *", { timezone: "America/New_York" })
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // Sleep until the next Monday at 8:30 AM
|
|
32
|
+
* await cronSleep("30 8 * * 1")
|
|
33
|
+
*/
|
|
34
|
+
export declare function cronSleep(cronExpression: string, options?: CronSleepOptions): Promise<void>;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,SAAS,CAC7B,cAAc,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,IAAI,CAAC,CASf"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { sleep } from "workflow";
|
|
2
|
+
import { Cron } from "croner";
|
|
3
|
+
/**
|
|
4
|
+
* Sleeps until the next occurrence of a cron expression.
|
|
5
|
+
*
|
|
6
|
+
* This function parses the provided cron expression, calculates when it would
|
|
7
|
+
* next trigger, and suspends the workflow until that time.
|
|
8
|
+
*
|
|
9
|
+
* @param cronExpression - A valid cron expression (e.g., "0 9 * * *" for 9 AM daily)
|
|
10
|
+
* @param options - Optional configuration including timezone
|
|
11
|
+
* @returns A promise that resolves when the workflow resumes at the scheduled time
|
|
12
|
+
* @throws Error if the cron expression is invalid or has no future runs
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Sleep until the next 9 AM
|
|
16
|
+
* await cronSleep("0 9 * * *")
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Sleep until the next 9 AM in New York timezone
|
|
20
|
+
* await cronSleep("0 9 * * *", { timezone: "America/New_York" })
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Sleep until the next Monday at 8:30 AM
|
|
24
|
+
* await cronSleep("30 8 * * 1")
|
|
25
|
+
*/
|
|
26
|
+
export async function cronSleep(cronExpression, options) {
|
|
27
|
+
const cron = new Cron(cronExpression, { timezone: options?.timezone });
|
|
28
|
+
const nextRun = cron.nextRun();
|
|
29
|
+
if (!nextRun) {
|
|
30
|
+
throw new Error(`Invalid cron expression or no future runs: ${cronExpression}`);
|
|
31
|
+
}
|
|
32
|
+
await sleep(nextRun);
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAa7B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,cAAsB,EACtB,OAA0B;IAE1B,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,cAAc,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAA;IACtE,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;IAE9B,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,8CAA8C,cAAc,EAAE,CAAC,CAAA;IACjF,CAAC;IAED,MAAM,KAAK,CAAC,OAAO,CAAC,CAAA;AACtB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "workflow-cron-sleep",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Sleep until the next cron expression occurrence in Vercel Workflows",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"workflow",
|
|
17
|
+
"vercel",
|
|
18
|
+
"cron",
|
|
19
|
+
"sleep",
|
|
20
|
+
"scheduler",
|
|
21
|
+
"cron-expression"
|
|
22
|
+
],
|
|
23
|
+
"author": "Dave Daly",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/dalyd14/workflow-cron-sleep.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/dalyd14/workflow-cron-sleep/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/dalyd14/workflow-cron-sleep#readme",
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"workflow": ">=4.0.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"croner": "^8.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"typescript": "^5.3.0",
|
|
41
|
+
"workflow": "^4.0.1-beta.44"
|
|
42
|
+
}
|
|
43
|
+
}
|