Welcome Guest, Not a member yet? Register   Sign In
Cron Library
#1

[eluser]Phil_B[/eluser]
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.
Code:
$config['cron_table_name'] = 'cron';
Code:
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.
Code:
// 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]

[strike]Calculating the 'last_scheduled_run' is currently done using the CronParser from PHPClasses.[/strike]

Once the scheduling is complete it could be quite useful. Suggestions welcome!
#2

[eluser]sophistry[/eluser]
i like it. thanks, phil.

it would be nice to be able to use this outside of ME.

the run_jobs() function would need a change to get it to run a standard function or a normal class method. OTTOH, maybe option to use call_user_func_array()?

Code:
modules::run($job['action']); // Run job (Modular Extensions 5.1)
#3

[eluser]Phil_B[/eluser]
Yes I planned on using some sort of callback later on. My main concern right now is the scheduling. I'm trying to find some creative way of looping through the days. Right now it only handles ideal cases.
#4

[eluser]sophistry[/eluser]
ok, post some of the edge cases then and i'll see if i can help.
#5

[eluser]Phil_B[/eluser]
Progress!

The next_scheduled_run seems to be working well. Hopefully I can get a few people to help test it out.

Thanks for the interest Sophistry!
#6

[eluser]Phil_B[/eluser]
[strike]Version 0.3[/strike] - Version 0.4

More feature complete and in a usable state.

Now supports keywords (and abbreviations), Sunday-Saturday, January-December.

Examples
Code:
// Everyday at 8:00 AM, Monday to Friday, during January, May, June, July, August and November
0 8 * january,may-aug,11 1-5

// Returns Sunday, February 29th, 2032 at 7:35 PM
35 19 29 feb Sunday

The last example took 29 loops and ~0.031 seconds to calculate.

To do:
- Special characters
- Optionally load jobs from config instead of database
- Better job execution
- [strike]Rewrite 'last_scheduled_run' and remove CronParser[/strike]
- Allowable years
#7

[eluser]drewbee[/eluser]
How are the scheduled jobs being executed? Do you actually need to set cron to run this every so often so that it can decide what to do next?

One thing that you may want to take a look at (if you are not doing it this way) is to run the scheduled programs via exec() so that it runs in the background and this cron script's only purpose is to actually worry about scheduling and execution. That way if multiple processes are expected to run within the same time frame, it isn't waiting on one process to finish before the next can start, but rather can start simultaneously.
#8

[eluser]BoogieK[/eluser]
How can be used this library? I mean a usage example code...

Let`s say I have three crons on my website and the crons are in /var/www/mysite/crons/cron1.php cron2.php and I want to run them at the beginning of the day. How do I have to write the line code?

I found a usage example in CronParser.php but I can not make it running.
#9

[eluser]Phil_B[/eluser]
In my project I set a linux cron to check every 30 mins. Typically pseudo-crons are checked whenever a visitor visits the site. The more activity a site receives the more accurate the crons are.

At the moment it still uses Modular Extensions for executing tasks.

Usage:

1. Create the table and add the config entry as described in the first post.

2. Add a job to the database
Code:
$this->cron->save_job(0, 'Email news letter everyday at 5am', '0 5 * * mon-fri', 'news_letter/send_all');

3. From anywhere in your application you can load the library and run any jobs that are scheduled to run.
Code:
$this->load->library('cron');

$this->cron->load_jobs();
$this->cron->run_jobs();

Ideally this would be on a page with heavy traffic. I've been considering an autorun feature which would let you simply autoload the library and have it run on every page load.
#10

[eluser]drewbee[/eluser]
If this is to be run on every page, I would definitely highly recommend PHP's exec function if you are not already.

While it would be great and have no issues with small processes, large processes would delay page loads for users who happened to hit that particular job.




Theme © iAndrew 2016 - Forum software by © MyBB