Top Poster
#1 ramon fincken 416
#2 Site admin 3
#3 Ber|Art 2
#4 braddmark 1
#5 accentmedia 1
#6 Xarzu 1
#7 khadish 1
#8 moxxx 1
#9 nistelrock 1
#10 Justin-ServerPilot 1
[snipplet] Pseudo cronjob



Rating : 3 / 5

 
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding
Beta
Want to be notified by email when this topic gets a reply?  
View previous topic :: View next topic  
Author Message
ramon fincken
Site's programmer

Get a free globally recognized avatar
It's free!


Joined: 03 Aug 2007
Posts: 416
Location: A'dam/Diemen, The Netherlands

PostPosted: Sun Nov 23, 2008 11:53 am    Post subject: [snipplet] Pseudo cronjob Reply with quote

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


Design pattern:
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)
Create table

Code:
CREATE TABLE `mycron` (
  `id` int(1) NOT NULL,
  `next_job_time` int(12) NOT NULL,
  PRIMARY KEY  (`id`)
);

INSERT INTO mycron VALUES(1,0);


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)
Code

Code:
# [ 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


3)
Things to modify:

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.


Read on / source:
http://www.mail2forum.com/forums/viewtopic.php?t=1144
http://www.phpbbinstallers.net/board/vi....php?t=506
Back to top
View user's profile Send private message Visit poster's website
Google adsense
Advertisement





PostPosted: Sun Nov 23, 2008 11:53 am    Post subject: [snipplet] Pseudo cronjob

Advertisement
Back to top
GravityForms
Advertisement





PostPosted: Sun Nov 23, 2008 11:53 am    Post subject: [snipplet] Pseudo cronjob

Advertisement
Gravity Forms Plugin for WordPress
Back to top
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
General forums
The world of freelance
Programming
Other projects
Poll
I love gifts

Yes goodies and presents! [3]
No but I love polls ! [1]

Related google ads