I\'m working on an online PHP application that has a need for delayed PHP event. Basically I need to be able to execute arbitrary PHP code x many seconds (but it could be da
checkout this with redis . may be useful to your problem
https://github.com/chrisboulton/php-resque-scheduler
This seems like the perfect place for an event Queue in a database.
Have your user-created events (triggered by visiting the web page) create an entry into the DB that includes the instructions for the action to take place, and the timestamp for when it should happen. You Daemon (either a persistant application or triggered by CRON) checks the DB for events that should have happened ( $TriggerTime <= time()
) and that have not been flagged as "processed" yet. If you find one or more of these events, execute the instruction, and finally mark the event as "processed" in the DB or simply delete the entry.
The bonus of using the DB to store the events (and not something that is resident in the RAM of an application) is that you can recover from a crash without data loss, you can have more than one worker reading in a single event at a time, and you can modify the event's simply.
Also, there are lots of folks who use PHP as a general daemon scripting language on servers, etc. Cron can execute a PHP script (and confirm that an instance of that "app" is already running) that checks the Event Queue every-so-often. You can have a little app that dies after a minute of inactivity, and then gets restarted by CRON. The app can check the DB for entries at a fast frequency of your choosing (like 1s). Normally Cron cannot do a timing event faster than once per minute.
A pure PHP solution exists. Pretty much what Evan said in his answer. The load on DB can be reduced (and lock problem) by just introducing a "Processing" state for the events. When processing script picks up the events from the queue (DB), they're marked as "Processing" and committed. After the script finishes they're marked as "Processed". If there was an error or script fails "Processing" events has to be updated back to original state. (This was meant to be a comment to Evan's answer but I don't have enough reputation yet)
What about this:
http://www.phpjobscheduler.co.uk/
Use the sleep function: http://php.net/sleep