How to cancel a scheduled workflow but allow all it's activities to finish

Hi,

I’m using the code from the tutorial with set CronSchedule: “* * * * */1” in client.StartWorkflowOptions and as expected the workflow is being triggered each minute (I’m running into one issue here, current run is always in Running state even though from logs I can see that all activities already finished, and status is changed to Completed once another workflow run is triggered by the cron).

I can trigger CancelWorkflow or TerminateWorkflow and it will stop the cron to spawn new workflow instances but it will also stop currently running workflow instance and it’s activities.

I tried using ctx, _ = workflow.NewDisconnectedContext(ctx) in TransferMoney workflow definition. In case of terminate, workflow will be moved to Terminated state, current activity will finish but next activity wont be triggered and following error can be found in the logs: Worker Error workflow execution already completed.

If I run cancel with that disconnected context workflow instance won’t be cancelled and new instance will be scheduled by cron.

Is there a way to stop the scheduler but allow current run of the workflow to finish?

You will want to use cancel and not terminate if you want graceful completion.

How are you determining running state? If you are using the client and APIs to describe/list execution, the state update of the workflow is eventually consistent with the actual state and you may have to wait a short bit.

Can you clarify “run cancel with that disconnected context workflow”? The disconnected context is only used for activity execution, correct? Then the activity can run to completion.

See samples-go/cancellation at main · temporalio/samples-go · GitHub for an example of workflow cancellation that cancels running activities and runs a cleanup activity with a disconnected context. If you didn’t want the activity to be cancelled when the workflow is, you can use the disconnected context too.