diff --git a/lib/scheduler/defer.rb b/lib/scheduler/defer.rb index 9758d95f287..21ef836e4a6 100644 --- a/lib/scheduler/defer.rb +++ b/lib/scheduler/defer.rb @@ -24,6 +24,7 @@ module Scheduler @reactor = nil @timeout = DEFAULT_TIMEOUT @stats = LruRedux::ThreadSafeCache.new(STATS_CACHE_SIZE) + @finish = false end def timeout=(t) @@ -72,7 +73,11 @@ module Scheduler end end - def stop! + def stop!(finish_work: false) + if finish_work + @finish = true + @thread&.join + end @thread.kill if @thread&.alive? @thread = nil @reactor&.stop @@ -96,7 +101,7 @@ module Scheduler @thread = Thread.new do @thread.abort_on_exception = true if Rails.env.test? - do_work while true + do_work while (!@finish || !@queue.empty?) end if !@thread&.alive? end end diff --git a/spec/lib/scheduler/defer_spec.rb b/spec/lib/scheduler/defer_spec.rb index 8f418beebae..2de303da1f2 100644 --- a/spec/lib/scheduler/defer_spec.rb +++ b/spec/lib/scheduler/defer_spec.rb @@ -28,7 +28,7 @@ RSpec.describe Scheduler::Defer do @defer.later("second") {} @defer.later("bad") { raise "boom" } - wait_for(200) { @defer.length == 0 } + @defer.stop!(finish_work: true) stats = Hash[@defer.stats] @@ -105,11 +105,8 @@ RSpec.describe Scheduler::Defer do it "can queue jobs properly" do s = nil - @defer.later { s = "good" } - - wait_for(1000) { s == "good" } - + @defer.stop!(finish_work: true) expect(s).to eq("good") end