workflow-cron-sleep 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/README.md +36 -5
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -97,7 +97,7 @@ Separate your scheduling logic from your business logic by keeping workflows and
97
97
  import { cronSleep } from "workflow-cron-sleep"
98
98
  import { startReportProcessor } from "./steps"
99
99
 
100
- interface ReportSchedulerInput {
100
+ export interface ReportSchedulerInput {
101
101
  reportId: string
102
102
  cronExpression: string
103
103
  }
@@ -111,7 +111,30 @@ export async function reportScheduler(input: ReportSchedulerInput) {
111
111
  await cronSleep(cronExpression, { timezone: "America/New_York" })
112
112
 
113
113
  // Trigger the processor workflow
114
- await startReportProcessor(reportId)
114
+ await startReportProcessor(reportId, cronExpression)
115
+ }
116
+ ```
117
+
118
+ **`workflows/report-processor.ts`** - The processor workflow (business logic)
119
+ ```typescript
120
+ import { restartScheduler } from "./steps"
121
+
122
+ export interface ReportProcessorInput {
123
+ reportId: string
124
+ cronExpression: string
125
+ }
126
+
127
+ export async function reportProcessorWorkflow(input: ReportProcessorInput) {
128
+ "use workflow"
129
+
130
+ const { reportId, cronExpression } = input
131
+
132
+ // Your business logic here
133
+ await generateReport(reportId)
134
+ await sendNotifications(reportId)
135
+
136
+ // Restart the scheduler for the next occurrence
137
+ await restartScheduler(reportId, cronExpression)
115
138
  }
116
139
  ```
117
140
 
@@ -119,14 +142,22 @@ export async function reportScheduler(input: ReportSchedulerInput) {
119
142
  ```typescript
120
143
  import { start } from "workflow"
121
144
  import { reportProcessorWorkflow } from "./report-processor"
145
+ import { reportScheduler } from "./report-scheduler"
122
146
 
123
- export async function startReportProcessor(reportId: string) {
147
+ export async function startReportProcessor(reportId: string, cronExpression: string) {
124
148
  "use step"
125
- await start(reportProcessorWorkflow, [{ reportId }])
149
+ await start(reportProcessorWorkflow, [{ reportId, cronExpression }])
150
+ }
151
+
152
+ export async function restartScheduler(reportId: string, cronExpression: string) {
153
+ "use step"
154
+ await start(reportScheduler, [{ reportId, cronExpression }])
126
155
  }
127
156
  ```
128
157
 
129
- This pattern keeps your scheduling and business logic decoupled, making each workflow easier to test and maintain.
158
+ This pattern keeps your scheduling and business logic decoupled, and creates a recurring loop: scheduler processor → scheduler → ...
159
+
160
+ For this pattern, it is recommended to return and persist your `runId` for each scheduler workflow run, in case you need to cancel a run. If the cron expression changes, you can cancel the current scheduler run and create a new one with the new cron expression.
130
161
 
131
162
  ## Requirements
132
163
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "workflow-cron-sleep",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Sleep until the next cron expression occurrence in Vercel Workflows",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -40,4 +40,4 @@
40
40
  "typescript": "^5.3.0",
41
41
  "workflow": "^4.0.1-beta.44"
42
42
  }
43
- }
43
+ }