问题描述
用例是我们考虑通过 Argo Events with PubSub 触发 Argo 工作流。 PubSub 不保证消息只传递一次。是否有一种简单的方法可以防止工作流在运行时再次被触发?
类似于 CronWorkflows 的 concurrencyPolicy
设置。
有一些东西要看 - 让我们假设 whalesay 工作流程:
apiVersion: argoproj.io/v1alpha1
kind: Workflow # new type of k8s spec
metadata:
name: hello-world # name of the workflow spec
namespace: argo
spec:
entrypoint: whalesay # invoke the whalesay template
templates:
- name: whalesay # name of the template
container:
image: docker/whalesay
command: [cowsay]
args: ["hello world"]
resources: # limit the resources
limits:
memory: 32Mi
cpu: 100m
我发现了以下两个有希望的问题 - 但我未能提取此问题的解决方案。
- https://github.com/argoproj/argo-workflows/issues/2677
- https://github.com/argoproj/argo-workflows/issues/2550
解决方法
如果您只需要确保工作流不会同时运行多个实例,请使用 Argo 的内置 synchronization 功能。
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
workflow: "1" # Only one workflow can run at given time in particular namespace
---
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: hello-world
spec:
entrypoint: whalesay
synchronization:
semaphore:
configMapKeyRef:
name: my-config
key: workflow
templates:
- name: whalesay
container:
image: docker/whalesay
# ...
如果您想避免处理同一条消息两次,您可以在工作流中添加一个步骤,以便在消息 ID 位于数据库(或类似内容)中时提前退出。