Starting and stopping cron-scheduled workflows


Suppose I have an activity that I want to run every day at noon between October 25th and November 8th. I need to launch a workflow today (September 21st) that will execute the activity at its scheduled times.

I can use a cronSchedule set to “0 12 * * *” to handle the the activity runs, provided the date is between 10/25 and 11/8. My question is: What’s the best way to handle the activity start and stop dates?

Is this something the cron string can handle, or should I use Workflow.sleep to wait until 10/25 and then on each run compare the date to 11/8, at which point I could cancel the workflow? Or is there a better way to do this?

Thank you!

Workflow has two timeouts: WorkflowRunTimeout and WorkflowExecutionTimeout. The run timeout defines the maximum time a single workflow run (which corresponds to a single cron invocation) can take. It includes all possible activity retries. The execution timeout defines the maximum time the workflow execution (which includes all the runs) executes. In the case of the cron, the execution timeout defines at which point the cron stops executing.

So if you just need to define when cron stops use the execution timeout. As for the start date the cron itself doesn’t have any specific support. I think the simplest solution is to have a workflow that sleeps until 10/25 and then starts a cron workflow child with an appropriate execution timeout.

Another option is to implement all the cron logic within your workflow code using sleep and a loop. See HelloPeriodic Java sample.

1 Like

Thanks, Maxim, I appreciate the quick and thorough response!