Asynchronous and nested workflows
Recipe 3.3 used a foreach element to run Describe a VM once for each VM in a list. Each run waited for the one before it to finish. That’s the safe default, and it’s what the workflow element does too: the parent stops, the child runs, and the parent carries on with the child’s outputs. When the children are slow and don’t depend on each other, waiting for each in turn wastes time. Orchestrator can also start a child and carry on at once, start several children together, or set one up to run later.
In this recipe you start Describe a VM without waiting for it, then build Describe VMs in parallel, which starts one run per VM, waits for all of them, and collects their summaries.
Getting ready
- The workflow
Describe a VMfrom recipe 3.1. It takes aVC:VirtualMachineinput calledvmand returns astringoutput calledsummary. - The folder VCF Orchestrator Guide/Chapter 3.
- Three or more VMs in the lab. They’re only read from.
How to do it
Start a workflow and carry on
- Create a workflow called
Start a VM descriptionin VCF Orchestrator Guide/Chapter 3, with an inputvmof typeVC:VirtualMachine. - On the Schema tab, drag an Asynchronous element from the Generic group onto the arrow between start and end. Under Workflow, choose
Describe a VM. The element takes the name of the workflow it calls. - When asked, promote the input
vmto the existing workflow input. - Open Options. Under Output Options, the element has one output,
wfTokenof typeWorkflowToken. Bind it to a new variabledescribeToken. - Add a scriptable task after the element, called
Report the token, withdescribeTokenin:
System.log("Started " + describeToken.name + ", state " + describeToken.state);
After you save and reopen the workflow, the Workflow box can show Select workflow again, as in the figure. The element still calls Describe a VM: its inputs, and the run in step 6, show it.
- Save and run it. The parent completes almost at once, and its log shows the child in the
runningstate. Open Activity > Workflow Runs: the run ofDescribe a VMfinishes on its own a moment later, with its own logs and its ownsummary.
Run several workflows at once and collect the results
- Create a workflow called
Describe VMs in parallel, with an inputvmsof typeArray/VC:VirtualMachineand an outputsummariesof typeArray/string. - On the Variables tab, add:
describeWorkflow, of typeWorkflow. Click its value and pickDescribe a VMfrom the library.timeoutSeconds, of typenumber, with the value300.
- Add a scriptable task called
Start one run per VM, withvmsanddescribeWorkflowin and a new variabletokensof typeArray/WorkflowTokenout:
tokens = [];for (var i = 0; i < vms.length; i++) { var props = new Properties(); props.put("vm", vms[i]); tokens.push(describeWorkflow.execute(props));}System.log("Started " + tokens.length + " runs of " + describeWorkflow.name);- Add a second scriptable task called
Wait and collect, withvms,tokens, andtimeoutSecondsin andsummariesout:
function isFinished(token) { var state = token.state; return state === "completed" || state === "failed" || state === "canceled";}
var deadline = new Date().getTime() + timeoutSeconds * 1000;var pending = tokens.length;while (pending > 0) { if (new Date().getTime() > deadline) { throw pending + " runs still unfinished after " + timeoutSeconds + " seconds"; } System.sleep(2000); pending = 0; for (var i = 0; i < tokens.length; i++) { if (!isFinished(tokens[i])) { pending++; } }}
summaries = [];for (var j = 0; j < tokens.length; j++) { if (tokens[j].state === "completed") { summaries.push(tokens[j].getOutputParameters().get("summary")); } else { summaries.push(vms[j].name + ": run " + tokens[j].state + ", " + tokens[j].exception); }}System.log(summaries.join("\n"));- Save and run it with three VMs. While it runs, open Activity > Workflow Runs in a second browser tab: three runs of
Describe a VMstart within a second of each other, instead of one after another. - When the parent completes, its
summariesoutput holds one line per VM, in the same order as the input.
![Variables tab of Describe VMs in parallel: describeWorkflow, type Workflow, value [object Workflow], used by Start one run per VM; timeoutSeconds, number, 300, used by Wait and collect; tokens, Array/WorkflowToken, not set, used by both elements.](/vcf-orchestrator-guide/screenshots/ch03/fig-3-28.png?v=6dd22e76)

Workflow variable’s value can be picked from the library on the Variables tab, that Workflow.execute accepts a Properties object of inputs, the token state strings completed, failed, and canceled, and that getOutputParameters().get(“summary”) returns the child’s output.How it works
Every run of a workflow, parent or child, is a workflow token: its own record in the database, with its own slots, logs, and state. What the calling elements differ in is whether the parent waits for the child’s token to finish.
- A workflow element calls the child synchronously. The child runs in the same memory space as the parent. The parent waits, then gets the child’s outputs through ordinary bindings. If the child fails, the element fails, and the parent’s exception path or default error handler takes over (recipe 3.4).
- An asynchronous element starts the child and moves on to the next element at once. It hands back the child’s token,
wfToken. The child’s outputs aren’t bound, because they don’t exist yet, and if the child fails, the parent doesn’t notice unless it looks. - A schedule workflow element creates a task that runs the child once, at a date you bind, and hands back the task. The parent carries on straight away.
- A nested workflows element starts several workflows at the same moment, and waits for all of them to finish before the parent carries on.
Workflow.execute is the scripting equivalent of the asynchronous element. It returns a WorkflowToken straight away, and the run it starts is a normal run like any other, queued and executed by the server’s workflow engine. The token object is live: each time the script reads token.state, it gets the current state from the server. A token’s state is one of running, waiting, waiting-signal, canceled, completed, or failed. The script waits for the last three, so a child that stops to wait for a person or an event (recipe 3.7) is treated as still running until the timeout. The wait loop itself polls with System.sleep, so it holds a thread while it waits, like the loop in recipe 3.3. Keep timeoutSeconds in minutes, not hours.
getOutputParameters() returns a Properties object of the child’s outputs, keyed by name. token.exception holds the error of a failed child, so the parent can report it without opening the child run.
The workflow engine runs a limited number of workflows at once on each node, and queues the rest. Broadcom documents the defaults as 300 running workflows per node and 10,000 in the queue, and says that runs fail when the queue is full. A loop that starts one child per item can reach those numbers faster than you’d think.
com.vmware.vco.workflow-engine.executors-count and com.vmware.vco.workflow-engine.executors-max-queue-size), for example in vracli vro properties advanced.There's more
Why not a foreach? The foreach element is simpler: no tokens, no wait loop, and its outputs are collected for you. Use it unless running the children together really saves time, for example when each one waits on a slow vCenter task. Parallel runs also put load on the target system all at once, so don’t start a hundred clones on one datastore just because you can.
On 9.1.1 the foreach element runs its iterations one after another: in the lab, each Describe a VM run wrote both of its log lines before the next one started, and the outputs came back in input order.
Limit how many run at once. To describe 200 VMs in batches of 10, start ten runs, wait for them, then start the next ten. Put the start and wait scripts in a loop on the canvas, or in one scriptable task.
The library’s own wait action. The module com.vmware.library.workflow has an action, waitAllWorkflowComplete, that saves writing the loop in step 10. Its input is tokens (Array/WorkflowToken) and it returns Array/Properties: one entry per child, in the same order, holding the child’s outputs plus __state, and __exception when the child failed. A failed child doesn’t make it throw, so check __state yourself. It checks every half second and has no timeout: a child that hangs keeps the parent waiting for ever. Use your own loop, as in step 10, when you need a deadline.
Ready-made library workflows . Under Library > Workflows there are three that do this for you:
| Workflow | Inputs | What it does |
|---|---|---|
| Start workflows in parallel | wf (Workflow), parameters (Array/Properties) |
Starts wf once per entry in parameters, all at once, waits for them, and returns their tokens in the output worfklowTokens (the typo is in the product). |
| Start workflows in a series | the same | Runs them one after another; output workflowTokens. |
| Run a workflow on a selection of objects | an object type, a workflow, and either an action that finds the objects or an array of them | Runs the workflow once per vCenter object. Set simulate to true to see which objects it would pick without running anything. |
Each entry of parameters is a Properties with one key per input of the child, and the values must be real objects, such as a VC:VirtualMachine, not their IDs. Build the array in a scriptable task, then call the library workflow with a workflow element. In the lab, three runs of Describe a VM took 2 seconds in a series. In parallel they all started in the same second, but the parent took 16 seconds to report back, because it checks on its children only every few seconds. So for short children, a series can finish first.
Several different workflows at once. When the children are different workflows, a nested workflows element does the starting and waiting for you. Drag it onto the canvas, click Add Workflow for each child, and bind each one’s inputs and outputs. The parent carries on when every child has finished. The element can also start workflows on another Orchestrator server, or with different credentials. For that, every input of the remote workflow must make sense on the remote server, and both servers need the same plug-ins and content.
The schema element reference has a working example: Nested workflows.
Run it later. A schedule workflow element takes a date input, workflowScheduleDate, and returns a Task, scheduledTask, for the scheduled run. See Schedule workflow in the reference. In a script, describeWorkflow.schedule(props, startDate) does the same. A run scheduled this way runs once. Chapter 7 covers recurring schedules.
Don’t start a child and forget it. An asynchronous child that fails fails silently, as far as the parent is concerned. Either check its token later, as step 10 does, or make sure the child reports its own failures, for example by sending a mail or failing loudly enough for monitoring to see.
See also
- 3.3 Decisions, switches, and loops, for the foreach element.
- 3.7 Waiting tasks and custom events, for children that wait.
- Broadcom TechDocs (VCF 9): Call a Workflow Synchronously and Schema Elements and Schema Element Properties.
- Broadcom TechDocs (Aria Automation Orchestrator): Calling Workflows Within Workflows and Schema Elements.
- Broadcom TechDocs (Aria Automation 8.16): Configure the workflow run properties.
- VMware type definitions for
Workflow,WorkflowToken, andServer: build-tools-for-vmware-aria, o11n-core.
