Reputation: 5302
I'm implementing an application that would check if the time is 4 or 6 or 12 hours after the data was inserted in the database.
So for example:
The data was inserted 1pm, then it's already 5pm (so this is 4 hours after the data was inserted), my application would process another task (no need for an explanation for this, I got it cover :D )
So this is a picture of my database fields:
id | name | time_added | time_to_check
ID is of course a type INT()
name is Varchar()
time_added is a type INT() because I am using time()
function of PHP to insert a value.
time_to_check is a type INT because this is the field where i'll insert the 4,6, or 12
So my question is how would I implement this one? Is setting up a Cron Job a good idea to perform this task?
If Yes, every what time should I run the Cron Job (every 15mins,1Hour, Once a day)? I know there are lots of consideration in doing this task. So I need your ideas guys.
If you have an idea please share it to me or even code how you think to implement this one, it would be a great help!
Thank you very much! :)
ADDITIONAL INFO:
My concern about running it every minute is that not all of the data are inserted at the same time. Like for example:
Data1 was added 3:15pm and time to check is 4 hours
Data2 was added 3:20pm and time to check is 4hours.
What if the cron job didn't execute during 3:15pm or 3:20pm, what should I do?
Upvotes: 1
Views: 277
Reputation: 6592
If you need data to be routinely processed at 4, 6 or 12 hours after insert, you could also look at the Linux (not PHP!) 'at' command which allows you to queue processes to execute at a particular time. If you are expecting a lot of inserts then cron remains a better option though.
Upvotes: 1
Reputation: 3882
I would recommend todo this task with a cronjob. The usage and some examples are explained here: http://en.wikipedia.org/wiki/Cron
The timeframe depends on your needs. If you check for 4,6 or 12 hours a cronjob per hour seems to be ok.
Upvotes: 0
Reputation: 75955
Yes a cron job would be good for this task. You would need to run it in an LCM of the times you'd like to check it. So from what I can see you should run it after 2 hours. (There would be a slight margin of error though - due to rounding). If you need complete accuracy you should check it every minute
Upvotes: 0
Reputation: 324750
Cron is probably your best bet, having it check the database for records to check and going through them.
As for how often you want it, it mostly depends on how accurate you need the time to be. For instance, in my game I have a cron running every minute to check for fleets arriving at their destinations, but only every hour for checking if any banned users have served their sentence and need unbanning.
Upvotes: 0