Skip to content

Commit

Permalink
Merge pull request #37 from basecamp/silence-polling
Browse files Browse the repository at this point in the history
Add option to silence the polling logs
  • Loading branch information
djmb authored Nov 13, 2023
2 parents d8413ad + 970f9ed commit 11cdc67
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/solid_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ module SolidQueue

mattr_accessor :shutdown_timeout, default: 5.seconds

mattr_accessor :silence_polling, default: false

mattr_accessor :supervisor_pidfile
mattr_accessor :supervisor, default: false

def self.supervisor?
supervisor
end

def self.silence_polling?
silence_polling
end
end
1 change: 1 addition & 0 deletions lib/solid_queue/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Engine < ::Rails::Engine
SolidQueue.process_heartbeat_interval = app.config.solid_queue.process_heartbeat_interval || 60.seconds
SolidQueue.process_alive_threshold = app.config.solid_queue.process_alive_threshold || 5.minutes
SolidQueue.shutdown_timeout = app.config.solid_queue.shutdown_timeout || 5.seconds
SolidQueue.silence_polling = app.config.solid_queue.silence_polling || false
SolidQueue.supervisor_pidfile = app.config.solid_queue.supervisor_pidfile || app.root.join("tmp", "pids", "solid_queue_supervisor.pid")
end
end
Expand Down
12 changes: 11 additions & 1 deletion lib/solid_queue/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def initialize(**options)

private
def run
claimed_executions = SolidQueue::ReadyExecution.claim(queues, pool.idle_threads, process.id)
claimed_executions = with_polling_volume do
SolidQueue::ReadyExecution.claim(queues, pool.idle_threads, process.id)
end

if claimed_executions.size > 0
procline "performing #{claimed_executions.count} jobs in #{queues}"
Expand Down Expand Up @@ -44,5 +46,13 @@ 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
30 changes: 29 additions & 1 deletion test/unit/worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class WorkerTest < ActiveSupport::TestCase
include ActiveSupport::Testing::MethodCallAssertions

setup do
@worker = SolidQueue::Worker.new(queues: "background", threads: 3, polling_interval: 10)
@worker = SolidQueue::Worker.new(queues: "background", threads: 3, polling_interval: 4)
end

teardown do
Expand Down Expand Up @@ -52,4 +52,32 @@ class WorkerTest < ActiveSupport::TestCase
assert_equal 5, JobResult.where(queue_name: :background, status: "completed", value: :paused).count
assert_equal 3, JobResult.where(queue_name: :background, status: "completed", value: :immediate).count
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

@worker.start(mode: :async)
sleep 0.5

assert_match /SELECT .* FROM .solid_queue_ready_executions. WHERE \(.solid_queue_ready_executions...queue_name./, 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

@worker.start(mode: :async)
sleep 0.5

assert_no_match /SELECT .* FROM .solid_queue_ready_executions. WHERE \(.solid_queue_ready_executions...queue_name./, log.string
ensure
ActiveRecord::Base.logger = old_logger
SolidQueue.silence_polling = old_silence_polling
end
end

0 comments on commit 11cdc67

Please sign in to comment.