gamesmstr Posted January 11, 2010 Share Posted January 11, 2010 I am writing a browser based multiplayer RTS game. I am looking for a feasible way of scheduling the timed tasks. (like it takes 1m 30s to build something) What I figured I would do is create a mysql database with a list of commands to be executed at a specific time. PHP would have to check that file every second to execute and remove completed tasks. I've done some major hunting but I have not yet found any way to do this. Cron only goes down to minutes. I'd love some help or maybe suggestions on a better way to accomplish this. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/ Share on other sites More sharing options...
ignace Posted January 11, 2010 Share Posted January 11, 2010 When the user gives the order to start build something then store the time it will be done (now() + time to build) in the database with the structure. Use JavaScript or Flash to animate the building process while this is merely fake because even if the user now cheats he would not be able to use the building as in the back-end it is still building... This also allows you to give the user special items like "reduce built time with 20 minutes" where after you update 1 or multiple rows and deduct 20 minutes of 1 or multiple rows for which the finished build time is higher then now() Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-992876 Share on other sites More sharing options...
gamesmstr Posted January 11, 2010 Author Share Posted January 11, 2010 I don't have any problem with storing the data.... What I want is the ability to launch the code at the appropriate time without using cron. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-992944 Share on other sites More sharing options...
laffin Posted January 11, 2010 Share Posted January 11, 2010 You can't php is called thru the webserver. if u can have a background task to update your db is a better solution, as this wont execute the script at particular intervals (which takes time, to load php, load the script, parse it and execute), but requires a dedicated server, and disabling php max runtime (set_time_limit(0) Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-992991 Share on other sites More sharing options...
laffin Posted January 11, 2010 Share Posted January 11, 2010 Look into php fork but hopefully your web hosting has it enabled. The problem you will encounter is finding out if your background task is running, but I found a site that may do the trick: http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/ but instead of refreshing the page, you store it in your scheduling database. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-992997 Share on other sites More sharing options...
ignace Posted January 11, 2010 Share Posted January 11, 2010 You don't need a background task for something like that. You will eat up all resources and even crash your server if you ever get lots of simultaneous users (imagine 10k users each having 10 or more tasks running?? Like building, upgrading and researching). Just use Ajax and a field in your database table that specifies when the build will be complete. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993056 Share on other sites More sharing options...
laffin Posted January 11, 2010 Share Posted January 11, 2010 Reason I said: The problem you will encounter is finding out if your background task is running, If the Scheuleder handles all tasks, than users dont matter. as you only want 1 scheduler running. All You have to do is check if your Scheuleder is running, if not, start it up. And it wont use up all resources. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993070 Share on other sites More sharing options...
laffin Posted January 11, 2010 Share Posted January 11, 2010 Using Ajax to me in this situation is silly. Because You need a user to update the db (No Ajax, nothing gets built) but your user scheduler will also need to update all users, not the ones just logged in or the current user logged in. so shure your getting that 1 sec update, however this script is gonna be hammered. in order to update all tasks completed, and return the result to the ajax script. In short, I recommend using a LRP (Long Running Process). Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993081 Share on other sites More sharing options...
gamesmstr Posted January 11, 2010 Author Share Posted January 11, 2010 Agreed. I have most of the site ajaxed anyways, but I need the server to update while the player is offline too. I'll look into the scheduler. and see what I come up with. I use dedicated hosting so I should be able to get whatever I like. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993085 Share on other sites More sharing options...
gamesmstr Posted January 11, 2010 Author Share Posted January 11, 2010 How about this for an idea.... I make a php program that covers all the tasks to be done in one minute. and schedule that task via cron to run every minute. Anybody see any issues with something like that? Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993091 Share on other sites More sharing options...
laffin Posted January 11, 2010 Share Posted January 11, 2010 There shouldnt be an issue with cron. It runs at specified intervals. but like I said, since this is going to be an ongoing process. You may as well run it as a background task instead (continually running). if your updates takes awhile (depends on how many tasks it has to update) and you find that its taking more than a minute, you may encounter problems with overlapped background processes. The same issue as stated before. with an LRP solution, you wouldnt have that issue. but you would find that optimizing the code to minimize queries is going to be the hard part. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993209 Share on other sites More sharing options...
ignace Posted January 12, 2010 Share Posted January 12, 2010 Reason I said: The problem you will encounter is finding out if your background task is running, If the Scheuleder handles all tasks, than users dont matter. as you only want 1 scheduler running. All You have to do is check if your Scheuleder is running, if not, start it up. And it wont use up all resources. It doesn't matter if you use Scheduler. Each new task will eat up more resources. It's not because you only use one instance of something that apparently uses 50 MB after startup that if you initialize 10k of tasks on this instance the amount of RAM used won't increase. It's not because windows uses 2GB of ram that if you launch each and every game you own your RAM usage won't increase because it runs in Windows. Imagine what would happen if I would launch Avatar, The Saboteur, Star Wars: TFU, TF2, Operation Flashpoint, DIRT 2, Left 4 Dead 2, Left 4 Dead, COD MW 2 all simultaneous... I have most of the site ajaxed anyways, but I need the server to update while the player is offline too It doesn't matter if the user is offline or not. Your database keeps track when which building is completed which is when build_complete_time <= now() Ajax and Flash are just a fancy way of displaying the building process and can aswell be done without. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993645 Share on other sites More sharing options...
greatstar00 Posted January 12, 2010 Share Posted January 12, 2010 you dont need to update every minute this is the thing when users are online, they send u commands when they want, and you have them send u the command each 5 minutes or so this shouldnt be a problem the updating process is just about calculation, for instance, a user has 1000 gold per hour, and he is building a market that will be finished in 1 hour, and he left at 30 minute later, that market will increase 10 gold per hour $new_increment_rating=(((currentTime-finishTime)/3600)*10)>0?((currentTime-finishTime)/3600)*10):0; //the above line basically, do this math, ((currentTime-finishTime)/3600)*10), if it is negative, give it a zero //it is to calculate how much will the new market will give in that time $currentGold=$currentGold+(currentTime-OldUpdateTime)/3600*$currentRating+$new_increment_rating //calculate how much time passed, in hour, and times the gold increment rate, then plus the new market's number //now, update the currentRating $currentRating+=$new_increment_Rating; //delete the finished building stuff u can see, my idea is the best, u dont eat up much extra resource it is basically, update all stuff when the user request it without cron, scheduler, or timer Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993690 Share on other sites More sharing options...
fooDigi Posted January 12, 2010 Share Posted January 12, 2010 if you absolutely needed something to run every second, you could write a script that loops 50-60 times with your functionality with a sleep(1) command, and call that script every minute or so. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993696 Share on other sites More sharing options...
ignace Posted January 12, 2010 Share Posted January 12, 2010 the updating process is just about calculation, for instance, a user has 1000 gold per hour, and he is building a market that will be finished in 1 hour This is a scenario where you would use CRON. To update player stats (+1000 gold) based on a few criteria (has a market?). Not to immitate the building process. Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993702 Share on other sites More sharing options...
greatstar00 Posted January 12, 2010 Share Posted January 12, 2010 the updating process is just about calculation, for instance, a user has 1000 gold per hour, and he is building a market that will be finished in 1 hour This is a scenario where you would use CRON. To update player stats (+1000 gold) based on a few criteria (has a market?). Not to immitate the building process. i dotn use cron, i just update when the user sends request i dont update each minute, or each hour, or any time interval this idea is better when there are less people coming to the website Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993705 Share on other sites More sharing options...
ignace Posted January 12, 2010 Share Posted January 12, 2010 Actually your idea only works if there are many users on your website. If there are only a few users on your website, guess what happens when no user logs on for 2 or more hours? Quote Link to comment https://forums.phpfreaks.com/topic/188055-php-scheduling/#findComment-993733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.