Pausing PHP while Crons run

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
antz
Forum Newbie
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Pausing PHP while Crons run

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hmm... whoops I didn't read your post correctly sorry. I thought the issue was the resource usage. My bad.
antz
Forum Newbie
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>
antz
Forum Newbie
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Post by antz »

Sorry about that. All done.
antz
Forum Newbie
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Post by antz »

See? :D
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
antz
Forum Newbie
Posts: 5
Joined: Wed Jul 26, 2006 5:50 am

Post by antz »

Nice idea, wouldn't take much to do, and players would appreciate it. I'll do that too then. Cheers.
Post Reply