nubronco
Enthusiast
Enthusiast

Wait timer using a random time

Jump to solution

I have created some workflows that work 99% of the time. The 1% they don't work is because they are trying to post data to a wiki via a JSON rest call at the exact same second based on the workflow runtime in vCO. If I put in a wait time task I can't figure out how I would go about setting the attribute to wait a random amount of seconds. If I just tell it to wait 1 minute it will still run into the same issue so that won't help. Also, telling it to run at a certain time in the future wouldn't work as both of the workflows would use the same code to decide the future time.

Has anyone come up with a way to generate a random number (think seconds) with a maximum that then could be passed to the attribute? This would not guarantee but should greatly increase my chances of multiple workflows not calling the wiki at the same time. Yes, ideally the wiki would be able to handle this but it can't so I must find a work-around. Thanks for any help.

0 Kudos
1 Solution

Accepted Solutions
Burke-
VMware Employee
VMware Employee

var num = Math.floor(Math.random()*(max - min)+min);

System.log("Random Number between "+min+" - "+max+": " +  num

Here you go. Just provide a value for min and max and you'll have a random number generated Smiley Happy

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter

View solution in original post

0 Kudos
2 Replies
Burke-
VMware Employee
VMware Employee

var num = Math.floor(Math.random()*(max - min)+min);

System.log("Random Number between "+min+" - "+max+": " +  num

Here you go. Just provide a value for min and max and you'll have a random number generated Smiley Happy

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
0 Kudos
nubronco
Enthusiast
Enthusiast

This is the code I actually ended up using. Still can run into overlaps but think this will help.

var now = new Date();

var pauseUntil = new Date(now);

var randomSecondsToWait = Math.floor(Math.random()*(maxRandom - minRandom) + minRandom);

System.log("Value of randomSecondsToWait equals " + randomSecondsToWait);

pauseUntil.setSeconds(pauseUntil.getSeconds() + randomSecondsToWait);

System.log("Current time is " + now.toISOString() + ", waiting until " + pauseUntil.toISOString());

0 Kudos