From be77c4b0539c8f6caea357dd00a06a9f3ace63c7 Mon Sep 17 00:00:00 2001 From: anitasus <49550536+anitasus@users.noreply.github.com> Date: Fri, 5 Feb 2021 18:56:50 +0100 Subject: [PATCH] Make reloading more thread-safe. Reloader will determine whether a reload is needed, wait until it's safe to reload, and then do so, before continuing. If delayed worker is run in the same process as application (but in a different thread) and Reloader is not used, there might be interference between the threads when they reload concurrently: constants might be not properly loaded and unloaded. For instance, a constant that is being loaded in the main thread might be too early detected as a new constant in the delayed worker thread and, therefore, not be added to the list of autoloaded constants at the end of the loading in the main thread, which then leads to the constant not being unloaded, which then leads to such errors as "A copy of ... has been removed from the module tree but is still active!". Also, using Reloader may save resources since code will be reloaded not every few seconds but only when a reload is required. --- lib/delayed/worker.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/delayed/worker.rb b/lib/delayed/worker.rb index 1a352f775..9c94fcc6b 100644 --- a/lib/delayed/worker.rb +++ b/lib/delayed/worker.rb @@ -169,14 +169,17 @@ def start # rubocop:disable CyclomaticComplexity, PerceivedComplexity say 'Starting job worker' self.class.lifecycle.run_callbacks(:execute, self) do + count = nil loop do - self.class.lifecycle.run_callbacks(:loop, self) do - @realtime = Benchmark.realtime do - @result = work_off + Rails.application.reloader.wrap do + self.class.lifecycle.run_callbacks(:loop, self) do + @realtime = Benchmark.realtime do + @result = work_off + end end - end - count = @result[0] + @result[1] + count = @result[0] + @result[1] + end if count.zero? if self.class.exit_on_complete @@ -184,7 +187,6 @@ 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)