"Looping" within an SDA Process


Have you ever wanted to loop within an SDA process, something like this?


Unfortunately, what I've drawn above isn't supported, and while you can design it within the process editor you can't run it. Attempting to do so will result in an execution error.  But don't despair, there is another way to achieve similar results.


While SDA doesn't support loops within processes, it does allow a process to invoke itself. The use-cases are going to vary, and what you use as your "loop control" mechanism will depend on your use case.  In some cases, for instance database deployments, there is a manifest or control file that contains the scripts to run, and the idea is to read the file and for each script process it within it's own loop. While we always recommend using an actual configuration tool for database deployments (i.e. DaticalDB for example), we also understand that it may be challenging to implement if you're not doing it already, and sometimes we need to take a phased approach to automation which may require using a manifest or control file initially. Long story short, there are valid reasons for needing to "loop" a process, so let's talk about how we can do it.

For this example, we're going to write to a file and use the "Monitor File Contents" plug-in step to exit after 3 "loops".  Our process is shown in Figure 2.

Figure 2.

Here's what we are doing:

  1. "Run some stuff" is where we are doing whatever needs to be done in this loop.  For this example, we are simply using the shell command to echo to a file (echo|set /p=1 >> temp.out). Nothing really special in this case, but if this was a real process this is where I'd be doing whatever needs to be done within each "loop".
  2. "Monitor File Contents" checks to see if the condition that would cause us to either loop or exit the loop exists.  In this case, it's looking for a string "1 1 1" in our output file.  This is what is being echo'd in step 1, so after 3 runs through this step should return true and exit the "loop".
  3. "Loop" is a "Run generic process" step, and invokes the "Loop Example" process which is this same process.  In short, it's calling itself.  It's going to cause this entire process to be invoked again.
  4. The "Complete" step will only happen once, and it'll happen in whichever "loop" we are in when the control step equals true.

Let's take a look at this in action.

First, we run the process.  The output is shown in Figure 3. below.  You'll see my first step returns "Success".  The second one, "Monitor File Contents", returns "Failed".  This failure takes us down the failure path to the loop step.  We'll also see that next to the Loop Failure indicator is a link to "View Child Process". Because this process run went down the loop path, it'll never hit the "Complete" step.

Figure 3.

Drilling into the second process run using the "View Child Execution", we can see the results in Figure 4 below. Because this is a child process, we can see the "View Parent Details" link.  Clicking this would take us back to the output displayed in Figure 3.  This second process-run did our "Do some stuff" step, and then went into "Monitor File Contents" which failed.  As before, this failure causes us to go down the loop path, so this child has a child of it's own, which we can drill into by clicking the "View Child Execution" link.

Figure 4.

Drilling into the third process run, we can see the results in Figure 5 below.  As before, we "Do some stuff", but this time when the "Monitor File Contents" step runs it finds our target string in our output file that has been being built, and returns true.  Because we get a true value here, we take the appropriate branch towards "Complete".

Figure 5.


This is a basic example of how you can have a process invoke itself in order to "loop".  As I mentioned before, your use-case and method of controlling the loop logic is going to vary, but if you get stuck ask for help in the forums and we're happy to help you out.

The example (generic) process is attached.  If you run this, you'll need to manually clean up the control (temp.out) file in your agent directory, otherwise this will only work the first time through.  I used a generic process for this example, however the concept works just as well in component processes.




How To-Best Practice
Comment List
Related Discussions