Reputation: 10541
I want to notify followers when a discussion changes. A discussion has many followers. Right now I'm doing this in the Discussion class:
self.followers.each do |follower|
DiscussionMailer.delay.new_reply_notification(user, reply) unless reply.user == user
end
While this is asyncronous, it's pretty slow, memory hungry and creates dependencies on redis and sidekiq. How can I send the self.followers array to mailgun or another api and send all of the emails like that? Here's my actual mailer method:
def new_reply_notification(user, reply)
@user = user
@reply = reply
@reply_user = @reply.user
mail(to: user.email,
from: "[email protected]"
subject: "#{@reply_user.user_name} has just replied!")
end
So I suppose the mail method is key here. How can I change it to do something like this:
mail(to: followers_aray,
from: "[email protected]"
subject: "#{@reply_user.user_name} has just replied!")
Im up for any gems, the mandrill gem looks promising.
Update
Everyone tells me to do this in a worker. While I agree, ive noticed this field in my smtp logs:
"envelope": {
"targets": "[email protected]",
"transport": "",
"sender": "[email protected]"
}
So im guessing my envelope can have any number of targets. If my smtp server is doing all the work, why do I need to use a worker?
Upvotes: 3
Views: 178
Reputation: 2129
in your case you should send your emails from a queue using sidekiq or similar this will improve your performance and move it into background job instead of realtime.
After the Discussion been changed push all the followers the the queue and it will send them one by one.
have a look here:
http://sidekiq.org/ https://github.com/mperham/sidekiq/wiki
Upvotes: 1