Scheduled workflows? #66
-
|
Is there or will there be a way to schedule workflows using CRON expressions? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
|
I think the idea is you use |
Beta Was this translation helpful? Give feedback.
-
I think so. You can set up a cron route and then run -> await start(yourWorkflow) |
Beta Was this translation helpful? Give feedback.
-
|
We do want to have native cron support in workflow. I'll write an RFC for it shortly. In the meantime, you can either
function cronWorkflow() {
"use workflow";
while (true) {
await sleep("1 day"); // or whatever
someStep(); // you can await if you want, but don't have to. This can "run in the background"
}
}
function someStep() {
"use workflow";
// do things...
}Note: I'm calling a workflow inside a workflow. This works but doesn't actually do anything today (it's treated as a normal function in the workflow) - but we are working on "child workflows" soon so this appears better in o11y etc (see #69) Note 2: A problem with this approach is that the run is tied to whatever vercel deployment you start it on. If you deploy a new version where you changed something in the cron, you will need to cancel and start it again. Hence why the vercel cron approach might be better If you care about consistently starting at the same time, no matter when you triggered the workflow, you can do this function tillTomorrowAt6AM() {
const now = new Date();
return new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1,
6, 0, 0, 0
);
}
function cronWorkflow() {
"use workflow";
while (true) {
await sleep(tillTomorrowAt6AM());
someStep();
}
}
function someStep() {
"use workflow";
// do things...
} |
Beta Was this translation helpful? Give feedback.
We do want to have native cron support in workflow. I'll write an RFC for it shortly. In the meantime, you can either
await start(yourWorkflow)Note: I'm calling a workflow inside a workflow. This works but doesn't actually do anything today (it's treated as a normal function in the workflow) …