Skip to content

Commit

Permalink
Merge pull request #41 from basecamp/silence-scheduled-polling
Browse files Browse the repository at this point in the history
Silence scheduler polling
  • Loading branch information
djmb authored Nov 15, 2023
2 parents 11cdc67 + a95855b commit a471237
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
8 changes: 8 additions & 0 deletions lib/solid_queue/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 9 additions & 7 deletions lib/solid_queue/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 0 additions & 8 deletions lib/solid_queue/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions test/unit/scheduler_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a471237

Please sign in to comment.