I’m trying to make a cronjob, but i don’t know if I do something wrong or not understand exactly the correct method to do this. I have a solution called batch_processor, and inside this I have two methods in the globals of the solution:
The first execute the scheduler method
function mybatchprocessor() {
//methodname: mybatchprocessor
//this is where the main code of your method would go.
application.output('running some code');
var startDate = new Date();
startDate.setTime(startDate.getTime() + 2000);
//adjust the 20000 above if you need a different interval, it's in milliseconds
var endDate = new Date(startDate.getTime() + 10000000);
plugins.scheduler.addJob('in20seconds', startDate, addNewRecord, 0, 0, endDate)
}
And the second have the code to execute
function addNewRecord() {
/**@type {JSFoundset<db:/bamboo/bamboo>}*/
var fs = databaseManager.getFoundSet("bamboo", "bamboo");
fs.newRecord();
var newRecord = fs.getSelectedRecord();
newRecord.id_c = "TEST";
newRecord.msi_sales_agent_c = "TEST";
newRecord.status_c = 0;
databaseManager.saveData(newRecord);
}
and, later i’m putting in the servoy server, but not execute anything
I search arround the internet and inside of the forum, but don’t see any obiosly form to do that, anyone can help me?
You should schedule the jobs like this:
function onOpenBatchprocessor() {
// application.output("OPENING BatchProcessor", LOGGINGLEVEL.INFO)
// see: http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html for more info
plugins.scheduler.addCronJob('5mins','0 0/5 * * * ?',globals.readPop3Inboxes)
//run Reports every night at 4
plugins.scheduler.addCronJob('ERReports','0 0 4 * * ?',globals.multiDBEReports)
}
Like you can see I’ve got 2 cronjobs, first one will run every 5 minutes to read mailboxes, the second one runs every night at 4:00 to run some reports.
For more information on the second parameter look at this url: http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html
And in what part of the module i have to put the onOpenBatchprocessor function in the globals for example?
that is a global function. You have to set this as the onOpen of the batchprocessor solution. So as soon as your batch processor starts up, it will create the cronjobs you want.
Okey, i put in the onOpen the onOpenBatchprocessor function, and the cronjob call the function addNewRecord like this:
function onOpenBatchprocessor() {
// application.output("OPENING BatchProcessor", LOGGINGLEVEL.INFO)
// see: http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html for more info
plugins.scheduler.addCronJob('Test', '5 * * * * ?', addNewRecord)
}
But not created any record in the database, anything to to in the server side, anything wrong. Thanks for all, i’m a little lost with this theme
Did you wait long enough? Your schedule is set to trigger 5 seconds past every minute.
If you want to trigger it every 5 seconds it would look like this:
plugins.scheduler.addCronJob('Test', '0/5 * * * * ?', addNewRecord)
I change the line, but i can’t see any change in the database, i put you the sample code here, in one .servoy and if you can tell me what is wrong. Thanks for all dude
batch_processor.servoy (3.34 KB)
I don’t have a bamboo server, so I modified your sample to work with the example_data database and the orders table.
When you run this solution you should get new orders with a orderdate and a shipname.
batch_processor.servoy (3.53 KB)
Okey thanks for all, seems than i not fill all the field are required, but your solution with example_data are so usefull. Now i put in the server and auto-update correctly without any problem.
And finally question, all the batch process is needed to be in a separate solution, no? and if posible to aggroup all the batch processors in unic solution or is better to have 1 solution to batch process?
In my batchprocessor there are currently 2 different jobs. I don’t know if there are any limitations on how many jobs you can have.
You can have as many jobs as you like. I had one with 10 jobs.
But I have changed strategy:
I have a while loop running forever, I use the sleep function to make sure it starts from the top once every minute.
It checks what tasks need to run, runs any tasks due, then if any time left over in that minute it sleeps for the rest of the minute.
I record the start time, finish time and status of each task in a database table so I can inspect from a Servoy client or pgAdmin3 what is going on. You can use this strategy with Cron jobs as well.
It is also useful to have a form and a button in your batch processor so you can test the tasks from Developer, although this will not catch all problems such as accidentally putting up dialog boxes which is not allowed in headless client.
Hope this helps,
C