From e4320858ef6ae3b69fc5506c9d86cc7aa1878602 Mon Sep 17 00:00:00 2001 From: David Genord II Date: Fri, 3 Oct 2014 17:27:12 -0400 Subject: [PATCH] A limited attempt at automatic code reloading If ActionDispatch::Reloader is available, introduced in Rails 3.1, we can use that to trigger the standard rails code reloading. --- lib/delayed/worker.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/delayed/worker.rb b/lib/delayed/worker.rb index 3453c12d9..925c65e33 100644 --- a/lib/delayed/worker.rb +++ b/lib/delayed/worker.rb @@ -100,6 +100,10 @@ def self.lifecycle @lifecycle ||= Delayed::Lifecycle.new end + def self.reload_app? + defined?(ActionDispatch::Reloader) && Rails.application.config.cache_classes == false + end + def initialize(options = {}) @quiet = options.key?(:quiet) ? options[:quiet] : true @failed_reserve_count = 0 @@ -155,6 +159,7 @@ def start # rubocop:disable CyclomaticComplexity, PerceivedComplexity break elsif !stop? sleep(self.class.sleep_delay) + reload! end else say format("#{count} jobs processed at %.4f j/s, %d failed", count / @realtime, @result.last) @@ -286,5 +291,11 @@ def reserve_job raise FatalBackendError if @failed_reserve_count >= 10 nil end + + def reload! + return unless self.class.reload_app? + ActionDispatch::Reloader.cleanup! + ActionDispatch::Reloader.prepare! + end end end