View Single Post
Old 02-28-2010, 07:55 PM   #5 (permalink)
Rhinos
The Wanderer
 
Join Date: Aug 2009
Posts: 17
Thanks: 0
Rhinos is on a distinguished road
Smile Alternative 5 minute cron job

if you are going to use a 5 minute cron job, make sure it is not going to start taking a long time when the amount of data you have increases.

If the 5 minute cron is updating members then you should consider an alternative way:

On your members table (or a OneToOne table for the members table; members_resets) add a new field named something like: last_5min_reset with a type of unsigned int(10).

When a member is first registered set this to the current time using the time() function.

In your header.php or the place where your code is run before the action code you will need the following code:

PHP Code:
<?php
// If the time from the last reset is greater than 5 minutes
if ((time() - $stat['last_5min_reset']) > 300)
{
    
$num_resets floor((time() - $stat['last_5min_reset']) / 300);

    for (
$i $num_resets$i 0; --$i)
    {
        
// perform member updates
        
$stat['energy'] += 0.5;
    }

    
$stat['last_5min_reset'] = time() - ((time() - $stat['last_5min_reset']) - ($num_resets 300));

    
// Make the updates, or if you do other things to the $stat variable then update it further down the code so that there is only 1 update query for the user
    
mysql_query('UPDATE members SET energy = ' (int) $stat['energy'] . ', last_5min_reset = ' . (int) $stat['last_5min_reset'] . ' WHERE id = ' $stat['id'] . ' LIMIT 1;');
}
?>
What this basically does is only perform the reset when the member logs in and whenever they make subsequent page requests whilst logged in. It is more efficient because it means there is not a script running every 5 minutes updating however many members there are.

I have found that I need to run this script before fights/battles in order to make it seem like the defending member is up to date with the resets. So I simply delegated the functionality to a function and made it possible to be used for any member and then updated the appropriate member that the function was called for.

If this isn't what you wanted I'm sure it is what someone else wanted and therefore anyone who may come across this now or in the future should find it useful.
Rhinos is offline  
Reply With Quote
The Following User Says Thank You to Rhinos For This Useful Post:
Arbaces (03-01-2010)