Rails / delayed_job - want to load newest version of job class

梦想与她 提交于 2019-12-01 13:02:27

There is nothing builtin to do this. Generally you are responsible for managing and reloading your workers. This is probably just as well since Rails development reloading is good but not perfect, and attempting to auto-reload a delayed job would potentially run into all sort subtle issues that would be pretty opaque to debug inside a worker process. Also, if it automatically reloaded the environment for every job a lot of use cases would get tremendously slow in dev mode.

My suggestion is just to get use to doing rake jobs:work and then Ctrl-C when you make changes. Alternatively you can create a script that just manually runs the jobs on an ad-hoc basis (taken from delayed_job docs):

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment'

Delayed::Worker.new.start  

I use this hack that seams to work quite nice, but be aware that it's probably very Rails and delayed_job version specific so you probably need to change some things. Tested with Rails 3.2.0 and delayed_job 2.1.4.

Put this into e.g. script/delayed_job_development and run it from the Rails root.

#!/usr/bin/env ruby

require File.expand_path('../config/environment', File.dirname(__FILE__))
require 'delayed/worker'
require "rails/console/app"

class DummyConsoleClass
  include Rails::ConsoleMethods
end
dummy_console = DummyConsoleClass.new

worker = Delayed::Worker.new({:quiet => false})

puts "Waiting for jobs..."
loop do
  if Delayed::Job.find_available(worker.name).count > 0
    puts "Found jobs"
    dummy_console.reload!
    loop do
      break if worker.work_off.sum == 0
    end
    puts "Done, waiting for jobs..."
  end
  sleep(2)
end

Please comment if you know that this is a very bad idea or things to be aware of, I mainly use it when editing and testing jobs that run immediately not with jobs scheduled to run long into the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!