What is the most elegant way to implement a digest email address without inventing a queuing system?

I have my transactional email system, and by default, people receive emails as events occur:

class Comment after_create :email_original_poster def email_original_poster UserMailer.delay.notify_author_of_comment self end end 

However, instead of receiving mail as if somehow, a piece of my users would prefer a daily or weekly digest.

What is the cleanest most elegant way to implement this?

I have already started delayed_job , but this does not look like a delayed_job task, since I queue up with data that needs to be acted upon, and not with actions that need to be performed.

... without rethinking the queuing system

I know that the queued_emails table is the obvious solution, and of course I could do that. The reason I ask the question is because for this you need to re-create the queue system. Not only are there many queuing systems, but as this well-worded post from Percona points out, it’s recommended that you don’t turn it off yourself:

http://www.engineyard.com/blog/2011/5-subtle-ways-youre-using-mysql-as-a-queue-and-why-itll-bite-you/

You implemented a digest, you used delayed_job and what did you learn?

+6
source share
3 answers

Obviously, you do not need to create your own queuing system. I did not use delayed_job, but I used resque in combination with a beautiful little gem called resque_mailer , which should do exactly what you want. What's good about resque_mailer is that after you configure it, you don’t need to change the way you send mail MyMailer.some_mailing({vars}).deliver : MyMailer.some_mailing({vars}).deliver If for some reason you don’t want to use the queue to send mail (send it right now ), you just need to add! after delivery, and he will do just that.

Look at resque (with redis) and resque_mailer, I think it will do what you want.

+1
source

A digest looks more suitable to be performed as a cron job. You still have to control “those you sent” to deal with accidents and errors, but the idea is to run, for example, daily, a special rake task that creates your email message, including all the information in the digest format and sending them or the queue for sending.

+1
source

If someone wants to get more detailed information on how to do this, there is a good overview here (second answer): Sending emails based on intervals using Ruby on Rails

+1
source

Source: https://habr.com/ru/post/898234/


All Articles