【问题标题】:Sending notifications with interval 1 minute using resque使用 resque 以 1 分钟间隔发送通知
【发布时间】:2017-01-13 14:13:03
【问题描述】:

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

有没有办法为 resque 设置它?

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

【问题讨论】:

  • 您没有利用 GCM 主题消息传递的任何原因?

标签: ruby-on-rails google-cloud-messaging resque


【解决方案1】:

你可以试试:

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-07
    • 2017-10-17
    • 2018-12-29
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多