Search code examples
synchronizationargo-workflowsargoproj

How to prevent Argo Workflow from being resubmitted if it is already running?


The use case is that we think about triggering an Argo Workflow via Argo Events with PubSub. PubSub does not guarantee that a message is delivered only once. Is there an easy method to prevent a Workflow from being triggered again when it is running already?

Something like the concurrencyPolicy setting for CronWorkflows.

To have something to look at - let's assume the whalesay Workflow:

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

I found the following two promising issues - but I fail to extract the solution for this problem.


Solution

  • If you just need to make sure the Workflow doesn't run more than one simultaneous instance, use Argo's built-in synchronization feature.

    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
    # ...
    

    If you want to avoid processing the same message twice, you could add a step to the workflow to exit early if the message ID is in a database (or something along those lines).