Pausing workflow after multiple errors

would it be possible to somehow pause a workflow after several errors in a row?

I have a workflow that starts every 1 minute to update the data.

And in case of error, I get a message.

the problem occurs when an error occurs, because every 1 minute a new message is resent because the workflow tries to process the same data with an error.

My intention was that when a workflow failed 2 runs in a row it would be paused.

That way I could fix the error and activate the workflow manually.

No, that is sadly currently not easily possible.

You can however theoretically build it yourself with an Error Workflow that uses the internal (and so undocumented) API to query the past executions and then also use the internal API to deactivate the workflow. Would however be a little bit more complicated and take so more time.

via http: //localhost:5678/rest/executions-current

returns executions in progress, is there any way to pull the execution history?

that way I would be able to analyze the time of the last execution of the error, and with an IF node not send the message

That would be without -current so: http://localhost:5678/rest/executions

1 Like

I’m impressed with the amount of content in this community and also with everyone’s willingness to help.
I managed to find a solution to the problem.

I would like to leave my contribution

fThe original workflow only starts if the error flow is no longer running.

And the workflow error checks if it already has a running in progress, if it doesn’t it checks the last 2 dates of the run error and checks if the interval is less than 3 minutes. If it is, it checks the date of the last update that occurred in the flow.

If the update date is greater than the error date, it closes the error.

This way, it is possible to check if the error correction has already been done.

If not corrected it will wait 1h and check again.

This way the original stream will not run because it is waiting for the error stream to be terminated

I only ran a few tests, there may be an error, but I’m sharing if it helps someone else

{
  "nodes": [
    {
      "parameters": {},
      "name": "Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -20,
        600
      ]
    },
    {
      "parameters": {
        "functionCode": "const executions = items[0].json.data.filter(execution => execution.workflowId.toString() === $workflow.id.toString());\n\nreturn [\n  {\n    json: {\n      isActive: executions.length > 1,\n    }\n  }\n];"
      },
      "name": "Função para consultar se está em execução",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        440,
        600
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json[\"isActive\"]}}",
              "value2": true
            }
          ]
        }
      },
      "name": "Erro em execução?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        640,
        600
      ]
    },
    {
      "parameters": {},
      "name": "Está em execução",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        800,
        440
      ]
    },
    {
      "parameters": {
        "authentication": "basicAuth",
        "url": "http://localhost:5678/rest/executions-current",
        "options": {}
      },
      "name": "Execuções em andamento",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        220,
        600
      ],
      "credentials": {
        "httpBasicAuth": {
          "id": "9",
          "name": "Conta N8N"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "fluxotrabalho",
              "value": "={{$node[\"Error Trigger\"].json[\"workflow\"][\"id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1060,
        740
      ],
      "notesInFlow": true,
      "notes": "Webhook mockup"
    },
    {
      "parameters": {
        "mode": "passThrough",
        "output": "input2"
      },
      "name": "Merge11",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1300,
        640
      ]
    },
    {
      "parameters": {
        "authentication": "basicAuth",
        "url": "http://localhost:5678/rest/executions",
        "options": {},
        "headerParametersUi": {
          "parameter": []
        },
        "queryParametersUi": {
          "parameter": [
            {
              "name": "\"id\"",
              "value": "\"50207\""
            }
          ]
        }
      },
      "name": "Histórico de execuções",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        860,
        620
      ],
      "credentials": {
        "httpBasicAuth": {
          "id": "9",
          "name": "Conta N8N"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const subtrairDatas = $node[\"datas formatada\"].json[\"work1\"]-$node[\"datas formatada\"].json[\"work2\"];\nitems[0].json.today = subtrairDatas;\nitems[0].json.Minutos = subtrairDatas /60/1000;\n\nreturn items;"
      },
      "name": "subtração de data1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        2640,
        660
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nconst datas = $item(\"0\").$node[\"Histórico de execuções\"].json[\"data\"][\"results\"]\n\nconst workflow = items[0].json.fluxotrabalho\n\nfor (const datae of datas) {\n    if (datae.workflowId === workflow ) {\n      results.push({ json: { dataflow: datae.startedAt } })\n    }\n  }\n\n\nreturn results;"
      },
      "name": "retornar datas",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1500,
        640
      ]
    },
    {
      "parameters": {},
      "name": "1h",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        3780,
        740
      ],
      "webhookId": "a9114420-1621-4b38-a1ab-ef4b925aecaa"
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "fluxoUpgrade",
              "value": "={{$node[\"Error Trigger\"].json[\"workflow\"][\"id\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set11",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1060,
        1060
      ],
      "notesInFlow": true,
      "notes": "Webhook mockup"
    },
    {
      "parameters": {
        "mode": "passThrough",
        "output": "input2"
      },
      "name": "Merge1111",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1300,
        960
      ]
    },
    {
      "parameters": {
        "authentication": "basicAuth",
        "url": "http://localhost:5678/rest/workflows",
        "options": {}
      },
      "name": "Lista de Workflow",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        860,
        940
      ],
      "typeVersion": 1,
      "credentials": {
        "httpBasicAuth": {
          "id": "9",
          "name": "Conta N8N"
        }
      },
      "notes": "Don't forget to add your credentials for your n8n instance in this Node. Use Basic Auth for this. "
    },
    {
      "parameters": {
        "conditions": {
          "string": [],
          "number": [
            {
              "value1": "={{$node[\"subtração de data1\"].json[\"Minutos\"]}}",
              "value2": 3
            }
          ]
        }
      },
      "name": "menos de 3 minutos?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        2840,
        660
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nconst lista = $item(\"0\").$node[\"Lista de Workflow\"].json[\"data\"]\n\nconst fluxo = items[0].json.fluxoUpgrade\n\nfor (const datae of lista) {\n    if (datae.id === fluxo ) {\n      results.push({ json: { UpdateFluxo: datae.updatedAt } })\n    }\n  }\n\n\nreturn results;"
      },
      "name": "data update",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1520,
        960
      ]
    },
    {
      "parameters": {
        "mode": "wait"
      },
      "name": "Aguardar datas",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        2240,
        660
      ]
    },
    {
      "parameters": {
        "operation": "aggregateItems",
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "dataflow"
            }
          ]
        },
        "options": {}
      },
      "name": "Ultimas 2 execuções",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1900,
        640
      ]
    },
    {
      "parameters": {
        "operation": "limit",
        "maxItems": 2
      },
      "name": "ultimos 2 resultados",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1700,
        640
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "work1",
              "value": "={{new Date($node[\"Ultimas 2 execuções\"].json[\"dataflow\"][0]).getTime()}}"
            },
            {
              "name": "work2",
              "value": "={{new Date($node[\"Ultimas 2 execuções\"].json[\"dataflow\"][1]).getTime()}}"
            },
            {
              "name": "data-update",
              "value": "={{new Date($node[\"data update\"].json[\"UpdateFluxo\"]).getTime()}}"
            }
          ]
        },
        "options": {}
      },
      "name": "datas formatada",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        2420,
        660
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [],
          "number": [
            {
              "value1": "={{$node[\"datas formatada\"].json[\"data-update\"]}}",
              "value2": "={{new Date($node[\"Ultimas 2 execuções\"].json[\"dataflow\"][0]).getTime()}}"
            }
          ]
        }
      },
      "name": "Sem atualização?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        3100,
        540
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "responseFormat": "string",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "="
      },
      "name": "Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        3340,
        680
      ]
    }
  ],
  "connections": {
    "Error Trigger": {
      "main": [
        [
          {
            "node": "Execuções em andamento",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Função para consultar se está em execução": {
      "main": [
        [
          {
            "node": "Erro em execução?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Erro em execução?": {
      "main": [
        [
          {
            "node": "Está em execução",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Histórico de execuções",
            "type": "main",
            "index": 0
          },
          {
            "node": "Lista de Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execuções em andamento": {
      "main": [
        [
          {
            "node": "Função para consultar se está em execução",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "Merge11",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge11": {
      "main": [
        [
          {
            "node": "retornar datas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Histórico de execuções": {
      "main": [
        [
          {
            "node": "Merge11",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "subtração de data1": {
      "main": [
        [
          {
            "node": "menos de 3 minutos?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "retornar datas": {
      "main": [
        [
          {
            "node": "ultimos 2 resultados",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1h": {
      "main": [
        [
          {
            "node": "Execuções em andamento",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set11": {
      "main": [
        [
          {
            "node": "Merge1111",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge1111": {
      "main": [
        [
          {
            "node": "data update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lista de Workflow": {
      "main": [
        [
          {
            "node": "Merge1111",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set11",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "menos de 3 minutos?": {
      "main": [
        [
          {
            "node": "Sem atualização?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "data update": {
      "main": [
        [
          {
            "node": "Aguardar datas",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Aguardar datas": {
      "main": [
        [
          {
            "node": "datas formatada",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ultimas 2 execuções": {
      "main": [
        [
          {
            "node": "Aguardar datas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ultimos 2 resultados": {
      "main": [
        [
          {
            "node": "Ultimas 2 execuções",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "datas formatada": {
      "main": [
        [
          {
            "node": "subtração de data1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sem atualização?": {
      "main": [
        [
          {
            "node": "1h",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like