Help with a solution for running a script at an interval (not as simple as it may sound)

1 replies
I've tried to come up with a solution a bunch of different ways and each time I come up with some reason it won't work. I'm hoping someone here will have an idea!

I'm building a php-based site for a client who will have users create tasks as part of their account. These task will do different things for the user at the interval set by the user (i.e. send an e-mail reminder, alert a user, send a SMS). The minimum interval would be one minute. So, for instance, send a SMS message every two hours (to remind them to move a car or something). Get the idea?

The database side of things and the actual alert is not a problem. Where I'm having trouble getting my mind around is how to get the alert to actually alert.

I think a cron job is the best solution, but each alert, since they run at different intervals would have to be a line of it's own, right?

Additionally, I wouldn't want one alert that may take longer for one client stall an alert for another client, so having one file that runs through the database and then does the alert doesn't seem like a good, longterm solution (as the site gets more use this may take more than a minute to run).

I thought about using the cron folders and putting things in there, but that starts at hourly.

I've looked and couldn't find a good way to update (or replace) the crontab file each time a new task is added through php.

So, I'm sort of stuck. Anyone have any ideas?

Thanks in advance.
#interval #running #script #simple #solution #sound
  • Profile picture of the author Unomateo
    Yes you will need a cron job... It's the only way to run scripts automatically.

    To solve the problem with many events firing and thus blocking other events, you need to use either Gearman or Redis. These are both backend event queues that will not block other events. Gearman is not persistant while Redis is, meaning if there is a queue and the server crashes, in gearman the queue is lost.

    Set your cron to fire every minute. the cron will fire a script that checks the DB for an event and send it to Gearman to process. In most cases the Queue will never build up until you have a ton of events. If your events are not related, I would make a class to handle each of them with an observer pattern.

    Write a class for each event and then use the function call_user_func_array() to fire each event

    example:

    function send_sms($user_id, $message){
    // do stuff to send message
    }

    call_user_func_array("send_sms", array(3,'go move your car!'));
    {{ DiscussionBoard.errors[4671994].message }}

Trending Topics