B
B
bmalets2015-04-09 11:28:21
Ruby on Rails
bmalets, 2015-04-09 11:28:21

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

My /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

Actually my /config/resque_scheduler.yml:
CheckUpdatesJob:
  queue: parse_updates
  every:
    - '1h'
    - :first_in: '10s'  
  class: CheckUpdatesJob
  args:
  description: scrape page

Active-job initializer /config/initializer/active_job.rb Rescue initializer /config/initializer/resque.rb:
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

Job to send mail:
class EmailNotificationJob < ActiveJob::Base
  queue_as :email_notifications

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

Job for periodic parser:
class CheckUpdatesJob < ActiveJob::Base
  queue_as :parse_updates

  def perform()
    ParseStrategy.new.check_for_updates
  end
end

How do I run it all:
1. redis-server
- the radish works
2. - the parsing worker works 3.rake environment resque:work QUEUE=parse_updates
rake environment resque:work QUEUE=email_notifications

- the worker for sending mail is running
4. - the scheduler is running running 5. - well, actually the rails After the launch of the rails, two workers, two queues are visible in the Rescue admin panel, the Schedule tab is also visible. But after clicking 'queue now' on the Schedule tab, you can see that the task appears in the queue, but it is not processed - as if it just disappears from the queue after a few seconds. I change the model through the rails console - the callback works, but the job with sending mail does not work. Similarly, with a periodic launch every hour .... Tell me, please, what did I set up wrong, maybe I start it wrong ... Because everything seems to work, but nothing works. Thanks in advance. rake environment resque:scheduler
rails s

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yoyasimf, 2015-08-04
@bmalets

Try to /lib/tasks/resque.rake
add Resque::Scheduler.dynamic = true
before

Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")

M
michaelkl, 2018-06-22
@michaelkl

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 question

Ask a Question

731 491 924 answers to any question