我有 2 个模型:用户和组 每次用户加入一个组时,都会向该组的所有成员发送一封电子邮件。我有一个负责在后台发送电子邮件的 Resque 工作人员。
问题如下:我应该在 Resque 任务之前还是内部进行迭代?
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
或者
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
我应该使用示例 1 还是示例 2?
谢谢