It is relatively straightforward to turn any Temporal workflow into a Cron workflow. All you need is to supply a cron schedule when starting the workflow using the CronSchedule parameter of StartWorkflowOptions.
You can also start a workflow using the Temporal CLI with an optional cron schedule using the
For workflows with CronSchedule:
- Cron schedule is based on UTC time. For example cron schedule "15 8 * * *" will run daily at 8:15am UTC.
- If a workflow failed and a RetryPolicy is supplied to the StartWorkflowOptions as well, the workflow will retry based on the RetryPolicy. While the workflow is retrying, the server will not schedule the next cron run.
- Temporal server only schedules the next cron run after the current run is completed. If the next schedule is due while a workflow is running (or retrying), then it will skip that schedule.
- Cron workflows will not stop until they are terminated or cancelled.
Temporal supports the standard cron spec:
The crontab guru site is useful for testing your cron expressions.
Convert existing cron workflow
Before CronSchedule was available, the previous approach to implementing cron
workflows was to use a delay timer as the last step and then return
ContinueAsNew. One problem with that implementation is that if the workflow
fails or times out, the cron would stop.
To convert those workflows to make use of Temporal CronSchedule, all you need is to
remove the delay timer and return without using
ContinueAsNew. Then start the workflow with the desired CronSchedule.
Retrieve last successful result
Sometimes it is useful to obtain the progress of previous successful runs.
This is supported by two new APIs in the client library:
GetLastCompletionResult. Below is an example of how
to use this in Go:
Note that this works even if one of the cron schedule runs failed. The next schedule will still get the last successful result if it ever successfully completed at least once. For example, for a daily cron workflow, if the first day run succeeds and the second day fails, then the third day run will still get the result from first day's run using these APIs.