CRON Scheduler
Flexible, lightweight CRON compliant scheduler. Works directly from the code. Main features:
- Compatible with most of what CRON is offering
- Setting the CRON is as simple as setting one string. Example bellow.
- It also supports simple intervals in addition to CRON style schedules
- Schedule can have valid from / to limits
- OnSchedule event can be called in a new background thread or in context of main thread
- Very lightweight implementation
The demo bellow shows the basic usage of CRON Scheduler. If you have trouble using the code or have any other questions, please contact me directly using the “Contact” page.
Warning: My version of CRON implementation also uses seconds so it actually has one more format option. You must be aware of that when you write your CRON string. The format is like that:
<Second> <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>
Sample scheduler usage:
procedure TfMain.FormCreate(Sender: TObject); var NewSchedule: TScheduledEvent; begin // clear memo lbEvents.Clear; // crete new event list that will hold events EventList := TSchEventList.Create; // first event NewSchedule := EventList.Add('ScheduleOne'); NewSchedule.Schedule.EventPlan := '0 */2 * * * * *'; NewSchedule.OnScheduleEvent := OnScheduleOneTrigger; NewSchedule.Run; // second event NewSchedule := EventList.Add('ScheduleTwo'); NewSchedule.Schedule.EventPlan := '*/30 * * * * * *'; NewSchedule.OnScheduleEvent := OnScheduleTwoTrigger; NewSchedule.Run; // third event NewSchedule := EventList.Add('ScheduleThree'); NewSchedule.Schedule.EventPlan := '*/5 * * * * * *'; NewSchedule.OnScheduleEvent := OnScheduleThreeTrigger; NewSchedule.Run; // fourth event NewSchedule := EventList.Add('ScheduleFour'); NewSchedule.Schedule.EventPlan := '15 * * * * * *'; NewSchedule.OnScheduleEvent := OnScheduleFourTrigger; NewSchedule.Run; // start stop dynamic event FDynamicEvent := EventList.Add('DynamicSchedule'); FDynamicEvent.Schedule.EventPlan := '*/5 * * * * * *'; FDynamicEvent.OnScheduleEvent := OnDynamicScheduleTrigger; Caption := Format('Started at : %s ( Press ''d'' for dynamic event )', [DateTimeToStr(Now)]); end; |
Event handlers:
procedure TfMain.OnDynamicScheduleTrigger(Sender: TScheduledEvent); begin lbEvents.Items.Add(Format('Dynamic event was trigered at : %s', [DateTimeToStr(now)])); end; procedure TfMain.OnScheduleFourTrigger(Sender: TScheduledEvent); begin lbEvents.Items.Add(Format('Event four was trigered at : %s', [DateTimeToStr(now)])); end; procedure TfMain.OnScheduleOneTrigger(Sender: TScheduledEvent); begin lbEvents.Items.Add(Format('Event one was trigered at : %s', [DateTimeToStr(now)])); end; |
Example how to use From / To valid range. The event will fire for one year, every sunday, every second hour, but only on 1,5 and 10 month in the year.
NewSchedule := EventList.Add('ScheduleOne'); NewSchedule.Schedule.EventPlan := '0 0 */2 * 1,5,10 7 *'; NewSchedule.OnScheduleEvent := OnScheduleOneTrigger; NewSchedule.Schedule.ValidFrom := EncodeDate(2010, 1, 1); NewSchedule.Schedule.ValidTo := EncodeDate(2010, 12, 31); NewSchedule.Run; |
Example how to fire an event at an 2 hour interval from teh start of the schedule. Each event will be fired in a new thread.
NewSchedule := EventList.Add('ScheduleOne'); NewSchedule.Schedule.SetInterval(0, 0, 2, 0, 0, 0); NewSchedule.OnScheduleEvent := OnScheduleOneTrigger; NewSchedule.SignalType := stThreaded; NewSchedule.Run; |
Change Log
- 2.2.4
- Terminate event is now created only when schedule is run
- 2.2.3
- Improved cron format support. Now “1,3,2,5-9,15-20/2,5,1,18-25/3″ gets parsed correctly
- Sort numbers on the value list to get correct sequence
- Eliminate duplicate values
Additional format checks
- ExecuteLimit added. Allows the event to be executed only N times
- Use QueryPerformanceCounter instead of GetTickCount (thanks Roberto Neto)
- Code adaptation for compatibility with Delphi 7
- Fixed a bug with day of the week not having a correct impact on month
- Raise exception if the schedule is not in the correct format (a start)
- Made FPC compatible (initial implementation)
- Fixed a bug when deleting a schedule from the list (it was not removed). Thanks to Daniel for pointing that out.
- Fixed a bug in calculation algorithm
- 64 bit compiler compatible
- added HasValue function for TChronEntry
- fixed a bug in valid interval calculation