【问题标题】:Sending notifications with interval 1 minute using resque使用 resque 以 1 分钟间隔发送通知
【发布时间】:2017-01-13 14:13:03
【问题描述】:
我正在尝试使用 GCM 和 resque 向用户发送通知。目前它正在向所有用户发送消息,并且由于用户太多而使服务器飙升。所以我要尝试的是每分钟向 1000 个用户发送消息。
有没有办法为 resque 设置它?
我想到一件事,循环遍历所有用户,然后将用户批处理 1000 个。虽然仍在循环内,但会超时 60 秒。
但我认为这种方法很糟糕。
【问题讨论】:
标签:
ruby-on-rails
google-cloud-messaging
resque
【解决方案1】:
你可以试试:
- 使用索引批量查找用户,因此可以使用索引计算延迟时间。
- 使用
wait 选项来延迟作业而不是超时。
User.find_in_batches.with_index do |group, index|
puts "Processing group ##{index}"
GCMJob.set(wait: index.minutes).perform_later(group.ids) # each batch will have 60 seconds in between
end
class GCMJob < ActiveJob::Base
def perform(user_ids)
users = User.where(id: user_ids)
users.map(&:send_gcm_message)
end
end