8
n8n 中文网amn8n.com

使用Redis防止工作流同时执行

高级

这是一个自动化工作流,包含 17 个节点。主要使用 If, Redis, Filter, ManualTrigger, ExecuteWorkflow 等节点。 使用Redis防止工作流同时执行

前置要求
  • Redis 服务器连接信息

分类

-
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "378c072a34d9e63949fd9cf26b8d28ff276a486e303f0d8963f23e1d74169c1b"
  },
  "nodes": [
    {
      "id": "3035a456-e783-4ac3-a6b7-1925a81672c1",
      "name": "执行工作流",
      "type": "n8n-nodes-base.executeWorkflow",
      "onError": "continueRegularOutput",
      "position": [
        1980,
        440
      ],
      "parameters": {
        "options": {},
        "workflowId": "4cnnwIeC9Sr5ngGZ"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "c2d4d0f3-5f84-41de-9a06-4cd5a19e3337",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        860,
        440
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "seconds",
              "secondsInterval": 5
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "deb51138-4f68-4e8e-8118-d29bd4b79cd6",
      "name": "获取状态",
      "type": "n8n-nodes-base.redis",
      "position": [
        1080,
        440
      ],
      "parameters": {
        "key": "=workflowStatus_{{ $workflow.id }}",
        "options": {},
        "operation": "get",
        "propertyName": "=workflowStatus"
      },
      "credentials": {
        "redis": {
          "id": "Hvn2Vf7bGjmFgDr0",
          "name": "Redis account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "73d4e23e-7860-4ac1-8f90-781817e2c98b",
      "name": "设置为运行中",
      "type": "n8n-nodes-base.redis",
      "position": [
        1760,
        440
      ],
      "parameters": {
        "key": "=workflowStatus_{{ $workflow.id }}",
        "value": "running",
        "operation": "set"
      },
      "credentials": {
        "redis": {
          "id": "Hvn2Vf7bGjmFgDr0",
          "name": "Redis account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "c7bc785f-dbb0-48be-98ef-d0f940be7749",
      "name": "设置为空闲",
      "type": "n8n-nodes-base.redis",
      "position": [
        2200,
        440
      ],
      "parameters": {
        "key": "=workflowStatus_{{ $workflow.id }}",
        "value": "idle",
        "operation": "set"
      },
      "credentials": {
        "redis": {
          "id": "Hvn2Vf7bGjmFgDr0",
          "name": "Redis account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f65c374e-f189-4d43-9b45-53776a74cbf2",
      "name": "如果空闲则继续",
      "type": "n8n-nodes-base.filter",
      "position": [
        1540,
        360
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "0c6308f0-1c96-41a9-b821-97031454d555",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.workflowStatus }}",
              "rightValue": "idle"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "45956f6b-52bf-48d2-8c68-0aa1fa338f8f",
      "name": "Redis 键存在",
      "type": "n8n-nodes-base.if",
      "position": [
        1300,
        440
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "a844597a-21f9-4869-9abb-4e4b1530931a",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.workflowStatus }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "29896363-cb31-4940-9cef-a993b931484d",
      "name": "无操作",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1540,
        520
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "7a8b0ceb-0c9c-4aa5-9cbb-68a7aee3641f",
      "name": "当点击\"测试工作流\"时",
      "type": "n8n-nodes-base.manualTrigger",
      "disabled": true,
      "position": [
        860,
        740
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "d9ab7f18-fe96-4514-a576-49e50575185f",
      "name": "重置为空闲",
      "type": "n8n-nodes-base.redis",
      "disabled": true,
      "position": [
        1080,
        740
      ],
      "parameters": {
        "key": "=workflowStatus_{{ $workflow.id }}",
        "value": "idle",
        "operation": "set"
      },
      "credentials": {
        "redis": {
          "id": "Hvn2Vf7bGjmFgDr0",
          "name": "Redis account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "043e00ca-d191-4b54-b0ec-c14e87a5facb",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        811,
        614
      ],
      "parameters": {
        "color": 5,
        "width": 433,
        "height": 300,
        "content": "## 故障排除"
      },
      "typeVersion": 1
    },
    {
      "id": "dc045338-4e41-41f3-b197-704e7560c54a",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1696,
        320
      ],
      "parameters": {
        "color": 7,
        "width": 222,
        "height": 281,
        "content": "这会更新标志,表示工作流当前正在运行"
      },
      "typeVersion": 1
    },
    {
      "id": "5b8dae2f-c2cf-4e16-b4a2-bc97bb64b9e0",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        810.8170310701956,
        320
      ],
      "parameters": {
        "width": 205.18296892980436,
        "height": 280,
        "content": "## 设置间隔"
      },
      "typeVersion": 1
    },
    {
      "id": "d55419d3-84e4-4b73-ae4c-a719d94f9bae",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1248,
        286
      ],
      "parameters": {
        "color": 7,
        "width": 445,
        "height": 382,
        "content": "如果存储在 Redis 中的标志已存在且表示工作流仍在运行,将阻止另一次执行。在这种情况下,此工作流在此结束。"
      },
      "typeVersion": 1
    },
    {
      "id": "55ab702d-0feb-4d13-a42c-f9aed6d4389d",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1920,
        320
      ],
      "parameters": {
        "width": 218,
        "height": 281,
        "content": "## 设置工作流 ID"
      },
      "typeVersion": 1
    },
    {
      "id": "7d469990-d3d7-41a1-9a59-7fcf76472342",
      "name": "便利贴5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1020,
        320
      ],
      "parameters": {
        "color": 7,
        "width": 222,
        "height": 281,
        "content": "这会检查动态标志(包含工作流 ID),该标志表示工作流当前是否正在运行。"
      },
      "typeVersion": 1
    },
    {
      "id": "0d96a5e4-40a2-4abf-96be-7e17c187bc3d",
      "name": "便签 6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2140,
        320
      ],
      "parameters": {
        "color": 7,
        "width": 222,
        "height": 281,
        "content": "这会更新标志,表示工作流当前处于空闲状态"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Get Status": {
      "main": [
        [
          {
            "node": "Redis Key exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Running": {
      "main": [
        [
          {
            "node": "Execute Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Operation": {
      "main": [
        [
          {
            "node": "Set Running",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Continue if Idle": {
      "main": [
        [
          {
            "node": "Set Running",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow": {
      "main": [
        [
          {
            "node": "Set Idle",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Redis Key exists": {
      "main": [
        [
          {
            "node": "Continue if Idle",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \"Test workflow\"": {
      "main": [
        [
          {
            "node": "Reset to Idle",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量17
分类-
节点类型8
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者

Workflow Optimization Expert | Software Architect. Use my link to book an initial consultation for custom built workflows using n8n.

外部链接
在 n8n.io 查看

分享此工作流