Blocking invocations mean the platform won’t send the HTTP response until the action finishes. This allows it to include the action result in the response. Blocking invocations are used when you want to invoke an action and wait for the result.
1 2 3 4 5 6 7 8 9 10 11 12
Non-blocking invocations return as soon as the platform processes the invocation request. This is before the action has finished executing. HTTP responses from non-blocking invocations only include activation identifiers, as the action result is not available.
HTTP responses from a blocking invocation will only wait for a limited amount of time before returning. This defaults to 65 seconds in the platform configuration file. If an action invocation has not finished before this timeout limit, a HTTP 5xx status response is returned.
“So, how can you invoke an action and wait for the result when actions take longer than this limit?”
This question comes up regularly from developers building applications using the platform. I’ve decided to turn my answer into a blog post to help others struggling with this issue (after answering this question again this week 😎).
- Invoke the action using a non-blocking invocation.
- Use the returned activation identifier to poll the activation result API.
- The HTTP response for the activation result will return a HTTP 404 response until the action finishes.
When polling for activation results from non-blocking invocations, you should enforce a limit on the maximum polling time allowed. This is because HTTP 404s can be returned due to other scenarios (e.g. invalid activation identifiers). Enforcing a time limit ensures that, in the event of issues in the application code or the platform, the polling loop with eventually stop!
Setting the maximum polling time to the action timeout limit (plus a small offset) is a good approach.
An action cannot run for longer than its timeout limit. If the activation record is not available after this duration has elapsed (plus a small offset to handle internal platform delays), something has gone wrong. Continuing to poll after this point runs the risk of turning the polling operation into an infinite loop…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
testing it out
Here is the source code for an action which will not return until 70 seconds have passed. Blocking invocations firing this action will result in a HTTP timeout before the response is returned.
1 2 3 4 5
Using the script above, the action result will be retrieved from a non-blocking invocation.
- Create an action from the source file in the example above.
- Run the Node.js script to invoke this action and poll for the activation result.
If the script runs correctly, log messages will display the polling status and then the activation result.
1 2 3 4 5 6 7 8