Best way to dynamically schedule email reminders? Anything better than cron?

Greetings, I am developing a web application. One of them will allow users to schedule a “reminder” via email, which will be sent to them at a specific time of the day. What is the best way to do this? Basically, all the solutions that I came up with work with a “polling” pattern, when what I want is a “interrupt” pattern.

Here are some possible solutions that I came up with:

  • You have a crown fire every minute. a script that runs a database check to see if there are any emails to send, if any, it sends them and then goes back to sleep again. The disadvantage of this is that every minute you have to impose additional overhead costs. In addition, it may not be a scalable system, especially when the number of users becomes so large that it may take more than a minute to send all emails.

  • Same as # 1, but work is done only every 15 minutes. This is a bit more manageable, but not ideal, as it restricts users to a reminder of 15-minute characters, while still not having enough overhead when there are no emails to send. Not bad, but not perfect.

  • PHP exec () has some code that dynamically modifies crontab or schedules the execution of the "at" on base Linux. This would give me the flexibility and model of the “interrupt” type that I crave, but open a huge security hole that allows PHP for Linux exec () code. So, I'm going to go and release it.

So, is anything better than what I came up with? Perhaps a way to schedule email without using cron? I am very curious to see what you guys have to say about this :).

+5
source share
5 answers

Use the first option.

it may take more than a minute to send all emails

  • Check if file_exists ('mailing.q'); If still exists - stop execution.
  • Create mailing.q file
  • send e-mail
  • Unlink ('mailing.q');

And don't think about overhead - not in this case.

+2
source

Your PHP script may be running. Each specified interval requests a database for emails to be sent to the next interval. Break it into an array with one group for every minute. Therefore, if you choose 15 minutes, you will have an array with 15 entries, each entry will have all the emails that should be sent at this time.

Then you can use forking to split the process, one processes the sending of letters, the other sleeps until the next minute and splits again. For scaling, you can fork several processes, with each process processing a certain number of letters.

In a nutshell, one process manages the queue and creates other processes to process the dispatch. When the queue is “empty”, it gets more. You can run cron periodically to make sure the process is not dead.

+3
source

There is nothing special about using cron in options # 1 and # 2, I don’t know which application you are using, but you may not need to give users the ability to schedule the exact minute. Even then, it probably won't be a problem if your script marks the reminder status as “pending” or such, and any new script instances send only those that are not “waiting” or “sent”.

You can use Hudson or a similar application that can help with script management and allow you to monitor for crashes, etc. It can even send notifications when there are crashes. It supports native java-based cron system.

If the application really gets big, you might want to offload this process to a separate server from your web server. You can also look at third-party mail sending tools if you are not already using an external SMTP service and see what integration tools they may have. It should also increase delivery speed, etc.

+2
source

Do not confuse the order of sending mail and sending mail.

It may take fifteen minutes for your mail server to send one email. But to send a mail message, only mail(1) mail(1) 0.036s is required for sending.

And even if you have dialed more than 1600 letters to be sent per minute (good work!), You can set up your code a bit to start sending reminders by e-mail a few minutes earlier, waiting for “spikes” - let's say, looking ahead to your database data for five minutes to find out if there were> 1000 letters for delivery, and start them with 1/5 probability, 1/4 probability, 1/3 probability, 1/2 probability, then the sequence of residues.

+1
source

There is a hostman command that allows you to call a function at a specific time. This should do what you want.


This was my initial suggestion:

How about a combination?

  • Ask for a cron job after <timespan>.
  • Will it populate the file with a list of timestamp =>, reflecting whether there is an email before <current time> + <timespan>.
  • Go through the second cron in every minute if the time is found in the email list file, then run the email sending script.
0
source

All Articles