{
  "id": "IP7HrftumgPGb0sM",
  "name": "Prevent simultaneous workflow executions V2",
  "tags": [],
  "nodes": [
    {
      "id": "58dc1dd1-38f7-4cab-aa91-007c35a72f07",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        64,
        112
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "seconds",
              "secondsInterval": 5
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "28085fe4-2fd3-4c36-81eb-34bbec6e6f4f",
      "name": "Execute Workflow",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        960,
        -64
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": ""
        }
      },
      "typeVersion": 1.1,
      "alwaysOutputData": false
    },
    {
      "id": "c0346bd9-2854-453d-a6c0-b25bd38a70b5",
      "name": "Get Flag",
      "type": "n8n-nodes-base.redis",
      "position": [
        288,
        112
      ],
      "parameters": {
        "key": "=workflowRunning_{{ $workflow.id }}",
        "options": {},
        "operation": "get",
        "propertyName": "=workflowRunning"
      },
      "credentials": {
        "redis": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "1c671fda-a479-47a1-acfd-23b98f514de5",
      "name": "Set Flag",
      "type": "n8n-nodes-base.redis",
      "position": [
        736,
        112
      ],
      "parameters": {
        "key": "=workflowRunning_{{ $workflow.id }}",
        "value": "running",
        "expire": true,
        "operation": "set"
      },
      "credentials": {
        "redis": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "75a1b909-0b39-4afc-812e-5dafc51fcef9",
      "name": "Remove Flag",
      "type": "n8n-nodes-base.redis",
      "position": [
        960,
        288
      ],
      "parameters": {
        "key": "=workflowRunning_{{ $workflow.id }}",
        "operation": "delete"
      },
      "credentials": {
        "redis": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7319acc6-6696-4b3d-9394-3b89effb0442",
      "name": "Flag does not exists",
      "type": "n8n-nodes-base.filter",
      "position": [
        512,
        112
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "6b3359cb-c8fc-4663-b2a9-e367ad4fcba5",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $json.workflowRunning }}",
              "rightValue": "={{ null }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "b268fe1f-200c-484d-81c6-2cd967960963",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -752,
        -128
      ],
      "parameters": {
        "width": 640,
        "height": 576,
        "content": "## How it works\n- This is a separate workflow which monitors the execution of the main workflow\n- Stores a flag in Redis (key dynamically named after workflow ID) which indicates if the main workflow is running or idle\n- The TTL setting ensures that the flag will also be removed automatically when the workflow hits the defined timeout\n- Only calls the main workflow if the last execution has finished\n\n## Setup\n- [ ] Update the interval in the Schedule Trigger\n- [ ] Turn your main workflow into a sub-workflow by adding an **Execute Workflow Trigger**\n- [ ] Set a reasonable timeout for that sub-workflow\n- [ ] Set the same timeout (seconds) in the TTL option of the **Set Flag** node\n- [ ] Select the right sub-workflow in the **Execute Workflow** node\n- [ ] Publish the workflow\n\n## Customization\n- Duplicate the **Execute Workflow** node and select another workflow you would like to run sequentially, then replace the placeholder node underneath with it\n- Make sure to set a timeout for that other workflow as well and then update the TTL value in the Redis node to the sum of all workflow timeouts\n- You can add as many workflow calls as you want, just make sure they are neatly organized below each other in one line to guarantee the right execution order"
      },
      "typeVersion": 1
    },
    {
      "id": "1c286399-3c27-491b-916f-adb49465b8cf",
      "name": "Optional: Execute another workflow",
      "type": "n8n-nodes-base.noOp",
      "position": [
        960,
        112
      ],
      "parameters": {},
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "5780a2be-c497-4ad8-b76d-4d4bbb754770",
  "connections": {
    "Get Flag": {
      "main": [
        [
          {
            "node": "Flag does not exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Flag": {
      "main": [
        [
          {
            "node": "Execute Workflow",
            "type": "main",
            "index": 0
          },
          {
            "node": "Remove Flag",
            "type": "main",
            "index": 0
          },
          {
            "node": "Optional: Execute another workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow": {
      "main": [
        []
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Flag",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flag does not exists": {
      "main": [
        [
          {
            "node": "Set Flag",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}