Answer the question
In order to leave comments, you need to log in
How to properly set up active_job and resque in rails 4.2.1?
Hello!
I'm trying to create two background jobs - one for sending email (will run as a callback when the model changes)
and a parser (which will run every hour).
Project on rails 4.2.1, also using resque-scheduler (4.0.0), resque (1.25.2)
My config.ru file (to add resque admin on startup):
# 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
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
CheckUpdatesJob:
queue: parse_updates
every:
- '1h'
- :first_in: '10s'
class: CheckUpdatesJob
args:
description: scrape page
ActiveJob::Base.queue_adapter = :resque
#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
class CheckUpdatesJob < ActiveJob::Base
queue_as :parse_updates
def perform()
ParseStrategy.new.check_for_updates
end
end
redis-server
rake environment resque:work QUEUE=parse_updates
rake environment resque:work QUEUE=email_notifications
rake environment resque:scheduler
rails s
Answer the question
In order to leave comments, you need to log in
Try to /lib/tasks/resque.rake
add Resque::Scheduler.dynamic = true
before
Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")
There is a problem with running ActiveJob tasks via resque-scheduler
.
The problem is described here . In short, you need to run the task through the adapter ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper.
Here you can read about solutions to the problem. One of them is this gem .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question