使用 resque 以 1 分钟的间隔发送通知

Sending notifications with interval 1 minute using resque

我正在尝试使用 GCM 和 resque 向用户发送通知。目前,它正在向所有用户发送消息,并且由于用户太多而使服务器激增。所以我要尝试的是每分钟向 1000 个用户发送消息。

有没有办法为resque设置它?

我想到一件事,循环遍历所有用户,然后对 1000 个用户进行批处理。虽然仍在循环中,但超时 60 秒。 但我认为这种方法很糟糕。

你可以试试:

  1. 通过索引批量查找用户,因此可以使用索引来计算延迟时间。
  2. 使用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