Search code examples
ruby-on-railsresquerails-activejobresque-schedulerrails-4-2-1

how setup active_job and rescue scheduler?


I'm trying to create background jobs for email notification and scraper. I use resque-scheduler (4.0.0), resque (1.25.2) and rails 4.2.1.

My config.ru file:

# 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

My /config/resque_scheduler.yml:

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

My /config/initializer/active_job.rb

ActiveJob::Base.queue_adapter = :resque

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

My first job for emails notifications:

class EmailNotificationJob < ActiveJob::Base
  queue_as :email_notifications

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

My second job for scheduled runs:

class CheckFsUpdatesJob < ActiveJob::Base
  queue_as :fs_updates

  def perform()
    FsStrategy.new.check_for_updates
  end
end

So I have to jobs: 1. emails notifications - should sends email when new record in DB appears 2. scrape a page - should runs every hour

How I run it:

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

After running these commands I see in Resque Dashboard two workers and two queues, as it is expected.

But!

After clicking on 'queue now' button at resque Schedule tab, I see that task was created and wroted to "fs_updates" queue. But it's not running and in a few second it dissapears.

When I run a job for emails sending from rails console - it does not work at all.

Please, help me to fix my configurations. Thanks kindly!


Solution

  • As I understood: rails and active_job developers is not responsible for resque plugins. Maybe this problem will be fixed in new gem versions, but now it does not work (active_job does not work fine with resque-scheduler).

    Currently I use gem 'active_scheduler' to fix current problem.