Execute scripts later in time
For a personal game I'm working on, I'd like to have a php function work over a certain period of time.
For example, right now it's set so that if the user wishes to purchase soldiers in the game, he fills out the form and when it's submitted, the user's database entry is instantly updated to reflect the change in soldiers.
What I would like is to have a time delay between the form submission and when the soldiers actually appear (to simulate training time, etc.).
I thought of one possible approach, but I hope it isn't the best one. I could create a new database table, "actions" that contains the type of action (i.e. the task to execute, in this case training soldiers), a parameter (the number of soldiers), and the action's start/end times. Whenever the user loads the page, the page checks to see if any actions have been completed and executes them if they are.
The key problem with this is that actions only execute when the user loads the page. He could buy soldiers and have them not appear until whenever he happens to log in, potentially leaving him vulnerable for large amounts of time.
I could resolve this by checking all user's actions whenever anyone loads the page, but I believe this would be unnecessarily taxing on the开发者_如何学Python server, in addition to being fairly difficult to implement.
Does anyone have better ideas, or is my train of thought the best approach there is? Thank you!
You invented a job queue, which is a good step. The problem with your solution is that each client constantly (for each request) checks the actions to execute which is a lot of unnecessary work.
It's a lot better to have one scheduler script that polls that database in each minute/hour/.., executes the pending tasks and removes the finished entries. Set up a cron job that calls that script in even intervalls.
Cronjob
Most Simple would be to use cronjob. If you can't install cron(jobs), you could use online cronjob web-services like for example http://www.setcronjob.com/, http://www.onlinecronjobs.com/, http://cronless.com/.
http://www.setcronjob.com/ has unlimited plan for only $10(I think pretty cheap).
Message Queue
Most efficient would be to use advanced message queue like beanstalkd which has delayed puts
. There are a lot of tutorials available explaining message queues, beanstalkd.
I would suggest a cron job. That is a script that you have set to execute on a time interval. In your case I would suggest maybe executing it maybe every fifteen minutes.
Then lets say you want it to take half an hour for the soldiers to be trained. You just check in that script whether or not it's been a full half hour since the soldiers were purchased. It could take anywhere from 30 minutes to 45 minutes for them to update then, since the script only checks every fifteen minutes, but you can play that in somehow to make it seem on purpose.
Say for example the user is purchasing soldiers at 3:54pm and your script is set to run every 15 minutes (4:00, 4:15, 4:30, 4:45... etc). Since the soldiers would be available at 4:30, you could have a message for the user telling them their next training camp starts in 6 minutes, so they know it will take a total of 36 minutes for the soldiers to be ready.
You could even make training time an upgradeable aspect of the game, so a user could purchase a better training camp or something which takes 15 minutes instead of half an hour. You get the idea :)
精彩评论