TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   scheduling the server to do updates/jobs (http://www.talkphp.com/absolute-beginners/5287-scheduling-server-do-updates-jobs.html)

Arbaces 02-25-2010 01:03 PM

scheduling the server to do updates/jobs
 
Hello,

<offtopic>This community looks great and its my first topic so I should at least say I entered here because I've recently started to become more and more into web programming and decided to create an online game. Right now I'm digging all around the web to find things to start with. I have basic PHP & MySQL knowledge (and good Java and C), but that's why I want to learn along the way as I'm making something..

So, what I'm trying to make is a game similar to Cybernations, Travian and Hattrick, with ideas and concepts from many other games but brought online, in-browser.</offtopic>


The question that bugs me lately is: how will I make the server update itself (scheduled updates and scheduled script running, say from 10 to 10 hrs or something like it).

For example, as an exercise - say I'm making a page which I want from 5 to 5 minutes to update itself with some text (automatically, by the server). for ex:

i upload the page on a free hosting service with php and mysql - like FreeHostia
now the page is blank ..
//after ~12 minutes it shows up:
5 minutes passed! update [1]
5 minutes passed!

I hope you get it, I want to know how can I configure a server / a hosting service / everything , to run scripts on a scheduled time (scripts to update a db, process some algorithms etc). I need something I can start studying on.


Thanks in advance
-Arbaces

Hightower 02-25-2010 04:14 PM

You need to look into Cron Jobs I think.....

Village Idiot 02-25-2010 04:14 PM

Quote:

Originally Posted by Arbaces (Post 30045)
Hello,

<offtopic>This community looks great and its my first topic so I should at least say I entered here because I've recently started to become more and more into web programming and decided to create an online game. Right now I'm digging all around the web to find things to start with. I have basic PHP & MySQL knowledge (and good Java and C), but that's why I want to learn along the way as I'm making something..

So, what I'm trying to make is a game similar to Cybernations, Travian and Hattrick, with ideas and concepts from many other games but brought online, in-browser.</offtopic>


The question that bugs me lately is: how will I make the server update itself (scheduled updates and scheduled script running, say from 10 to 10 hrs or something like it).

For example, as an exercise - say I'm making a page which I want from 5 to 5 minutes to update itself with some text (automatically, by the server). for ex:

i upload the page on a free hosting service with php and mysql - like FreeHostia
now the page is blank ..
//after ~12 minutes it shows up:
5 minutes passed! update [1]
5 minutes passed!

I hope you get it, I want to know how can I configure a server / a hosting service / everything , to run scripts on a scheduled time (scripts to update a db, process some algorithms etc). I need something I can start studying on.


Thanks in advance
-Arbaces

If you are on a linux server, Cron Jobs is what you are looking for. If you are on windows, Task Scheduler.

Arbaces 02-25-2010 08:14 PM

Yay! Cron Jobs got it done for me, found a nice service setcronjob.com which I can use to schedule updates on my free hosting server :) Thanks.

Rhinos 02-28-2010 07:55 PM

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.


All times are GMT. The time now is 05:02 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0