cadence-workflowcadenceuber-cadence

Cadence: What is the best practice to change the workflow cron schedule?


We have a workflow that uses cron based scheduling. We need to support a use case to change the cron expression.

What is the best practice to do so?


Solution

  • TL;DR

    Start the same cron workflow again with the same workflowID, with IDReusePolicy = TerminteIfRunning

    Example

    Like in the documentation, CRON will stop only when canceled or terminate. So you can also terminate/cancel, and then start a new one. But there is no consistency guarantee if you use two requests to do it yourself.

    Using IDReusePolicy = TerminteIfRunning will make sure terminate+start is an atomic operation in Cadence.

    Here is the example of using it

    1.Start a helloworld worker

    ./bin/helloworld -m worker &
    [1] 24808
    2021-03-22T20:08:09.404-0700    INFO    common/sample_helper.go:97  Logger created.
    2021-03-22T20:08:09.405-0700    DEBUG   common/factory.go:131   Creating RPC dispatcher outbound    {"ServiceName": "cadence-frontend", "HostPort": "127.0.0.1:7933"}
    ...
    ...
    
    1. Starting a cron workflow
    $./cadence  --do samples-domain wf start --tl helloWorldGroup -w "test-cron" --execution_timeout 10 --cron "* * * * *" --wt "main.helloWorldWorkflow" -i '"Hello"'
    Started Workflow Id: test-cron, run Id: 2d9f06f9-7e79-4c9d-942a-e2c6a20c9f85
    
    1. Update the cron workflow
    $./cadence  --do samples-domain wf start --tl helloWorldGroup -w "test-cron" --execution_timeout 10 --cron "* * * * *" --wt "main.helloWorldWorkflow"  -i '"Cadence"'  --workflowidreusepolicy 3 
    Started Workflow Id: test-cron, run Id: 4344448d-5a95-4a91-a56e-ebc0b93b4d29
    

    NOTE that in CLI: --workflowidreusepolicy 3 will set the IDReusePolicy = TerminteIfRunning

    The CLI usage will be updated after this PR.

    1. Then you should be able to see the helloworld workflow print the new value:
    $2021-03-22T20:24:00.307-0700   INFO    helloworld/helloworld_workflow.go:29    helloworld workflow started {"Domain": "samples-domain", "TaskList": "helloWorldGroup", "WorkerID": "24808@IT-USA-25920@helloWorldGroup", "WorkflowType": "main.helloWorldWorkflow", "WorkflowID": "test-cron", "RunID": "1e2e6d2f-dcc7-410f-8d06-81c94622bbb7"}
    2021-03-22T20:24:00.307-0700    DEBUG   internal/internal_event_handlers.go:470 ExecuteActivity {"Domain": "samples-domain", "TaskList": "helloWorldGroup", "WorkerID": "24808@IT-USA-25920@helloWorldGroup", "WorkflowType": "main.helloWorldWorkflow", "WorkflowID": "test-cron", "RunID": "1e2e6d2f-dcc7-410f-8d06-81c94622bbb7", "ActivityID": "0", "ActivityType": "main.helloWorldActivity"}
    ...