Wednesday, July 13, 2011

Servoy TIP: Creating a Timed Recurring Script

Have you ever wanted to create a method that would run at a certain interval within your solution? Maybe you're creating a messaging application that checks for messages every 30 seconds; or maybe you want to automatically generate PDF reports and email them every night...

The good news is - it's simple with Servoy!

First, create (and test) the method you want executed. Then you're going to write a little code and put it in your solution's startup method. You can run a method "Every Wednesday and Thursday at 8:00am and 1:00pm", "Every 2 hours", etc. but start easy and say you want to run a method every 30 seconds. Let's start with the code, and then I'll dissect it and tell you what's happening:

//methodname: 30SecondBatch;
// your code that does stuff goes here
//put a message in the console - so you know it's running
application.output('Executing method "30SecondBatch"');
var startDate = new Date();
//adjust the 30000 below (it's in milliseconds) if you need a different interval
var endDate = new Date(startDate.getTime()+10000000);

Done! This method will run every 30 seconds. OK - the code is pretty simple, but the last couple of line can be confusing - so here's what's going on:

var startDate = new Date(); 
//adjust the 30000 below (it's in milliseconds) if you need a different intervalstartDate.setTime(startDate.getTime()+30000);
Make a new variable with the current date, and then in the last line we set the time component of the date (all JavaScript dates contain both a date and time element) to the current time plus 30,000 milliseconds (or 30 seconds).

The last step is the real workhorse of the code - it uses the scheduler plug-in to create a CRON job. The syntax for the function is:
plugins.scheduler.addJob(jobname, Date startDate, Function method, [number repeatInterval(ms)], [number repeatCount], [Date endDate], [Object[] arguments])
So our code will run with the name "30SecondBatch", starting in 30 seconds, will execute the global method called "30SecondBatch" (call itself again), we don't need a repeatCount because the method calls itself, and the endDate is 30 seconds in the future (so we don't get multiple copies of the same method running at once).

If you wanted to make a complicated timing - you would use a 2nd scheduler plug-in function called "addCronJob()" that has different parameters:
plugins.scheduler.addCronJob(jobname, cronTimings, Function method, [Date startDate], [Date endDate], [Object[] arguments])
This is slightly different because rather than specifying an endDate and interval - you can specify a string to represent how often you want the method to run. If you wanted to run your method at 2:10pm and at 2:44pm every Wednesday in the month of March you would change the last line of the method above to:
plugins.scheduler.addCronJob('30SecondBatch', '0 10,44 14 ? 3 WED', globals.30SecondBatch, startDate)
There's no need to call this method again - because you've set up the method to run at specific times - this is just an example of the syntax. Here's guide to help you understand what the settings are for the CRON timing string:

Field NameMandatoryAllowed ValuesAllowed Special Characters
SecondsYES0-59, - * /
MinutesYES0-59, - * /
HoursYES0-23, - * /
Day of monthYES1-31, - * ? / L W
MonthYES1-12 or JAN-DEC, - * /
Day of weekYES1-7 or SUN-SAT, - * ? / L #
YearNOempty, 1970-2099, - * /

Here's some examples of what you can do (if you want more - check out the Quartz Scheduler page):

0 15 10 ? * *Trigger at 10:15am every day
0 15 10 * * ? 2011Trigger at 10:15am every day during the year 2011
0 * 14 * * ?Trigger every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ?Trigger every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ?Trigger every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ?Trigger every minute starting at 2pm and ending at 2:05pm, every day
0 15 10 ? * MON-FRITrigger at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ?Trigger at 10:15am on the 15th day of every month
0 15 10 L * ?Trigger at 10:15am on the last day of every month
0 15 10 L-2 * ?Trigger at 10:15am on the 2nd-to-last last day of every month
0 15 10 ? * 6LTrigger at 10:15am on the last Friday of every month
0 15 10 ? * 6L 20011-2015Trigger at 10:15am on every last friday of every month during the years 2011-2015
0 15 10 ? * 6#3Trigger at 10:15am on the third Friday of every month
0 0 12 1/5 * ?Trigger at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ?Trigger every November 11th at 11:11am.

No comments:

Post a Comment