Batch Processes 1 or more headless clients

Hi,

Can anyone clarify. If i run a batch process on servoy 6.0.9 and i have 5 scheduler cron jobs within the script.

Does each cron job create a separate headless client for each call? or is it one headless client running all scripts.

i.e if a different script is run at 1,2,3,4,5 minutes, all script only taking 4 minutes each to run, is that 5 headless clients, meaning one script will never bump into another, or is it one client, which will then explode?

Thanks

David

Hi David

david.pearce:
Does each cron job create a separate headless client for each call? or is it one headless client running all scripts.

It’s the latter.
As you create them from 1 client and Servoy is only single threaded, this will get you into trouble.

2 solutions:

  1. let the first job create a job for the 2nd, which will create a job for the 3rd, and so on.
    Also take into account the time that the first job started, so you can make the last job decide whether it should schedule a new job directly or schedule it x minutes later.
  2. let the batch processor spawn a separate HC for each job, via a callback method you can determine whether the ‘previous’ executed job has already ended or not.

Hope this helps.

Thanks that explains a lot.

I guess we could also pass a parameter to the solution and run a different process and then have multiple batch scripts running by adding them to the server.

david.pearce:
I guess we could also pass a parameter to the solution and run a different process and then have multiple batch scripts running by adding them to the server.

Yes, but be careful you don’t queue tasks again, as you were just trying to avoid this ;-)

Does anyone have some sample code?

I removed the job in on open then created the crib job

In my script I delete the job

Run the script and

Finally {

AddJob to the scheduler for 3000000 ms no repeats

The problem is this just keeps firing despite the start time being correct in 5 minutes. It just fires immediately

I tried a corn job and this didn’t fire at all…?

It’s like the add job runs immediately whatever I do

Hi David,

can you post the code you are using right now?
I don’t know which type of scheduling you are using right now: cron or just a scheduled task.
the latter should be enough to get done what you want here.

In the on open:

if (_args == 'EMAIL') {
			plugins.scheduler.removeJob('EPIBATCH_5MINUTELY')
			plugins.scheduler.addCronJob('EPIBATCH_5MINUTELY', '0 4/5 3-23 * * ?', globals.batch_fiveminutely)

		}

And then in the main script

function batch_fiveminutely() {

	try {
		plugins.scheduler.removeJob('EPIBATCH_5MINUTELY')
		globals.send_batchmail();
		globals.get_batch_email();
		if (globals.epicountry == 1) {
			globals.sms_pickup();
			globals.barkermedical_newcase();
		}
	} catch (e) {

		plugins.mail.sendMail('d.pearce@me.com', 'no-reply@episource.co.uk', 'Email Error', 'Email Script Error\n' + e)

	} finally {
		
		plugins.mail.sendMail('xxx@xxx,com', 'no-reply@xxx.com', 'Email', 'Email Script')
		
		var startDate = new Date();
		startDate.setTime(startDate.getTime() + 30000);

		plugins.scheduler.addJob('EPIBATCH_5MINUTELY', startDate, globals.batch_fiveminutely)
	}

}

What is strange is the I have tried everything, including just the same cron job as in on open, sleep for 30000 ms. This latest one appears to put 2 minutes between the emails, but i think its just I am now in working hours so the script is actually taking time to run. I still think the addJob is firing straight away.

Very bizarre.

Ok,

problem is probably this piece

var startDate = new Date();
      startDate.setTime(startDate.getTime() + 30000);

The startDate variable ends up being a integer instead of date.
Just enclose this like ‘startDate = new Date(startDate)’ and you should be good.

You also just need to add a job (no cronjob) on startup of your batch solution.

Have a look at my code below of how I handled the interval of 5 minutes.
Take into account: ‘_dNextRun’ is a global variable
This way you will schedule the job exactly every 5 minutes, unless the previous job took longer than 5 minutes to run.

function addSchedulerJob() {
	try {
		var _aJobs = plugins.scheduler.getCurrentJobNames();
		if(_aJobs instanceof Array) {
			for (var i = 0; i < _aJobs.length; i++) {
				plugins.scheduler.removeJob(_aJobs[i]);
			}
		}
		
		var _dCurrent = new Date();
		if(_dCurrent > _dNextRun) {
			_dNextRun = new Date();
		}
		
		plugins.scheduler.addJob('runBatch',_dNextRun,runBatch,null);
//		application.output('job added: ' + utils.dateFormat(_dNextRun,'dd-MM-yyyy HH:mm:ss'), LOGGINGLEVEL.INFO);
		_dNextRun = new Date(_dNextRun.valueOf() + (5 * 60 * 1000));
	} catch (e) {
		application.output('Error adding job:\n' + e.name + '\n' + e.message,LOGGINGLEVEL.ERROR)
	}
}

Hope this helps

Thanks Marc. ill try that

i love it when the servoy demo code doesnt work! thats was there line