[snipplet] Pseudo cronjob

By.

min read

My profile

Share this:

This is an example how to run a light cron when your webhost does not support cronjobs.

[b:cc592ff9d9]Design pattern:[/b:cc592ff9d9]
The overall picture / design pattern is this:
* Store your last cron start time in a database table
* Let a commonly opened page ( say index.php ) check if you need to run the cron again because the time which has past is above your time threshold.
* If so, run the cron job, which could be including a file or running a function. Next you update the last cron time in the database.

Note: you could have more cronjobs running, because you have your primary key (id) to identify your cronjobs.

1)
[b:cc592ff9d9]Create table[/b:cc592ff9d9]

[code:1:cc592ff9d9]CREATE TABLE `mycron` (
`id` int(1) NOT NULL,
`next_job_time` int(12) NOT NULL,
PRIMARY KEY (`id`)
);

INSERT INTO mycron VALUES(1,0); [/code:1:cc592ff9d9]

In this table, you’ll store the time the next cron job should have run. I say “should have”, because it won’t get executed unless two conditions are met: (1) the specified time period has elapsed, and (2) someone browses to your forum home page (e.g. /phpBBroot/index.php) — hence, why this is a “pseudo” cron jobber.

The value in the “next_job_time” column will be stored as Unix Epoch time (i.e. seconds since Jan. 1, 1970).

Next, open your index.php (Note: you could also put this in other commonly accessed file):

2)
[b:cc592ff9d9]Code[/b:cc592ff9d9]

[code:1:cc592ff9d9]# [ Find ]

include($phpbb_root_path . ‘common.’.$phpEx);

# [ After, Add ]

//
// MyCron mod
//
$sql = “SELECT * FROM mycron WHERE id=1”;
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, ‘Could not query mycron table’, ”, __LINE__, __FILE__, $sql);
}
$next_job = $db->sql_fetchrow($result);
$job_time = $next_job[“next_job_time”];

$current_time = (int)Date(“U”);
if ($current_time > $job_time)
{
$job_time = $current_time + 300; // 5 minutes (i.e. 60*5)
$sql = “UPDATE mycron SET next_job_time=$job_time WHERE id=1”;

if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, ‘Could not update mycron table’, ”, __LINE__, __FILE__, $sql);
}

// Run job here….
include_once($phpbb_root_path . ‘/m2f/m2f_import_msgs.php’);
}
//
// End MyCron mod
//

# Close and Save index.php [/code:1:cc592ff9d9]

3)
[b:cc592ff9d9]Things to modify:[/b:cc592ff9d9]

1. Change the value 300 to the number of seconds you want to wait between cron jobs. Remember, however, that this is just a minimal time, as the actual time the job is run is dependant on when the index.php is actually requested. The above code just guarantees that the job is not run until AT LEAST the specified time (e.g. 300 seconds, in this case).

2. Where the mode indicates // Run job here….
You can add what you need. As you can see, I’m using the include_once() function, and am calling the m2f_import_msgs.php file. This works great for me.

[b:cc592ff9d9]Read on / source:[/b:cc592ff9d9]
http://www.mail2forum.com/forums/viewtopic.php?t=1144
http://www.phpbbinstallers.net/board/viewtopic.php?t=506

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *