VMware Cloud Community
DavidM_WLF
Contributor
Contributor

issue with error handling when using foreach to launch workflow

Hello,

I am having an issue when using the foreach loop to launch a workflow. I am iterating over a list of servers.

I have chosen the option to "Catch any exception and continue with the next iteration", but any time the child workflow throws an error, the entire workflow fails....   it does not continue to the next iteration.

What I would like to have happen is for the error to be caught, so I can report that the iteration failed for a specific target, while continuing to process the rest of the targets. 

I considered trying to manually track success failure and suppress error-throwing in the child workflow, but I would rather not do that.  The child workflow can be run independently, and I would like it to correctly report as successful/failed.

Any ideas?

David

0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee

Hi David,

This should work. Which vRO version do you use?

What kind of error is thrown by the child workflow? Eg. a Javascript error like if (some-condition) throw "error occurred", or something different? It would be great if you can prepare a small sample workflow(s) demonstrating the issue, and attach it here.

0 Kudos
jasnyder
Hot Shot
Hot Shot

The best option here is likely to manually build a loop and call the each workflow for each server asynchronously.  You then collect all the workflow tokens and save them for the end of the main workflow run.  Let it block at the end in a loop checking the status of the child async workflows until they report completed.  Then log whether they finished successfully or with error.  Another benefit of the async approach is that the workflow tokens will be collected independently of the main workflow within the vRO client so you can inspect the status of the workflow run later just like any other workflow you kick off.  It won't be hidden inside the main workflow run, which enables you to inspect them individually and right click the run and say "run again" if you want to isolate it and try again.

You can build the loop in the schema or use a scriptable task to do it.

The script would look something like this.  It takes 2 inputs, an array called servers (in my example, just an array of strings) and a Workflow object which is the workflow you want to run for each iteration of the loop.

var wfTokens = new Array();

for(var s in servers) {

    var props = new Properties();

   

    //push workflow inputs

    props.put("serverName", servers[s]);

   

    wfTokens[s] = wf.execute(props, null, "");

}

var stillRunning = true;

//wait while all workflows complete

while(stillRunning) {

    stillRunning = false;

   

    for(var t in wfTokens) {

        var state = Server.getWorkflowTokenState(wfTokens[t].id);

        if(state == "running" || state == "waiting" || state == "active" || state == "suspended") stillRunning = true;

    }

   

     //sleep for 5 seconds before checking status again

    if(stillRunning) System.sleep(5000);

}

//iterate through the collection of tokens and check the state.  If it failed, output the exception that was thrown.

for(var t in wfTokens) {

    var state = Server.getWorkflowTokenState(wfTokens[t].id);

   

    if(state == "failed") {

        System.log(wfTokens[t].id + " - failed - " + wfTokens[t].exception)

    }

    else {

        System.log(wfTokens[t].id + " - " + state);

    }

}

0 Kudos