0

我们有一个使用基于 cron 的调度的工作流。我们需要支持一个用例来更改 cron 表达式。

这样做的最佳做法是什么?

4

1 回答 1

1

TL;博士

使用相同的 workflowID 再次启动相同的 cron 工作流,IDReusePolicy =TerminteIfRunning

例子

与文档中一样,CRON 仅在取消或终止时才会停止。所以你也可以终止/取消,然后开始一个新的。但是如果你自己使用两个请求来做,没有一致性保证。

使用 IDReusePolicy =TerminteIfRunning将确保 terminate+start 是 Cadence 中的原子操作。

这是使用它的示例

1.启动一个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. 启动 cron 工作流程
$./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. 更新 cron 工作流程
$./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

请注意,在 CLI 中:--workflowidreusepolicy 3将设置 IDReusePolicy =TerminteIfRunning

CLI 使用将在此PR之后更新。

  1. 然后您应该能够看到 helloworld 工作流打印新值:
$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"}
...
于 2021-03-23T03:25:34.853 回答