From 07813ba83cdbb879faf9a624ad4832b2f23aaa07 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Nov 2024 14:06:19 +1100 Subject: [PATCH] DEV: fix hanging spec (#29974) --- lib/scheduler/defer.rb | 5 ++++- spec/lib/scheduler/defer_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/scheduler/defer.rb b/lib/scheduler/defer.rb index 21ef836e4a6..d6a3132cd4f 100644 --- a/lib/scheduler/defer.rb +++ b/lib/scheduler/defer.rb @@ -76,6 +76,7 @@ module Scheduler def stop!(finish_work: false) if finish_work @finish = true + @queue.push({ finish: true }, force: true) @thread&.join end @thread.kill if @thread&.alive? @@ -108,7 +109,9 @@ module Scheduler # using non_block to match Ruby #deq def do_work(non_block = false) - db, job, desc = @queue.shift(block: !non_block).values_at(:db, :job, :desc) + db, job, desc, finish = @queue.shift(block: !non_block).values_at(:db, :job, :desc, :finish) + + return if finish start = Process.clock_gettime(Process::CLOCK_MONOTONIC) db ||= RailsMultisite::ConnectionManagement::DEFAULT diff --git a/spec/lib/scheduler/defer_spec.rb b/spec/lib/scheduler/defer_spec.rb index 2de303da1f2..fbee4b8dcd3 100644 --- a/spec/lib/scheduler/defer_spec.rb +++ b/spec/lib/scheduler/defer_spec.rb @@ -22,6 +22,12 @@ RSpec.describe Scheduler::Defer do Discourse.reset_catch_job_exceptions! end + it "can finish work properly without crashing" do + @defer.later {} + sleep 0.005 + @defer.stop!(finish_work: true) + end + it "supports basic instrumentation" do @defer.later("first") {} @defer.later("first") {}