I have 2 models: User and Group Every time a user joins a group, an email gets sent to all members of that group. I have a Resque worker that takes care of sending emails in the background.
The question is the following: Should I iterate before or inside the Resque task?
Example 1 (before):
# inside controller action
@group = Group.find(params[:group_id])
@group.users.each do |user|
Resque.enqueue(EmailSender, {:user_id => user.id})
end
# inside worker
class EmailSender
@queue = :emails_queue
def self.perform(args = {})
user = User.find(args['user_id'])
Sender.send('new_member', {:user => user}).deliver
end
end
Or
Example 2 (inside):
# inside controller action
@group = Group.find(params[:group_id])
Resque.enqueue(EmailSender, {:group_id => @group.id})
# inside worker
class EmailSender
@queue = :emails_queue
def self.perform(args = {})
group = Group.find(args['group_id'])
group.users.each do |user|
Sender.send('new_member', {:user => user}).deliver
end
end
end
Should I go with example 1 or example 2?
Thank you
You should use Example 1 (before) for the following reasons:
It's easier to debug because you can see all the important logic in one place. Example 2 splits up an important feature (emailing all members of the group) into two separate places.
It is more flexible. Suppose you want to send a similar (or the same) email in a different context? This way, you can use the same Resque class rather than creating a new one.
(As another commenter pointed out) If an error occurs in your Resque class, it will retry. You probably want it to only retry a single email, not the whole batch. So, the Resque job should only be responsible to send one email.