How to set delay in scheduled job for every run?

rkreddy
Giga Expert

Hi,

I am using a scheduled job in which I am getting the records from a table and performing action on every record in while loop. Now I want to set some delay for every iteration in while loop, how to achieve it?

var csa = new GlideRecord('cmdb_ci_cloud_service_account');
csa.addEncodedQuery('datacenter_type!=null');
csa.query();
while(csa.next()){
var scheduleConfig = new global.CloudDiscoveryScheduleConfig();
var accountSysId = csa.sys_id + '';
var result = {};
try {
	result = scheduleConfig.discoverDatacenters(accountSysId);
} catch(err) {
	result.error = err;
}
	
}

Thanks

1 ACCEPTED SOLUTION

we do nothing in that while as it will just iterate for 10 seconds

That's how the delay would be generated

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Sravani44
Tera Contributor

@rkreddy 

The easiest solution is to try gs.sleep(milliseconds); 

 

Put the below function in a script include if you want to reuse it too often and you can call it wherever required with the required delay in milliseconds.

sleep: function(duration) {
        gs.sleep(duration);
    },

 

Mark my answer as correct if this helps you in any way

Hi @Sravani ,

I have gone through few community blogs before posting the question and I found that gs.sleep() is not good to use in few of them. Is it okay to use?

If it is okay to use, can I give that directly at the end of my script without defining that in any of the script includes?

var csa = new GlideRecord('cmdb_ci_cloud_service_account');
csa.addEncodedQuery('datacenter_type!=null');
csa.query();
while(csa.next()){
var scheduleConfig = new global.CloudDiscoveryScheduleConfig();
var accountSysId = csa.sys_id + '';
var result = {};
try {
	result = scheduleConfig.discoverDatacenters(accountSysId);
} catch(err) {
	result.error = err;
}
gs.sleep(5000);	
}

Hi @rkreddy ,

 

Since you are using this in a scheduled job and at the end of the script, instead you can run the scheduled job periodically according to the time required as below

 

find_real_file.png

Hi @Sravani ,

Thank you. I need to run this once in a day. As we don't know the number of records we get as per the query condition we have given at the initial part of the script, we can't set it to run periodically, because it may run again for the same record on the same day right?