It's often preferable or even necessary to make a script wait a little while before performing the next operation. For example, if you want a script to retry a failed HTTP post, you might want it to wait for a few seconds before trying again to avoid any rate limiting or traffic throttling. For an example of how to add retry logic to a script, see this knowledge base article.
Note: We recommend that scripts with wait times run on an xMatters Agent if at all possible. This is because scripts running on an agent are not subject to the 60-second time limit that applies to scripts running in the xMatters cloud, and less likely to be terminated. For a complete explanation of running scripts on an xMatters Agent, see this page in the online help.
Sample code
Assuming that your script is running on an xMatters Agent, the following code will allow you to specify how long the script should wait before proceeding to the next instruction.
var waitTimeMs = 10000; // number of milliseconds to wait before proceeding
var thread = Java.type("java.lang.Thread"); // allows execution of the script to be halted for a specified number of milliseconds.
// simple code to add timestamp data to comments
var date = new(Date);
var dateStr = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds() + " : " ;
console.log( dateStr + "Waiting for " + waitTimeMs + " milliseconds..." );
thread.sleep( waitTimeMs );
date= new(Date);
dateStr = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds() + " : " ;
console.log( dateStr + "Finished waiting..." );
To help illustrate the process, the script includes a simple way to add timestamps to the log messages. These timestamps are in local time if run on an agent, and in UTC if run on the xMatters cloud. While the timestamp code is rudimentary, you can see a more robust example of how to use moment.js to control timestamp formatting, time zones, and more in this knowledge base article.
Comments
0 commentsPlease sign in to leave a comment.