Jump to content

Reliably triggering events once at a certain time


Recommended Posts

There are a couple great threads on SO about how to store complex recurring events for a calendar:

However these are for storing events that get read at any time. 

I'm trying to repurpose these storage methods for having scripts run at those times. Once. Reliably. And be run late if they don't run on time. I might have thousands or tens of thousands of events, so I've read that I should not use Apache's Crontab for this.

For example, I have a script that needs to send an email to a customer every Tuesday and Thursday at 2PM. Thanks to those threads I now understand how to store that interval, and even how to query for it. What I can't figure out how to reliably only send the email to the customer once.

Here's the best solution I've come up with:

I run a cron job every minute. It polls the database and finds events that should have run in the last 5 minutes, and that haven't been run in the last 5 minutes. It adds the event to a queue (another table?) to be run by a separate script. It then updates the event record to say it ran now.


  • If the cron job ever fails, or takes more than 5 minutes, it could miss events that fall outside of the 5 minute window.
  • What if an event is scheduled every 2 minutes?

How can I guaranty the event only fires once when scheduled, even if the interval between events is every minute?

How would you solve this?

Instead of storing the last time it ran, should I calculate and store the next time it should run and just query based on that?

Thanks for any help!

Link to comment
Share on other sites

I personally have been using cron jobs for any timed execution and its worked well for me.

Thanks! I use cron jobs for a few things, but I'm talking about setting up thousands and thousands of events. I've read that setting up thousands of cron jobs is a bad idea. How many do you have?

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.

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.