How can I trigger events in a future time in PHP
I have written a PHP application that broadcasts a voice message to phone numbers in the database.
Now I want to add a "Schedule" functionality to it. It basically means that the administrator would be able to set a future date and time for a particular voice file. The voice messa开发者_如何学Cges will be broadcast at exactly that date and time.
How can I code this please? Some code snippets will be highly appreciated.
Thanks, Amit
You need to look into CRON jobs to automate script execution automatically. Take a look at: http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/ for some more info.
As Tom Walters says, cron (or Scheduled Tasks if you're using windows) is probably a good way to go (at least at first).
Cron's maximum resolution is 1 minute, so hopefully, that's precise enough.
Consider a table like:
create table calls(
id int,
target_time datetime not null,
actual_time datetime default null
-- plus whatever data are necessary
);
Then you write a script that does the following:
- Queries the database for all call with target_time <= the current
time, where actual_time is NULL. (Something like
SELECT * FROM calls WHERE actual_time IS NULL and target_time <= NOW()
) - Iterates over those calls, making the calls, and updating the row to set actual_time as it goes.
Then, you use cron (or whatever) to run that script every X minutes.
That's a basic architecture that should get you going.
Of course, things get more complicated if you have multiple lines for outbound calls, or other fancy requirements.
If you require accuracy approaching seconds, cron is going to fall short. At that point, you might consider writing one or more daemons (scripts that run continuously) to poll the database more frequently.
精彩评论