Skip to content

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 VM from recipe 3.1. It takes a VC:VirtualMachine input called vm and returns a string output called summary.
  • 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

  1. Create a workflow called Start a VM description in VCF Orchestrator Guide/Chapter 3, with an input vm of type VC:VirtualMachine.
  2. 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.
  3. When asked, promote the input vm to the existing workflow input.
  4. Open Options. Under Output Options, the element has one output, wfToken of type WorkflowToken. Bind it to a new variable describeToken.
  5. Add a scriptable task after the element, called Report the token, with describeToken in:
Start a VM description — Report the token
System.log("Started " + describeToken.name + ", state " + describeToken.state);
Schema tab of Start a VM description: Start, Describe a VM (asynchronous element), Report the token, End. The element is selected. Options, Output Options: wfToken of type WorkflowToken bound to describeToken. Workflow: Select workflow. Inputs: vm of type VC:VirtualMachine bound to vm.
Figure 3.27. An asynchronous element, binding the child's token to describeToken.

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.

  1. Save and run it. The parent completes almost at once, and its log shows the child in the running state. Open Activity > Workflow Runs: the run of Describe a VM finishes on its own a moment later, with its own logs and its own summary.

Run several workflows at once and collect the results

  1. Create a workflow called Describe VMs in parallel, with an input vms of type Array/VC:VirtualMachine and an output summaries of type Array/string.
  2. On the Variables tab, add:
    • describeWorkflow, of type Workflow. Click its value and pick Describe a VM from the library.
    • timeoutSeconds, of type number, with the value 300.
  3. Add a scriptable task called Start one run per VM, with vms and describeWorkflow in and a new variable tokens of type Array/WorkflowToken out:
Describe VMs in parallel — Start one run per VM
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);
  1. Add a second scriptable task called Wait and collect, with vms, tokens, and timeoutSeconds in and summaries out:
Describe VMs in parallel — Wait and collect
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"));
  1. Save and run it with three VMs. While it runs, open Activity > Workflow Runs in a second browser tab: three runs of Describe a VM start within a second of each other, instead of one after another.
  2. When the parent completes, its summaries output 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.
Figure 3.28. The variables of Describe VMs in parallel. For a Workflow variable, the Value column shows [object Workflow], not the name of the workflow it holds.
Activity, Workflow Runs, filtered on Describe. One run of Describe VMs in parallel started at 11:38:56 PM and ended at 11:38:59 PM; three runs of Describe a VM all started and ended at 11:38:57 PM.
Figure 3.29. Three child runs started together by one parent.
Needs a lab check. Run steps 7–12 on 9.1.1. Confirm that a 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.

Needs a lab check. Find the 9.1.1 defaults for concurrent and queued workflow runs (the properties com.vmware.vco.workflow-engine.executors-count and com.vmware.vco.workflow-engine.executors-max-queue-size), for example in vracli vro properties advanced.
Back in vRO 5.5–7Find the child runs of a parent in the Java client's inventory tree, as tokens under each workflow, and refresh the tree to see their state.
In Orchestrator 9.xSee every run, parent and child, in one list under Activity > Workflow Runs, and filter it by workflow or state.

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