Alternative of gs.sleep() in Scripted Rest API

mansigoel
Tera Contributor

Hi,

 

I want a delay of 10 seconds in my scripted REST API Script. 

It is working through gs.sleep() -  my usecase is to wait for sc_task to be generated after creation of RITM.

 

But, since this is not a best practice to use gs.sleep() , I want an alternative approach to implement.

 

Can anyone help on this?

1 ACCEPTED SOLUTION

Amitoj Wadhera
Kilo Sage

Hi @mansigoel ,

 

You can use: 

var startTime = new Date().getTime();
                var delay = 10000; // Simulating a delay of 10 seconds 
while (new Date().getTime() < startTime + delay) {
                    // Simulating the delay
                }

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

View solution in original post

4 REPLIES 4

dgarad
Giga Sage

Hi @mansigoel 

refer to the below link for waiting response.

https://www.servicenow.com/community/developer-forum/how-to-wait-the-script-until-receive-response-f...

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Amitoj Wadhera
Kilo Sage

Hi @mansigoel ,

 

You can use: 

var startTime = new Date().getTime();
                var delay = 10000; // Simulating a delay of 10 seconds 
while (new Date().getTime() < startTime + delay) {
                    // Simulating the delay
                }

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

I recommend against a solution that would freeze threads (e.g. while loop or gs.sleep). ServiceNow instances have a discreet number of threads available to handle incoming requests (these are known as Semaphore Pools). If you hang the threads doing pointless loops or sleep operations then you are wasting scarce resources that could be used to handle other incoming integration traffic. In the worst case, you could completely use up all your semaphores and any new requests would be stuck until threads are available.

 

Instead you should look at asynchronous implementation methods, for example, you could use the events queue or scheduled job methods to schedule some operation to happen 10 seconds in the future.

 

https://www.servicenow.com/docs/bundle/xanadu-platform-administration/page/administer/platform-event...

 

https://www.servicenow.com/community/developer-articles/custom-event-queues/ta-p/3045097

 

It is undocumented, but something I've done in the past is to directly insert records into the sys_trigger table with the "next_action" field set to some time in the future. This has the advantage of leveraging the 8 scheduled worker threads and potentially clogging the Default event queue (you can make custom event queues as noted above, but that is an extra step).

 

Here's an example:

 

var script = "script here";
var nowGDT = new GlideDateTime();
nowGDT.add(1000); // add 10 seconds to the date time

var job = new GlideRecord("sys_trigger");
job.initialize();
job.setValue("trigger_type", 0);// 0 is run once, 2 is ondemand
job.setValue("script", script);
job.setValue("next_action", nowGDT);
job.setValue("name", "testName");
job.insert();

 

 

palanikumar
Mega Sage

Hi @mansigoel,

Please explain in detail.

  • Is third party system calling your API and you want to delay the response for 10 secs? Delaying API response is not advisable. We should 
  • Or you are calling an API from Workflow or BR? Please explain how you are calling them.
Thank you,
Palani