1

I have setup crontab with whenever gem to send emails at time. Cron just calls the rake task, which gather all emails and sends them in loop. My question is do I need some queue system that provide me assurance that code won't crash ? I mean when I gather 1000 emails and I will be sending them in loop is it consider bad practice ?

As I can see i can catch errors but not all(maybe... ?). In mailgun doc https://documentation.mailgun.com/en/latest/best_practices.html#bounce-and-esp-feedback-handling

I would have to fetch bounces bye their api and resend mail if I want... So much painful approach. Don't get me wrong I still appraciate mailgun as a service.

sonic
  • 1,282
  • 1
  • 9
  • 22
  • Can your loop deal with exceptions / interruptions? – Stefan Aug 09 '18 at 13:10
  • I guess yes. I don't have real test for it. I mean in rake task handling errors work. To send mails I will use mailgun – sonic Aug 09 '18 at 13:18
  • So the e-mails are stored somewhere? Don't you have the opportunity to add a flag that it got indeed send, and use that in the where clause of the rake task? In case an e-mail wasn't send, it will get picked up by the next rake task. – bo-oz Aug 09 '18 at 13:20
  • What happens when your mail provider fails in the middle of your rake task? – Aaron Breckenridge Aug 09 '18 at 13:20
  • exacly that's the question, how to rescue that error. I guess I would have to use some api to get that information. I don't know yet. @bo-oz When should i add this flag ? I mean I do it after running send so its not a problem, but Aaron point is right, if anything go well but mail wasn't sended by mailgun, The flag would be wrong setted – sonic Aug 09 '18 at 13:27
  • You should be able to get that information from mailgun, but it depends a little bit on how important those e-mails are. If you need 1000% certainty, you need to check it, if it's acceptable that from time to time an e-mail does not get delivered, just fire and forget. The flag should be set after the Mailer has returned success (i.e. delivered message to Mailgun). So in the loop of 1.000 you will be updating individual email records with a flag that it got send. If somehow the code fails, you can continue where you left off. – bo-oz Aug 09 '18 at 13:30
  • So maybe approach suggested by Ramses to use Delayed job is good way. I need to be sure that mail got sended. I found this https://stackoverflow.com/questions/8709984/how-to-catch-error-exception-in-actionmailer – sonic Aug 09 '18 at 13:36

1 Answers1

0

While it depends on the context, for a low traffic app with a low number of emails to send, it won't be too much of an issue.

However, email sending is a "slow" task. You should consider using something like Delayed job to process these long-running processes.

Ramses
  • 996
  • 4
  • 12
  • 28
  • thank you for fast response. low traffic app. it means 1000 emails per 5minut on low cpu shouldn't be much issue ? How delayed job make it work ? – sonic Aug 09 '18 at 13:04
  • It's already delayed if you run it through a separate rake task right? – bo-oz Aug 09 '18 at 13:21
  • yes, but this gem maybe has more advance error handling and sets flag when mail got delivered instead attept to send.... i dont know.. Maybe those errors handlng that I found are not bad. But I don't know if I can be sure if mail got delivered. – sonic Aug 09 '18 at 13:57