Page 1 of 1

Pausing PHP while Crons run

Posted: Wed Jul 26, 2006 6:11 am
by antz
I've never used crons before, but I have a rather intensive online game job on my plate. I will need to run a good handful of processes each hour, which may take up to 5 minutes at 100% CPU. I am afraid that if users submit an action before the cron has finished, it will interfere with the calculations.

Eg, if member1 has sold goods to member2 before tax is calculated for member2, but after tax has been charged from member1. Tax is charged twice in this case (which doesn't bother me as the admin, but I am afraid of user's finding this sort of loophole).

I plan to write a class that instantiates and calls other classes in order, and the control class will be run every hour, by cron.

* Will PHP queue the initiation of classes after the moment control::execute() starts? (I guess I am asking if PHP can only answer 1 request at a time)

* If not, can anyone direct me to a class or methodology that can handle this for me?

Cheers.

If you are interested in Pure CSS and custom borders without Tables, see http://www.freewebs.com/good-code

Posted: Wed Jul 26, 2006 6:23 am
by Chris Corbyn
You can get PHP to seem to hang by using a MySQL table with row-level locking and runa query against it whilst locked. Release the lock at the end of the cron job. Sounds alwfully hackish though. Why not "nice" the process to lower it's resource usage?

Posted: Wed Jul 26, 2006 6:24 am
by AKA Panama Jack
Why not just do something SIMPLE. :)

If the cron being executed is executing a PHP program on the game site the easy thing to do is...

1. Write a dummy file somewhere in the website when the cron starts executing and then delete it when finished.

or

2. If you are using a database to store configuration variables have a variable called cron_executing. Set it to 1 when the cron starts and the set it to 0 when finished.

Now have an include file called cron_check.php or include the code in a file that is ALWAYS loaded by every page. This code checks to see if the cron_executing config variable is set to 1 or checks to see if the dummy file is present. If either is true then the player is redirected to a page that says the game is executing the hourly cron and they should wait and try again later.

No need to do anything nasty like stopping PHP.

Posted: Wed Jul 26, 2006 6:29 am
by Chris Corbyn

Posted: Wed Jul 26, 2006 6:31 am
by Jenk
redirect all traffic to a "maintenance" page whilst the cron runs, or kill the webserver for the duration of the housekeeping jobs would be a much more simple process.

Put a notification out on the homepage or somewhere telling users the game will be briefly unavailable at 'x' time everyday whilst housekeeping takes place.

Posted: Wed Jul 26, 2006 6:35 am
by Chris Corbyn
Hmm... whoops I didn't read your post correctly sorry. I thought the issue was the resource usage. My bad.

Posted: Wed Jul 26, 2006 6:59 am
by antz
Heck, what a turnout! Thanks for everyone's help!

I spose I will just have to use the maintenance page then. I had thought of that initially, but wondered if there was another way to do it. Players will have to accept that daily maintenance will occur.

Make sure you look at http://freewebs.com/good-code if you are into CSS.

Posted: Wed Jul 26, 2006 7:21 am
by Chris Corbyn
antz wrote:Heck, what a turnout! Thanks for everyone's help!

I spose I will just have to use the maintenance page then. I had thought of that initially, but wondered if there was another way to do it. Players will have to accept that daily maintenance will occur.

Make sure you look at http://freewebs.com/good-code if you are into CSS.
I think it's fair enough, especially if you list times on your maintenance page :)

<side note>
Try not to keep sticking a little ad at the bottom of your posts.... fell free to stick it in your signature though
</side note>

Posted: Wed Jul 26, 2006 7:41 am
by antz
Sorry about that. All done.

Posted: Wed Jul 26, 2006 7:59 am
by antz
See? :D

Posted: Wed Jul 26, 2006 1:31 pm
by AKA Panama Jack
d11wtq wrote:
antz wrote:Heck, what a turnout! Thanks for everyone's help!

I spose I will just have to use the maintenance page then. I had thought of that initially, but wondered if there was another way to do it. Players will have to accept that daily maintenance will occur.

Make sure you look at http://freewebs.com/good-code if you are into CSS.
I think it's fair enough, especially if you list times on your maintenance page :)
Actually, he might think about putting a javascript countdown timer somewhere on each page of the game like we do with AATraders. That way every player knows just how far away it is until the next game update.

Posted: Wed Jul 26, 2006 6:37 pm
by antz
Nice idea, wouldn't take much to do, and players would appreciate it. I'll do that too then. Cheers.