Open Side Menu Go to the Top
Register
If I knew what I was asking this would have a better title If I knew what I was asking this would have a better title

02-10-2012 , 10:09 AM
Warning: Vague and probably dumb question follows.

Been learning how to code the past few months and am building a small site for fun/to keep learning. The site has games that people can enter where entrants have to sign in every day and complete x task. If task is completed correctly, user continues on to the next day. If incorrect, user is eliminated. However, if user does not even attempt the task in that 24 hour period they are also eliminated.

My current method of having them eliminated when they don't participate is when they go to the games lobby page a script compares time stamps of them doing x task and if they didn't do x yesterday they are eliminated. However, if a user registered for a game and then never logged in they wouldn't be eliminated because the script only checks for that individual user and if that user never came back to the games lobby page it would never check if they have been participating every day.

So I know there is a way to have a script or something run in the background that either constantly monitors all user activity or every 24 hours it would scan the database and eliminate anyone who didn't participate that day. Can someone point me in the general direction of creating these "server-side" scripts or whatever the hell they're called?

The site is just built in HTML/PHP with a MySQL database btw. Thanks in advance.
If I knew what I was asking this would have a better title Quote
02-10-2012 , 11:49 AM
To schedule a script look into cron jobs.

http://en.wikipedia.org/wiki/Cron

Most likely if you're running PHP and MySQL your site is hosted on a Linux server which typically uses cron to schedule task. At lot of hosting control panels will allow you to schedule task to run. You could schedule a page hit to your script, eg: http://mysite.com/myscript.php
If I knew what I was asking this would have a better title Quote
02-10-2012 , 01:05 PM
As bjordan says, you want to set up a cron job to run a script that checks for users that should be eliminated.
If I knew what I was asking this would have a better title Quote
02-10-2012 , 01:21 PM
Just read the wikipedia page on cron and that is exactly what I want, thank you. Now to figure out how the hell these things work!
If I knew what I was asking this would have a better title Quote
02-12-2012 , 06:30 PM
Just for the sake of argument can you approach the problem another way (Mostly because long term Cron jobs are ****-tastic)?

For a very simple example, if you had a last-active timestamp and last-completed task timestamp in the database you could just query those two fields to get your users without ever needing to run a maintenance job.
If I knew what I was asking this would have a better title Quote
02-13-2012 , 05:19 PM
Quote:
Originally Posted by jjshabado
Just for the sake of argument can you approach the problem another way (Mostly because long term Cron jobs are ****-tastic)?

For a very simple example, if you had a last-active timestamp and last-completed task timestamp in the database you could just query those two fields to get your users without ever needing to run a maintenance job.
might use your idea actually, seems much simpler. and yes, i can approach it any way i like, im just building the site for myself as a way to keep learning.

just because im curious, why do you say cron jobs are crap? i would think running routine scripts like the one i described is a relatively common thing in web applications, but maybe not? or is there just a better way to schedule scripts than cron?
If I knew what I was asking this would have a better title Quote
02-13-2012 , 08:22 PM
Cron jobs are crap because they're hard to maintain and manage. It's easy to make a change to something and break the cron job without even realizing it. Or you buy new hardware and forget to copy the cron job. Or a machine gets reset at the wrong time and the cron job isn't run that night. All of these things can cause you to have unexpected data in your db and then unexpected/incorrect behaviour for your users.

There's also a strong correlation with needing cron jobs and having a poor design. For applications like yours its often a good idea to store raw facts instead of derived state in a database. If you have a bug in your code or want to change your business logic having the raw data lets you solve the problem immediately just by pushing new business logic. If you use derived state you have a much bigger job cleaning up all the bad rows in the db (and sometimes you might not be able to fix the problem at all).

I think running routine scripts like this are extremely uncommon. You can almost always get around the need for them by writing your business logic better.
If I knew what I was asking this would have a better title Quote
02-13-2012 , 09:52 PM
makes sense, ty for the explanation
If I knew what I was asking this would have a better title Quote
02-18-2012 , 08:29 PM
jj is right, this is more of a design problem. Instead of having a field in your database called `isActive` it's better to query the data live based on various times, this will remove any possibility of expired/out of date information in your system which is why proper normalisation is so good, as well as keeping your system easy to maintain.
If I knew what I was asking this would have a better title Quote

      
m