[web] On daily updating a DB for web-based systems

Started by
6 comments, last by darklordsatan 18 years, 6 months ago
Hi everyone. The question I bring now is this. Suposse a web-based game, like a RPG such as dark throne or king of chaos. Every 24 hours lets say, the users get some determined amount of money (depending on their level, etc...). The easiest approach, would be simply, when the user gets online, check out if this is a new day, and the user should be given the money. But the problem here is obviously, the user is not the only one who is supossed to know about his money, i.e other players should know how much every single other user has in order to attack them, etc... (e.g the user is offline for 3 months... obviously the game must give the user his 24-hour money every day, regardless the fact the user is online or not). How would I implement such behaviour? I dont think this sort of web-based systems rely on external applications, like running a cron job in the background that would update the daily money amount for all users everyday, since you cant rely on the fact you are going to have a shell or even permission to execute such tasks on your webhost. (I mean, big games like dark throne could... but lets pretend Im making a game who should be installable everywhere with just apache+php+mysql+web-based upload interface and thats it). My opinion, and I dont know how wrong might I be, is that, the first user to get online everyday is the one who triggers the RPG system to update the DB tables accordingly. For example, nobody gets online for 3 days. If I look at the DB tables in the second day, Ill see the daily money for the past 2 days hasnt been given to any user yet. But when user Joe Doe connects to the game at the fourth day, the game realizes this is a new day, and everyone should get their daily amount of money... Is this how it works? Another example would be the "users online today" in forum systems. My bet is that the first user to get online in a new day (after 12:00 am server time obviously) will force the forum scripts to update the online users table, delete the current records, and then proceed to insert the user. Am I close? IMHO, this would be a sorta dumb approach, since I think it would mean every single client request for a script would force a DB check, even if we are in the middle of a day and such... or maybe this is the only way to do it? Thanks a lot for your time.
Advertisement
cron would be the best way to handle it. I'm sure there are plenty web hosts out there that grant shell access and allow cron jobs.
Free Mac Mini (I know, I'm a tool)
Cron is the best way to handle it.

If for some reason, you can't run the task directly as a cron job, you could run a cron job somewhere else (on another machine if necessary), which could make a HTTP request to a URL (with parameters if necessary) to "kick off" the cron job running inside (for example) PHP or ASP.

Be sure if you do this, to be aware of any timeouts imposed by PHP / ASP and not hit them. Make sure that you have some logging / reporting to monitor this process.

Mark
Guys, maybe I didnt explain myself well.
Ok, lets ditch the RPG system idea :)
Now lets just talk about the "users online today" function in forum systems. How is it handled if none of them require shell access?
Most forum apps keep track of when a user last logged in in the database, so they just have to run a query for users that logged in that day.
I never heard of a cron job before, but I'm glad I Googled it. Very useful stuff :)
Programming since 1995.
What you described can easy be done by attaching a job to the first person that logs in of a given day. That's trivial. The problem is that the ammount of work that this faked cron job would do would have to be small for two reasons:

1) script timeout (can be overcome easily though)
2) user annoyance. You don't want to force the first guy to log in wait 10 minutes to recalculate some global site stats. A faked cron like this is nice if you can keep it witin 2-3 seconds or so. No more (not even for really big sites).

Most decent host support cron jobs though. It's no outragous requirement on your web application. Even Windows scheduler will do (for cross-platform support). Just make a single php script that needs to be called once every 24 hours and put something like "wget <url_to_script>" in the cron job.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Ah ok Sander. So it was like I expected, but still I dont like it...
Well, thanks y'all for your answers.
Cheers.

This topic is closed to new replies.

Advertisement