Solved

How does the while loop in Calm actually work?

  • 15 December 2021
  • 4 replies
  • 212 views

Userlevel 2
Badge +2

Hi!

I need help wrapping my head around how the while loop in Calm works. The documentation is unfortunately not helpful at all. :/

The first thing I can’t understand is why a while loop has a fixed number of iterations. This to me is a for loop. A while loop runs while a condition isn’t met.

The second thing that’s confusing is the exit condition that I can set to Success, Failure, or Don’t Care. I guess that Don’t Care turns the while loop into an actual for loop?

The Success or Failure condition is as far as I can tell completely out of my hands as this is based on the result of the execution of the task(s) within the loop and not a parameter I control. Do I have to fail a task on purpose to keep the loop going if I choose Success as my exit condition?

What happens if I have multiple tasks in the loop, do all tasks need to fail or succeed for the exit condition to trigger?

Is it possible at all to set a parameter in one of my scripts and then use that as an exit condition?

Thanks.

icon

Best answer by SrinathGS 16 December 2021, 04:41

View original

This topic has been closed for comments

4 replies

Hi Martin,

 

In most programming languages, While & For loop are usually convertible. If a variable initialisation, break condition and variable update mechanisms are given together, it constitutes a For loop and if just the break condition is present in the loop, it is a While loop. 

 

But, our implementation is inspired from a pythonic way of defining this, where For loop iterates over an iterable, such as a list, set, or a dictionary, whereas a While loop contains a condition. 

 

You can consider our While loop to be of the following implementation.

 

iteration = 0

max_iteration = n

can_continue = true

while (iteration < max_iteration) or can_continue:

    can_continue = loop_body()

    iteration += 1

 

here, the can_continue can be manipulated by the task that the loop is executing and currently, can_continue can be manipulated by the status of the loop_body as you’ve found out.  

 

Following is an example escript where I achieved the same. 

 

example escript
While loop configuration

This while loop exited on the 7th iteration when the task succeeded. 

 

Number of iterations for example while loop.

 

Please let me know if you need more details. 

Userlevel 2
Badge +2

Hi Srinath.

Thanks for the explanation.

I’m not familiar with Python so I can’t say if this is a “valid” while loop in Python, but as someone who grew up coding C and C++ I’d say that using a for loop as a while loop isn’t exactly considered best practices. :)

Your implementation still seems like an unnecessarily complicated way of doing a loop as it forces me to specify a maximum number of iterations when I might not know how many iterations that I require.

Relying on the exit status of the script - and I still don’t know which or all of the scripts that triggers an exit condition - is extremely crude as I might use my exit codes for other purposes.

Why not have a proper while loop that monitors a parameter I set in anyone of the scripts in the loop? Calm reads the output of the “Set variable” types of scripts as is so why not use that as the condition?

For the time being we’ll be coding loops directly in the scripts, which is a shame as we’d very much like to use the UI of Calm to illustrate the logic of a runbook, without having to open up a script.

Userlevel 4
Badge +5

Hi Martin,

I’d suggest using the Nutanix Support portal you raise a RFE to help improving this FEAT to make it more user friendly.

Thank you for the feedback. 

Userlevel 2
Badge +2

Thanks Jose, will do!