From a95855b09b960c93f1e27440ff2ba8ab91936569 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Tue, 14 Nov 2023 08:46:28 +0000 Subject: [PATCH] Silence scheduler polling Use SolidQueue.silence_polling to also silence the scheduler. --- lib/solid_queue/runner.rb | 8 +++++++ lib/solid_queue/scheduler.rb | 16 ++++++++------ lib/solid_queue/worker.rb | 8 ------- test/unit/scheduler_test.rb | 42 ++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 test/unit/scheduler_test.rb diff --git a/lib/solid_queue/runner.rb b/lib/solid_queue/runner.rb index 3f513e23..3cfcde8b 100644 --- a/lib/solid_queue/runner.rb +++ b/lib/solid_queue/runner.rb @@ -104,5 +104,13 @@ def all_work_completed? def running_inline? mode.inline? end + + def with_polling_volume + if SolidQueue.silence_polling? + ActiveRecord::Base.logger.silence { yield } + else + yield + end + end end end diff --git a/lib/solid_queue/scheduler.rb b/lib/solid_queue/scheduler.rb index 5b76e6ca..e122e3fb 100644 --- a/lib/solid_queue/scheduler.rb +++ b/lib/solid_queue/scheduler.rb @@ -14,15 +14,17 @@ def initialize(**options) private def run - batch = SolidQueue::ScheduledExecution.next_batch(batch_size) + with_polling_volume do + batch = SolidQueue::ScheduledExecution.next_batch(batch_size) - if batch.size > 0 - procline "preparing #{batch.size} jobs for execution" + if batch.size > 0 + procline "preparing #{batch.size} jobs for execution" - SolidQueue::ScheduledExecution.prepare_batch(batch) - else - procline "waiting" - interruptible_sleep(polling_interval) + SolidQueue::ScheduledExecution.prepare_batch(batch) + else + procline "waiting" + interruptible_sleep(polling_interval) + end end end diff --git a/lib/solid_queue/worker.rb b/lib/solid_queue/worker.rb index 2529cf2a..61eeb783 100644 --- a/lib/solid_queue/worker.rb +++ b/lib/solid_queue/worker.rb @@ -46,13 +46,5 @@ def all_work_completed? def metadata super.merge(queues: queues, thread_pool_size: pool.size, idle_threads: pool.idle_threads, polling_interval: polling_interval) end - - def with_polling_volume - if SolidQueue.silence_polling? - ActiveRecord::Base.logger.silence { yield } - else - yield - end - end end end diff --git a/test/unit/scheduler_test.rb b/test/unit/scheduler_test.rb new file mode 100644 index 00000000..e7036404 --- /dev/null +++ b/test/unit/scheduler_test.rb @@ -0,0 +1,42 @@ +require "test_helper" +require "active_support/testing/method_call_assertions" + +class SchedulerTest < ActiveSupport::TestCase + include ActiveSupport::Testing::MethodCallAssertions + + setup do + @scheduler = SolidQueue::Scheduler.new(polling_interval: 0.1, batch_size: 10) + end + + teardown do + @scheduler.stop if @scheduler.running? + end + + test "polling queries are logged" do + log = StringIO.new + old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(log) + old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, false + + @scheduler.start(mode: :async) + sleep 0.5 + + assert_match /SELECT .* FROM .solid_queue_scheduled_executions. WHERE/, log.string + ensure + ActiveRecord::Base.logger = old_logger + SolidQueue.silence_polling = old_silence_polling + end + + test "polling queries can be silenced" do + log = StringIO.new + old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(log) + old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, true + + @scheduler.start(mode: :async) + sleep 0.5 + + assert_no_match /SELECT .* FROM .solid_queue_scheduled_executions. WHERE/, log.string + ensure + ActiveRecord::Base.logger = old_logger + SolidQueue.silence_polling = old_silence_polling + end +end