Cron Library
* Work in Progress - Version 0.4
—————
I’m writing a pseudo-cron library for a project I’m working on. I thought I would throw it up here to get a little help with the scheduling and in case anyone else finds it useful. Instead of checking all jobs and applying a cron expression to see if should be executed, this library will calculate when the job should run next and store the timestamp.
And that’s where this library is lacking. The ‘next_scheduled_run’ method is very basic and handles simple cron expressions without checking the last day of the month or weekday.
All jobs are stored in a table which can be set in config.
$config['cron_table_name'] = 'cron';
CREATE TABLE IF NOT EXISTS `cron` (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
description varchar(100) NOT NULL,
expression varchar(100) NOT NULL,
action varchar(100) NOT NULL,
last_run int(10) unsigned DEFAULT 0 NOT NULL,
next_run int(10) unsigned DEFAULT 0 NOT NULL,
PRIMARY KEY (id)
);
Expressions are expanded into a full array of valid values. Currently supports individual days, ranges, wildcards and wildcards with steps.
// Monday-Friday at 5:30 am
30 5 * * 1-5 // Becomes: [30],[5],[1,2,3...29,30,31],[1,2,3...10,11,12],[1,2,3,4,5]
// Midnight on the first of every 3 months
0 0 1 */3 * // Becomes: [0],[0],[1],[1,4,7,10],[0,1,2,3,4,5,6]
Calculating the ‘last_scheduled_run’ is currently done using the CronParser from PHPClasses.
Once the scheduling is complete it could be quite useful. Suggestions welcome!
