4

In my rails application, users can subscribe to some events. I'd like to send a mail to the user 24 hours before the begining of the the event.

I'm not sure how to implement that. A rake task ? Is there some gems or some best practices to follow ?

Thanks!

EDIT:

I guess that my cron must be run every minute to get each event that starts in 24 hours. But if the cron stops, or it takes more than a minute, I can't skip some emails. In the other way, how can I be sure I'm not sending the same email twice ?

Mathieu Mahé
  • 2,647
  • 3
  • 35
  • 50
  • You didn't try to enter some of your words into google, did you? – pdu Feb 19 '13 at 21:19
  • possible duplicate of [A cron job for rails: best practices?](http://stackoverflow.com/questions/285717/a-cron-job-for-rails-best-practices) – pdu Feb 19 '13 at 21:20
  • Of course I did, but it's not the same problem. Please try to understand before downvoted! Maybe I must clarify my question. It's not a simple cron. My cron must be run every minute to get each event that starts in 24 hours. But if the cron stops, or it takes more than a minute, I can't skip some emails. In the other way, how can I be sure I'm not sending the same email twice ? – Mathieu Mahé Feb 19 '13 at 21:26
  • Updated my answer to take this comment into account. – rainkinz Feb 19 '13 at 21:30

2 Answers2

6

Check out:

http://railscasts.com/episodes/206-action-mailer-in-rails-3

for sending mail and

http://railscasts.com/episodes/164-cron-in-ruby-revised

for using whenever to set up a cron to call your mailer

EDIT: Just saw your comment. In that case I would look at using something like resque-scheduler or the equivalent with sidekiq:

https://github.com/bvandenbos/resque-scheduler

then you can do things like this:

Resque.enqueue_at(24.hours.from_now, SendFollowUpEmail, :user_id => current_user.id)

sidekiq example:

FollowUpEmailer.perform_in(24.hours.from_now, current_user.id)
rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • 1
    I already use sidekiq. In fact, I don't really need a cron for that. I just need to add your line in a after_create, right ?! Is there a way to cancel the email if the user unsubscribe the event ? – Mathieu Mahé Feb 19 '13 at 21:32
  • I don't think there is a way see: https://github.com/mperham/sidekiq/pull/257, the last comment there Mike says that your job should check to see if it should be sent. In that case I'd just have: send_email if user.subscribed? in my job – rainkinz Feb 19 '13 at 21:36
  • Should be easy to implement. Thanks a lot! The only down side I see is if my server crashes or is rebooted, I lose all the messages in my sidekiq queue... – Mathieu Mahé Feb 19 '13 at 21:41
  • Oops, read a little further on that link I put up and there is an API to remove jobs now. However, in your case, I'd probably still go with what I suggested. – rainkinz Feb 19 '13 at 21:41
  • You can make redis save to disk from time to time to minimize the impact of a crash: http://redis.io/topics/persistence – rainkinz Feb 19 '13 at 21:44
0

https://github.com/mperham/sidekiq/pull/257

This will help you to solve automatic mailing system in scheduled time!

Vishnu
  • 1
  • 1