Server side scripting: Job submission - running jobs sequentially, or waiting between jobs.

Debraj G_
ServiceNow Employee
ServiceNow Employee

Hi,

Using server-side scripts, I have a series of jobs that I am submitting (FYI - ML/predictive intelligence training jobs) to be run on historical instance data. Each job takes about 6 hours worth of created incidents and runs these jobs. Each job will take a different 6 hour window (4 windows each day). There can be > 100 jobs for each month, for example. 

 

When I use a for loop to run these jobs (through API calls to ML/predictive intelligence), all of the jobs are getting submitted and run AT THE SAME TIME. This is creating an issue as some jobs are failing with error messages related to Issues Network Connectivity. 

What I want to do is to:

1. Run each job SEQUENTIALLY (one job finishes after another one starts), OR

2. Wait for say 15 mins before submission of any two jobs. 

 

Are there approaches to do any of the above?

 

Thank you.

4 REPLIES 4

Aman Kumar S
Kilo Patron

Hey @Debraj G. 

I think what you might try is to call script from your first script include, then you can decide the sequence in which the jobs would run.

So, suppose you have 3 jobs, you can decide the JOB1 to run periodically(daily, weekly, etc). And you can have JOB2 and JOB3 as On Demand.

Now when your JOB1 runs you can have at the end script to explicitly run the JOB2 and similarly at the end of JOB2 to call JOB3.

Below lines of code gives you insight how to do that:

//Execute a scheduled script job
var rec = new GlideRecord('sysauto_script');
rec.get('name', 'YOUR_JOB_NAME_HERE');
if (typeof SncTriggerSynchronizer != 'undefined')
   SncTriggerSynchronizer.executeNow(rec);
else
   Packages.com.snc.automation.TriggerSynchronizer.executeNow(rec);

Ref article:

execute-scheduled-jobs-script

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

suvro
Mega Sage
Mega Sage

You can explore gs.sleep()

Debraj G_
ServiceNow Employee
ServiceNow Employee

What is the behavior of gs.sleep(millisecond)?

I don't find much documentation of its behavior. Does it "wait" or "hold" execution of subsequent jobs in the same script (say additional for loops) for a fixed number of milliseconds?

What happens to the remaining jobs running on the instance. If I put gs.sleep(ms) in my code, do the other processes keep running without disruption?

Thanks.

It waits for that many millisecond before executing the next line of the script