ENF News Bot
Automated News Bot
- Joined
- Apr 15, 2022
- Messages
- 2,591
You can now attach cron schedules directly to a Workflow binding in
For example, you can configure hourly, every-15-minute, or weekday schedules on the same Workflow:
Cron workloads get all the same benefits of Workflows with built-in retries, multi-step durable execution, and configurable timeouts of Workflows.
This makes it easier to build recurring, scheduled jobs such as database backups, invoice generation, report aggregation, and cleanup tasks without wiring up a separate Cron Trigger entrypoint.
For more information, refer to Trigger Workflows.
Continue reading...
wrangler.jsonc. Each scheduled run creates a new Workflow instance automatically, so you do not need to define a separate Worker with a scheduled handler just to trigger your Workflow on an interval.For example, you can configure hourly, every-15-minute, or weekday schedules on the same Workflow:
Code:
{
"workflows": [
{
"name": "my-scheduled-workflow",
"binding": "MY_WORKFLOW",
"class_name": "MyScheduledWorkflow",
"schedules": ["0 * * * *", "*/15 * * * *", "0 9 * * MON-FRI"],
},
],
}
Cron workloads get all the same benefits of Workflows with built-in retries, multi-step durable execution, and configurable timeouts of Workflows.
Code:
import {
WorkflowEntrypoint,
WorkflowEvent,
WorkflowStep,
} from "cloudflare:workers";
// Runs automatically on each cron schedule defined for the MY_WORKFLOW binding in wrangler.jsonc.
export class MyScheduledWorkflow extends WorkflowEntrypoint<Env> {
async run(event: WorkflowEvent, step: WorkflowStep) {
const data = await step.do("fetch source data", async () => {
return await fetchSourceData();
});
// If this step fails, only this step is retried with the custom logic below
await step.do(
"process and store results",
{
retries: { limit: 5, delay: "30 seconds", backoff: "exponential" },
timeout: "10 minutes",
},
async () => {
await processAndStore(data);
},
);
}
}
This makes it easier to build recurring, scheduled jobs such as database backups, invoice generation, report aggregation, and cleanup tasks without wiring up a separate Cron Trigger entrypoint.
For more information, refer to Trigger Workflows.
Continue reading...