Implement timeout in Run Server Side Script

ayano
Giga Guru

Is it possible to write a script in "Run Server Side Script" that will execute a timeout for 590 seconds?
Like this timeout in the picture.

find_real_file.png

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ayano 

you can use this in the run server side script step to pause for the seconds

(function executeStep(inputs, outputs, stepResult, timeout) {

        var secondsValue = 590;
	var seconds = parseInt(secondsValue, 10) * 1000;
	var start = parseInt(new Date().getTime()) + seconds;
	while(start>parseInt(new Date().getTime())){
		// do nothing
	}
	stepResult.setOutputMessage('This step gave sleep of 590 seconds');
	stepResult.setSuccess();

}(inputs, outputs, stepResult, timeout));

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

On server scripts you can use the global gs.sleep method to pause the script in milliseconds

gs.sleep(590000); //590 seconds

 

Community Alums
Not applicable

Just as a warning on this - it can be dangerous if that script will run concurrently. 

gs.sleep will keep control of the thread, and on smaller instances (example with only two app nodes) you can quickly run out of threads and cause an instance outage.

This script pauses the entire instance. I pasted it in my widget server script and everybody on the team can't code anything in the instance for 10 minutes lol

Ankur Bawiskar
Tera Patron
Tera Patron

@ayano 

you can use this in the run server side script step to pause for the seconds

(function executeStep(inputs, outputs, stepResult, timeout) {

        var secondsValue = 590;
	var seconds = parseInt(secondsValue, 10) * 1000;
	var start = parseInt(new Date().getTime()) + seconds;
	while(start>parseInt(new Date().getTime())){
		// do nothing
	}
	stepResult.setOutputMessage('This step gave sleep of 590 seconds');
	stepResult.setSuccess();

}(inputs, outputs, stepResult, timeout));

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader