Jump to content

Ensuring PHP "cron" job runs only once?


Attilitus

Recommended Posts

I have a script which basically does the following:

Reads time stamp from database.
Checks timestamp against current time.
If the difference is above a certain amount a code snippet runs which then sets the timestamp in the database to the current time.

Now... this works fine and dandy in theory. But is it possible that this script will actually run multiple times if users access it near the same time? If this is a valid concern, would there be any way to lessen the chances of this occuring?
Link to comment
Share on other sites

If the code to update only runs when the stored time and the current time have x amount of difference between them, and every time it runs its updated to the current time then how would a person y who ran the script 2 seconds after person x cause the update code to run if the required difference is for example, 10 minutes? Perhaps you could post some code if you need to make things clearer?

And if this is actually a cron(scheduled) job then whats the point of setting a cron to run once a day to update a field with the time the cron ran?
Link to comment
Share on other sites

I was probably not entirely clear. This is not a "cron job" this is a wholly php device within one of my scripts that is acting in replacement of a cron job.

Whenever the script is loaded by a user, it checks the time against the last stored time in the database. If there is a difference between those two times of lets say 30 minutes, then a few queries will run. After those queries have run, the timestamp in the database is updated to reflect the current time. Therefore, the queries will not run again until the difference between the last recorded timestamp and the current time again reaches a certain level.

My question is whether there is a chance that two users would trigger the script at the same time, and the queries that are supposed to run only once every 30 minutes could run twice.

Here is the relavent code, but it really isn't very helpful:

[code]

//if negative this defaults to 0
if(crontimecheck($userinfo)==0){

//this stuff needs to only happen once per time period

<<Do stuff>>

//set database value that crontimecheck() checks to current time.
setlastactivity(time());

}


[/code]
Link to comment
Share on other sites

[quote]My question is whether there is a chance that two users would trigger the script at the same time, and the queries that are supposed to run only once every 30 minutes could run twice.[/quote]

Its unlikely since your code is going to be executed in fractions of a second. If its absolutely imperative that the record is only accessed by one user at a time then you should look in to database transactions, but as redarrow suggested, the simplest solution is to set up a cron job (if you're on a web host there should be a way to do it through the hosts control panel), otherwise this: [url=http://www.faqts.com/knowledge_base/view.phtml/aid/1005]http://www.faqts.com/knowledge_base/view.phtml/aid/1005[/url] might be helpful.
Link to comment
Share on other sites

Ah, well that is good to know thanks for the info. It is not exactly a matter of national security, but it is important that things  run cleanly at least 98% of the time, just for the sake of having a well-working script.

The reason why I would like to avoid using a traditional cron job is mainly because I want the script to be as user friendly as possible, and am attempting to maintain near-universal compatability. A manual cron job set-up is just not something to which I am willing to resort.

Thanks alot for the info!

I would ask again though, what do you think would be the chances of such a job running more than once on a relatively high-traffic site? Perhaps if the timestamp was updated before the main queries were run things could be tightened up a bit? (The queries cover a large number of rows)
Link to comment
Share on other sites

It all depends on what kind of threading and multi-tasking the server uses. If the server doesn't use real multi-tasking, then you're fine. However, if it's possible that two processes could be started at the same exact time, then there's a possibility of overlap. It's highly unlikely that duplicate threads would make their way through the whole system in complete synchronization, and maybe impossible, but I'm not very well up on the low level stuff. In order for there to be a problem, Apache would have to handle the duplicate requests at the same exact time. However, the likelihood of that happening is next to nil.

In the off-chance that it is indeed possible, your idea of updating the timestamps first thing would be an excellent idea - then you could throw an error for the next request, even before you waste any time and energy running useless queries.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.