How to retry a failed call with a loop
This guide shows you how to make a workflow send a request again after the request fails. The workflow waits before each new try. It stops after a number of tries that you set.
You need a workflow with an External API node first. Refer to How to call an external API from a workflow.
The loop
A loop is a path of edges that goes back to a node that the run reached before. This loop has three nodes:
| Node | Function |
|---|---|
External API send | Sends the request. |
Condition check | Decides if the workflow sends the request again. |
Sleep pause | Waits before the next try. |
When you save a workflow, Picked refuses a loop that does not have a sleep node and a condition node on it. Picked also refuses an edge from a node to the same node.
1. Connect the error exit to a condition
- Open the workflow.
- Choose Add condition on the error exit of
send. - In the rule of
check, test{{ nodes.send.error.retryable }}. - Connect the
falseexit ofcheckto the nodes that record the failure.
check runs only when the call fails. The rule can then read the error of the last try.
2. Add the sleep node
- Choose Add sleep on the
trueexit ofcheck. - Set Wait for to the time between two tries.
- Connect the exit of
pauseto the input ofsend.
The editor shows a loop as an edge that goes back to send.
3. Set the number of tries
Each node in the loop runs one time for each try. A run can visit one node a maximum number of times. This number is the visit cap of the workflow.
- Open the workflow settings.
- Set Visit cap to a number from 1 to 100. If you do not set it, the visit cap is 10.
A node can get to the visit cap. If the run then gets to the node again, the run stops.
The run shows Failed with the code loop_limit_reached.
To stop the loop before the visit cap, test current.visit in the rule of check.
Its value is the number of the visit to check. This is also the number of failed tries.
This rule tries a maximum of 3 times:
{
"and": [{ "var": "nodes.send.error.retryable" }, { "<": ["{{ current.visit }}", 3] }]
}With this rule, the run continues on the false exit of check after 3 failures.
4. Read the result
A node reads the last visit of another node. After the loop, {{ nodes.send.status }}
gives the status of the last try.
The Tasks card of the run shows each visit as a task. Refer to the Workflow runs reference.
Related
- Workflow runs — the tasks of each visit, and the visit cap.
- Workflow templates — the data that a node reads in a loop.
