Part of the EllisLab Network
   
1 of 3
1
Cron Library
Posted: 10 March 2009 01:20 AM   [ Ignore ]  
Summer Student
Total Posts:  21
Joined  05-02-2008

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(10unsigned NOT NULL AUTO_INCREMENT,
  
description varchar(100NOT NULL,
  
expression varchar(100NOT NULL,
  
action varchar(100NOT NULL,
  
last_run int(10unsigned DEFAULT 0 NOT NULL,
  
next_run int(10unsigned 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-// 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 */// 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!

File Attachments
cron0.4.zip  (File Size: 3KB - Downloads: 266)
Profile
 
 
Posted: 10 March 2009 11:27 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1100
Joined  08-06-2006

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()?

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

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 10 March 2009 11:42 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  21
Joined  05-02-2008

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.

Profile
 
 
Posted: 10 March 2009 09:08 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1100
Joined  08-06-2006

ok, post some of the edge cases then and i’ll see if i can help.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 11 March 2009 12:10 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  21
Joined  05-02-2008

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!

Profile
 
 
Posted: 11 March 2009 04:37 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  21
Joined  05-02-2008

Version 0.3 - Version 0.4

More feature complete and in a usable state.

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

Examples

// 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
- Rewrite ‘last_scheduled_run’ and remove CronParser
- Allowable years

Profile
 
 
Posted: 12 March 2009 08:39 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  549
Joined  07-28-2008

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.

 Signature 

~ 4 All the Right Reasons ~

Profile
 
 
Posted: 12 March 2009 09:42 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  12
Joined  01-23-2009

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.

Profile
 
 
Posted: 12 March 2009 10:45 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  21
Joined  05-02-2008

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

$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.

$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.

Profile
 
 
Posted: 12 March 2009 10:48 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  549
Joined  07-28-2008

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.

 Signature 

~ 4 All the Right Reasons ~

Profile
 
 
Posted: 12 March 2009 11:03 AM   [ Ignore ]   [ # 10 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1100
Joined  08-06-2006

@drewbee…

that’s what robots are for:

$this->load->library('user_agent');
if (
$this->agent->is_robot()) 
echo 
'would you please run this cron job for me, thanks, it will only take a minute.'
 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
   
1 of 3
1