A workflow executes its elements along its path one after another. Using asynchronous workflow execution, we can change this behavior and actually execute workflows in parallel.
We will need to build a new workflow.
For the first example, we will reuse the sleep workflow as well as the getNow
action we created in the recipe Waiting tasks in this chapter. (06.04.01 Sleep
and the com.packtpub.Orchestrator-Cookbook2ndEdition.helpers
module in the example pack).
We will see two examples to demonstrate the asynchronous feature.
Here are some basics:
Name |
Type |
Section |
Use |
|
Number |
IN |
It defines the time the workflow should sleep |
|
WorkflowToken |
Attribute |
The workflow token of the asynchronous workflow |
06.04.01 Sleep
in the example package).sleepTime
in-parameter.workflowToken
. Bind it to the wfToken
attribute.getNow
action on each side of the Asynchronous workflow tasks.
Have a look at the execution. The workflow that started the sleep workflow asynchronously has finished (A in the previous screenshot) but the sleep workflow is still running (B). Also, please note that the sleep workflow doesn't write its logs into the main workflow.
This example shows how to combine asynchronous workflows with custom events:
06.04.01
in the example package).
Name |
Type |
Section |
Use |
|
String |
IN |
This transports the custom event name |
eventName
in-parameter to it.
Name |
Type |
Section |
Use |
|
Number |
IN |
Time the asynchronous workflow for when it should sleep. |
|
String |
IN |
This is a string that contains an event name. |
|
Date |
IN |
This is the date/time until the workflow should wait. |
|
Boolean |
Attribute |
This is the return value from the wait for a custom event. |
|
WorkflowToken |
Attribute |
This is the return value from an asynchronous task. |
|
Boolean |
Attribute |
Set to |
3.15.2
in the example package).getNow
actions (see the following figure).
You will see that the main workflow will wait until it has received the custom events, which indicates that the asynchronous workflow has finished.
Normally, a workflow executes one element after the other in a serial approach. From time to time, this can mean that your main program has to wait for some other tasks to finish before continuing. Using asynchronous execution, we can make workflows execute in parallel. A typical example is that you clone a VM (which can take a few minutes), and while it clones, you can create a new PortGroup
that you can later attach to the cloned VM. To do this, create a new workflow that runs the create VM
workflow asynchronously and sends a custom event when it is finished. In the meantime, the main workflow creates the PortGroup
and then waits for the custom event to signal that the VM is ready. After the custom event has arrived, you can then map the PortGroup
to the VM.
One thing you have to watch out for is a so-called race condition. If you start a workflow asynchronously and in sequence and then wait for the other, you might run into this. If the asynchronous workflow finishes before the sequential does, the event is already sent but nobody is waiting for it, resulting in the fact that the event wait will time out. In this case, you should either use the workflow token (see the recipe Scripting with workflow tokens in this chapter) or add a sleep to the asynchronous task to make sure it finishes after the sequential one. As an example for this, check out the example workflow, 06.06.04 wait for asynchronous (Race condition)
.