如何设置 active_job 和救援调度程序?

how setup active_job and rescue scheduler?

我正在尝试为电子邮件通知和抓取程序创建后台作业。 我使用 resque-scheduler (4.0.0)、resque (1.25.2) 和 rails 4.2.1.

我的 config.ru 文件:

# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application

require 'resque/server'  
run Rack::URLMap.new "/" => AppName::Application,  "/resque" => Resque::Server.new

我的 /lib/tasks/resque.rake:

require 'resque/tasks'
require 'resque/scheduler/tasks'

namespace :resque do
  task :setup do
    require 'resque'
    require 'resque-scheduler'

    Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")
    Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }
  end
end

我的/config/resque_scheduler.yml:

CheckFsUpdatesJob:
  queue: fs_updates
  every:
    - '1h'
    - :first_in: '10s'  
  class: CheckFsUpdatesJob
  args:
  description: scrape page

我的/config/initializer/active_job.rb

ActiveJob::Base.queue_adapter = :resque

我的/config/initializer/resque.rb:

#config/initializers/resque.rb
require 'resque-scheduler'
require 'resque/scheduler/server'

uri = URI.parse("redis://localhost:6379/")  
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }

Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }
Resque.schedule = YAML.load_file(Rails.root.join('config', 'resque_schedule.yml'))

Resque::Server.use(Rack::Auth::Basic) do |user, password|
  user = 'admin'
  password = 'admin'
end

我的第一份电子邮件通知工作:

class EmailNotificationJob < ActiveJob::Base
  queue_as :email_notifications

  def perform(episode_id, email)
    NotificationMailer.new_record_appears(record_id, email).deliver_now
  end
end

我的第二份工作安排在 运行s:

class CheckFsUpdatesJob < ActiveJob::Base
  queue_as :fs_updates

  def perform()
    FsStrategy.new.check_for_updates
  end
end

所以我必须工作: 1. 电子邮件通知 - 应该在数据库中出现新记录时发送电子邮件 2. 抓取页面 - 应该 运行 每小时

我怎么运行呢:

redis-server
rake environment resque:work QUEUE=fs_updates
rake environment resque:work QUEUE=email_notifications
rake environment resque:scheduler
rails s

在 运行 执行这些命令后,我在 Resque Dashboard 中看到两个工作人员和两个队列,正如预期的那样。

但是!

在重新请求计划选项卡上单击 'queue now' 按钮后,我看到任务已创建并写入 "fs_updates" 队列。但它不是 运行ning,几秒钟后它就消失了。

当我 运行 从 rails 控制台发送电子邮件时 - 它根本不起作用。

请帮我修复我的配置。 谢谢!

我在尝试在 rails 4.2.1 上配置 Sucker Punch 时遇到了同样的问题 最后,我将我的自定义初始化程序逻辑移到了 application.rb,不是很好,但它让我振作起来 运行。

不确定此版本中的初始化程序是否存在问题。尝试将您的代码从 active_job.rb 和 resque.rb 移动到 application.rb 或适当的环境文件中。显然这不是一个长期的解决方案,但它至少可以帮助您确定它是初始化程序问题还是 Resque 配置问题。

据我了解:rails 和 active_job 开发人员不对 resque 插件负责。也许这个问题会在新的 gem 版本中得到修复,但现在它不起作用(active_job 不能与 resque-scheduler 一起正常工作)。

目前我使用gem 'active_scheduler'来解决当前的问题。