Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

php

cron

lamp

Greetings, I am developing a web app. One piece of it will allow users to schedule a "reminder" email to be sent to them at a particular time of day. What is the best way to accomplish this? Basically, all the solutions I've come up with operate on a "polling" pattern when what I want is an "interrupt" pattern.

Here are some possible solutions I've come up with:

  1. Have a cronjob fire every minute. The script that fires checks a database to see if there are any emails to send, if there are, it sends them, else it goes back to sleep. Drawback with this is that there is a bit of overhead incurred every minute. Also, this may not be a scalable system, especially when the number of users gets so large that it may take over a minute to send out all the emails.

  2. Same as #1, but job only fires every 15 minutes. This is a bit more manageable, but not perfect, as it restricts the users to reminders on the 15 minute marks, and it still incurs a bit of overhead when there are no emails to send. Not bad, but not perfect either.

  3. Have PHP exec() a bit of code that dynamically alters crontab or schedules an "at" job in the underlying linux. This would give me the flexibility and "interrupt" type model I so crave, but would open up a huge security hole in allowing PHP to exec() linux code. So, I'm going to go ahead and rule this one out.

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

like image 291
Keenahn Jung Avatar asked Jun 08 '11 00:06

Keenahn Jung


People also ask

How do I automate reminders?

Select Automate > Set a reminder > [select specific date column name here]. Under the Set a reminder panel, Power Automate should sign you in to the apps that the flow uses. When you see green check marks, select Continue. Enter a flow name, and number of days in advance reminder, and select Create.

How often should you send reminder emails?

A Sample Schedule Between 2-8 emails per month should be enough to get your message out to your entire list without being too much. You can start with only a few emails, increase the frequency when you have something that your recipient can act upon, then drop back to a lower level.


2 Answers

You can have a PHP script that remains running. Every set interval, query the database for emails that need to be sent in the next interval. Break that down into an array with one group for every minute. So if you choose 15 minutes, you would have an array with 15 entries, each entry having all emails that need to be sent out at that time.

You can then use forking to split the process, one handles sending the emails, the other sleeps until the next minute and splits again. To scale, you could fork multiple processes with each process handling a certain number of emails.

In a nutshell, one process manages the queue and forks other processes to handle the sending. When the queue is "empty" it gets more. You could have a cron running periodically to make sure the process hasn't died.

like image 191
Brent Baisley Avatar answered Sep 29 '22 00:09

Brent Baisley


Use first variant.

it may take over a minute to send out all the emails

  1. Check, if file_exists('mailing.q'); If still exists - terminate execution.
  2. Create file mailing.q
  3. send emails
  4. unlink('mailing.q');

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

like image 25
OZ_ Avatar answered Sep 29 '22 00:09

OZ_