redis 将控制器方法用于重新排序后台作业

pgx2nnw8  于 7个月前  发布在  Redis
关注(0)|答案(1)|浏览(82)

我在Rails 4上使用Redis的Resque。
如何在后台作业中使用当前在application_controller中定义的控制器方法?
下面是我定义的当前方法:

def push_to_google(token, message)
  if token.present?
    gcm = GCM.new("843jf9384fj839f848j890fj3")
    registration_ids = ["#{token}"] # an array of one or more client registration tokens
    options = {data: {notification: "#{message}"}}
    response = gcm.send(registration_ids, options)
  end
end

字符串
我想在我的delayed_notifications中定义的这个后台作业中使用它:

class DelayedNotifications
  @queue = :notifications_queue

  def self.perform(registration_id, user_name)
    push_to_google(registration_id, "New message from #{user_name}.")
  end
end


当然,我的作业目前失败了这个错误:
第一个月

b1payxdu

b1payxdu1#

push_to_google提取(移动)到ApplicationHelper,并将ApplicationHelper包含在ApplicationControllerDelayedNotifications中。
修改后,您的application_helper.rb应该是:

module ApplicationHelper
  # other methods

  def push_to_google(token, message)
    if token.present?
      gcm = GCM.new("843jf9384fj839f848j890fj3")
      registration_ids = ["#{token}"] # an array of one or more client registration tokens
      options = {data: {notification: "#{message}"}}
      response = gcm.send(registration_ids, options)
    end
  end
end

字符串
application_controller.rb

class ApplicationController < ActionController::Base
  include ApplicationHelper

end


delayed_notifications.rb

class DelayedNotifications
  include ApplicationHelper

  @queue = :notifications_queue

  def self.perform(registration_id, user_name)
    push_to_google(registration_id, "New message from #{user_name}.")
  end
end

相关问题