Hi everybody,
I have 2 jobs in my scheduler. One works fine but the second one is run each time I reset the scheduler. So when I restart the server it runs automatically this job, even if the cronJob says that it should be run at a specific time.
Here is how I setup my jobs:
function SchedulerSetup()
{
forms.frm_scheduler.controller.loadAllRecords();
// loop through the foundset and push all record information to the cronjobs in the schedule plugin
// cronjobs always run in memory in the batchprocessor (client side)
forms.frm_scheduler.resetScheduler();
// refresh the cronjob memory after 20 and 50 seconds after the minute by running this startup script again
// This is done to push changes in the SCD module records to the scheduler plugin again.
//plugins.scheduler.addCronJob("reset", "55 * * * * ?", globals.SchedulerSetup); //if I enable that my job is run every 55 sec!
}
function resetScheduler()
{
//remove all jobs
var vJobsArray = plugins.scheduler.getCurrentJobNames()
for(var i=0 ; i< vJobsArray.length ; i++)
{
plugins.scheduler.removeJob(vJobsArray[i])
}
//loop through jobRecords and push data to scheduler Plugin
for(var i=1 ; i<=foundset.getSize() ; i++)
{
foundset.setSelectedIndex(i)
if(fld_job_status==1)
{
var v_success = startJobInScheduler();
}
}
}
function startJobInScheduler()
{
var args = null;
if (fld_method_arguments != null)
args = plugins.serialize.fromJSON(fld_method_arguments);
var jobCreated = plugins.scheduler.addCronJob(fld_job_name, fld_cron_timings, globals[fld_methodname],fld_start_date,fld_end_date,args)
if(jobCreated)
{
return true
}else{
return false
}
}
I dont know if it’s linked, but the job running on reset has an argument while the other one not.
Here is the data in the jobs table (2nd is the job causing problems): [attachment=0]Capture2.PNG[/attachment]

at what point is it really executing?
At the moment you call removeJob?
or at the moment you add one?
First a little rectification. It actually runs both jobs! (There was a mistake in the code of the 1st job which made it fail)
If I commented out the line running startJobInScheduler(), nothing happens. No job run. So it looks like it’s when I add the jobs that they are run.
Here what happens in the Profiler when I start the solution(SCD_PO_Overdue and SCD_Reports are the 2 jobs methods):[attachment=0]Capture2.PNG[/attachment]
but which part of startJobInScheduler does it?
for example is it the:
var vJobsArray = plugins.scheduler.getCurrentJobNames()
for(var i=0 ; i< vJobsArray.length ; i++)
{
plugins.scheduler.removeJob(vJobsArray*)*
}
disable that?
Or do you really see that at the moment you add it to the scheduler it also executes straight away? And not at the time you think because of your cron timings?
The code you mention is not in startJobInScheduler().
Have a look at the code in my first post.
I have commented, in function resetScheduler(), the line:```
var v_success = startJobInScheduler();
Which is like commenting, in function startJobInScheduler(), the line ```
var jobCreated = plugins.scheduler.addCronJob(fld_job_name, fld_cron_timings, globals[fld_methodname],fld_start_date,fld_end_date,args)
``` because the rest of the code has no link with the plugin or the jobs
In resetScheduler() nothing else has been commented. The line ```
plugins.scheduler.removeJob(vJobsArray*)*
*``` is still run.*
*So in my opinion, it's plugins.scheduler.addCronJob(...) which triggers the run of the jobs.*
Foobrother:
So in my opinion, it’s plugins.scheduler.addCronJob(…) which triggers the run of the jobs.
in the end that statement is ofcourse true…
when you add a cron job that will be scheduled for execution. (so it will run jobs)
If it executes quicker then you think i think there is something wrong with the cron timings
Which job is executed when you add a cron job? The previous one?
Also it is impossible that at the exact same time another job is executed
Because a scheduler job will wait for your current method stack (reset->start and so on) to be finished.
you can see that a bit because the 2 SCD_xx functions are not started from or inside the startJobInScheduler method call
They are done after that. But it seems that they are triggered someho in initApp (when you call alo call SchedulerSetup)
The first job is setup with “0 30 05 * * ?” which is 05H30AM daily.
The second job is setup with “0 58 10 * * ?” which is 10H58AM daily.
When I start the solution now from developer I call globals.SchedulerSetup(). It removes the jobs from the scheduler (there should be nothing actually because I just built the solution). Then it finds each jobs in the database (2 in total) and adds them to the scheduler using the method “plugins.scheduler.addCronJob(…)”. Once added, the method of the 1st job should be triggered at 05H30AM tomorrow for the first time. And the 2nd job method should be triggered tomorrow at 10H58AM for the first time. No?
Or, seeing that the hour is passed, does the scheduler runs directly the method for the current day and then waits until tomorrow? I’ll try to put 20H00PM on both jobs to see what happens ![Rolling Eyes :roll:]()
EDIT: after test, no it is still running the jobs automatically at the start even if the hour in the cronJob is in the future.
please make a case where you see this behavior.
I cant directly see what is happening at your place.
jcompagner:
please make a case where you see this behavior.
What do you mean? Do you want a sample solution?
Or do you just want me to open a case explaining what happens?
My code is based on the one of this presentation: http://www.servoy.com/servoyworld/sw_do … essors.pdf
I have uploaded a sample solution in the case.
I have discovered that the problem is due to the start and End job dates arguments in add addCronJob().
With no date (null), it works ok. With dates it does what I have explained previously.
Also, I have discovered with this sample solution that, with no date when the job starts (at the cronjob time) it loops and runs the job several time?! However it doesn’t do it with my current solution.
Forget the problem of loop!
I did a stupid mistake in the conjob setting. I put a * for the sec instead of 0 ![Embarassed :oops:]()
ok this is how Quartz (our scheduler) default works
If you add start date like 1-1-2010
then it starts calculation from that date so the first time it should do its thing is then 1-1-2010 05:00 (if you have a cron setting of everyday at 5)
But that will result in a misfire because that date is already passed, then the setting that Quartz has is that it will fire once …
So dont specify a date if you just want to do it from now on. Or if you really want to start in the future then specify a date.
Ok fair enough.
We don’t really need to be able to specify a start date anyway. It was more a “bonus feature”.
By the way, this is raising another issue. If you put null for the start date and a future date for the End Date, it works fine until you pass the end date, Then when you will restart the scheduler you will get the following error “Wrapped java.lang.IllegalArgumentException: End time cannot be before start time”.
But it’s not a big issue as you don’t want the job to start anyway ![Laughing :lol:]()
how do you restart the scheduler?
jcompagner:
how do you restart the scheduler?
I should have said “reset” instead of “restart”. In our case we have a batch processor which resets the Scheduler regularly in order to pick up the new job created by our solution users.
Using that line ```
plugins.scheduler.addCronJob(“reset”, “55 * * * * ?”, globals.SchedulerSetup)
See my first post code for details.
But you can also reset/restart your scheduler when restarting Servoy server (usually after updates) and having a batch processor loading the scheduler.