# Batch Processes 1 or more headless clients

**URL:** <https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665>\
**Category:** Classic Servoy\
**Created:** [December 3, 2013, 1:23pm UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665 "2013-12-03T13:23:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![david.pearce](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@david.pearce](https://forum.servoy.com/u/david.pearce)\
**Post date:** [December 3, 2013, 1:23pm UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/1 "2013-12-03T13:23:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mboegem](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/mboegem/32/4399_2.png) [@mboegem](https://forum.servoy.com/u/mboegem)\
**Post date:** [December 3, 2013, 2:43pm UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/2 "2013-12-03T14:43:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![david.pearce](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@david.pearce](https://forum.servoy.com/u/david.pearce)\
**Post date:** [December 3, 2013, 3:00pm UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/3 "2013-12-03T15:00:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mboegem](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/mboegem/32/4399_2.png) [@mboegem](https://forum.servoy.com/u/mboegem)\
**Post date:** [December 4, 2013, 9:39am UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/4 "2013-12-04T09:39:07Z")

</div>

> 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 ![;-)]( "Wink")

---

<div class="post-metadata">

**Author:** ![david.pearce](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@david.pearce](https://forum.servoy.com/u/david.pearce)\
**Post date:** [December 5, 2013, 12:13am UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/5 "2013-12-05T00:13:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mboegem](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/mboegem/32/4399_2.png) [@mboegem](https://forum.servoy.com/u/mboegem)\
**Post date:** [December 5, 2013, 12:40am UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/6 "2013-12-05T00:40:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![david.pearce](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@david.pearce](https://forum.servoy.com/u/david.pearce)\
**Post date:** [December 5, 2013, 9:22am UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/7 "2013-12-05T09:22:45Z")

</div>

In the on open:

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mboegem](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/mboegem/32/4399_2.png) [@mboegem](https://forum.servoy.com/u/mboegem)\
**Post date:** [December 5, 2013, 11:23am UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/8 "2013-12-05T11:23:03Z")

</div>

Ok,

problem is probably this piece

```auto
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.

```auto
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

---

<div class="post-metadata">

**Author:** ![david.pearce](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@david.pearce](https://forum.servoy.com/u/david.pearce)\
**Post date:** [December 5, 2013, 11:34am UTC](https://forum.servoy.com/t/batch-processes-1-or-more-headless-clients/17665/9 "2013-12-05T11:34:17Z")

</div>

Thanks Marc. ill try that

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