SstCron is a Smalltalk equivalent of Unix cron(1). SstCron allows automatic, repetitve scheduling of events, such as on the first of the month, or every Sunday, or every 5 seconds, or every 5 seconds when the first of the month is a Sunday. It also supports scheduling events to be run at a given moment such as 3:05 p.m. on Jan 1, 2004.
A cron entry (SstCronEntry) has a block (the unit of work), and a set of scheduling time constraints on months, days, days of the week, hours, minutes, and seconds, which are all relative to the local time zone. These constraints can have three possible types of values:
An entry is considered ready-to-run when all of its constraints are met.
Entries are scheduled by adding a cron entry to the SstCron object (obtained with SstCron default) with add:, and stopped by removing the entry with remove:. Entries are automatically requeued to run at the next time satisfying its constraints. The entry is requeued only after it has finished executing. Thus an entry registered to execute every second taking takes five seconds to run will only be run every five seconds; there will not be multiple simulaneous executions.
It's easiest to show how to use SstCron with some examples:
"Primitive clock on the Transcript's title bar, updating every 5 seconds. Inspecting the entry will show %seconds was expanded to (0 5 10 15 20 25 30 35 40 45 50 55) In Smalltalk terms this is (0 to: 59 by: 5). So specifying a single number really means: (minValue to: maxValue by: number)." SstCron default add: (SstCronEntry new workBlock: [Transcript shell title: SstDate now printString]; seconds: 5; yourself)
"Run a backup every Sunday at midnight. Note that midnight (00:00:00) is regarded to be morning. Also note that it's important to specify the hours, minutes and seconds: otherwise they default to nil which means match every possible value. So without them explicitly specified you'd be running this event repeatedly all day! We don’t set the month or day cause we want them to always match." SstCron default add: (SstCronEntry new workBlock: ["perform backup"]; daysOfWeek: #(1); hours: #(0); minutes: #(0); seconds: #(0); yourself)
"Run this on the first Monday of the month. There can only be one day from the 1st to the 7th that's a Sunday." SstCron default add: (SstCronEntry new workBlock: [foo]; days: (1 to: 7); daysOfWeek: #(2); hours: #(0); minutes: #(0); seconds: #(0); yourself)
"This should be obvious" SstCron default add: (SstCronEntry at: (SstDate newYear: 2000 monthIndex: 1 day: 1 hours: 0 minutes: 0 seconds: 0 milliseconds: 0) do: [Transcript shell title: 'Happy New Year!'])