Sleep function in server side script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2017 07:51 AM
In my server side script, I am triggering an action via an outbound ReST call. The asynchronous operation returns immediately, but provides a URL which I can poll for the operation's completion. I would like to keep the function atomic, so would like to poll the URL, and if the operation hasn't yet completed, would like to wait for a second or so and poll again, and do that for several times (I do not expect the operation to take more than 10-20 seconds, normally not more than 5 seconds) until done.
I am looking for something like setTimeout or setInterval function that I could use on the server, so that I can tell my script to wait, but can't find anything. Am I missing something? Can somebody point me to a solution for this kind of problem?
I have seen a timer activity in Workflow, which would pause the workflow for a specified period of time; it can probably serve as a workaround, but I wanted a programmatic (and programmable) solution. Will be grateful for any guidance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2022 06:31 AM
Thank you! It works. This is not in the documentation and the link to the wiki page is no longer available.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2022 06:44 AM
Watch out! The instance becomes unusable while using gs.sleep and running the script in background script. It seems to be putting the whole instance to sleep!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2017 08:03 AM
There is an undocumented function gs.sleep(milliseconds) that perhaps can help you here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2017 08:30 AM
Göran,
As I noted in the thread with Chuck Tomasi, I have a scoped application and can't use gs.sleep(). Thanks for the advice anyway!
Any other possibilities I should consider?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2017 06:16 AM
I've seen this workaround, never tested it thou.. a selfmade sleep function:
- /* time is in milliseconds - this is essentially a "sleep" function */
- wait: function(time){
- var t1 = new GlideDateTime().getNumericValue();
- var t2 = new GlideDateTime().getNumericValue();
- var duration = t2 - t1;
- while(duration < time){
- t2 = new GlideDateTime().getNumericValue();
- duration = t2 - t1;
- }
- },
from here: gs.sleep() in scoped application?