Cancelling cron scheduled workflow gracefully

I have a use case where I have a workflow consisting of few activities. The workflow is cron scheduled and will execute after every 30 days after getting started. There is an api exposed which can use to cancel the workflow. But the thing I found through the docs is every cron scheduled workflow will execute as a new workflow and all the local variables are getting refreshed whenever any workflow is exposed. I need to know the following two things:

  1. I want to stop the schedule for next run, but the current execution shall continue until the last activity. Not sure if there’s any trick to achieve the stop scheduled periodic workflow gracefully?
  2. How can i can propagate state of workflow in every cron run. Like values of local variables if updated in last run should be picked updated in next/current run.
  1. I want to stop the schedule for the next run, but the current execution shall continue until the last activity. Not sure if there’s any trick to achieve the stop scheduled periodic workflow gracefully?

We are looking into improving the default CRON workflow experience to implement your requirements out of the box. Now your best option is to implement the periodic workflow yourself. See HelloPeriodic sample that demonstrates how it can be done. In your case, you can implement workflow cancellation logic according to your business requirements.

  1. How can i can propagate state of workflow in every cron run. Like values of local variables if updated in last run should be picked updated in next/current run.

It depends on how long you want your periodic logic to execute. If you have a fixed number of months, like 12 or 36, you might not need to call continue as new and just use a single loop for the whole workflow. If your number of iterations is very large or potentially unbounded, you have to call continue as new. In this case, you’ll have to pass all the state to the next run through the workflow arguments.

If you keep using the built-in CRON functionality, consider using Workflow.getLastCompletionResult to read the previous workflow run result.

Thanks @maxim I think I should go with the combination. My business logic needs to run as long as customer has not invoked any cancellation. So it can be like in years. But I am thinking of running a loop for like 3 years(means 36 months) and after that if no cancellation has been invoked then I will just continue that same workflow as new.