Skip to content

Commit dbda4e5

Browse files
committed
Generalize silence_polling to silence_queries
Broadens the existing `silence_polling` config option into a unified `silence_queries` that silences all of Solid Queue's internal SQL logging — not just polling, but also heartbeats, concurrency maintenance, process pruning, and scheduler dynamic task reloading. This addresses the long-standing request in rails#210 and aligns with the direction outlined in rails#389 (review comment): a single toggle instead of accumulating per-feature silencing flags. `silence_polling` remains as a backward-compatible alias.
1 parent 176721e commit dbda4e5

10 files changed

Lines changed: 102 additions & 31 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ All the options available to Active Record for multiple databases can be used he
385385

386386
### Other configuration settings
387387

388-
_Note_: The settings in this section should be set in your `config/application.rb` or your environment config like this: `config.solid_queue.silence_polling = true`
388+
_Note_: The settings in this section should be set in your `config/application.rb` or your environment config like this: `config.solid_queue.silence_queries = true`
389389

390390
There are several settings that control how Solid Queue works that you can set as well:
391391
- `logger`: the logger you want Solid Queue to use. Defaults to the app logger.
@@ -402,7 +402,7 @@ There are several settings that control how Solid Queue works that you can set a
402402
- `process_heartbeat_interval`: the heartbeat interval that all processes will follow—defaults to 60 seconds.
403403
- `process_alive_threshold`: how long to wait until a process is considered dead after its last heartbeat—defaults to 5 minutes.
404404
- `shutdown_timeout`: time the supervisor will wait since it sent the `TERM` signal to its supervised processes before sending a `QUIT` version to them requesting immediate termination—defaults to 5 seconds.
405-
- `silence_polling`: whether to silence Active Record logs emitted when polling for both workers and dispatchers—defaults to `true`.
405+
- `silence_queries`: whether to silence Active Record logs emitted by Solid Queue's internal queries—including polling, heartbeats, concurrency maintenance, and process pruning—defaults to `true`. For backward compatibility, `silence_polling` is still supported as an alias.
406406
- `supervisor_pidfile`: path to a pidfile that the supervisor will create when booting to prevent running more than one supervisor in the same host, or in case you want to use it for a health check. It's `nil` by default.
407407
- `preserve_finished_jobs`: whether to keep finished jobs in the `solid_queue_jobs` table—defaults to `true`.
408408
- `clear_finished_jobs_after`: period to keep finished jobs around, in case `preserve_finished_jobs` is true — defaults to 1 day. When installing Solid Queue, [a recurring job](#recurring-tasks) is automatically configured to clear finished jobs every hour on the 12th minute in batches. You can edit the `recurring.yml` configuration to change this as you see fit.

lib/solid_queue.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module SolidQueue
3232

3333
mattr_accessor :shutdown_timeout, default: 5.seconds
3434

35-
mattr_accessor :silence_polling, default: true
35+
mattr_accessor :silence_queries, default: true
3636

3737
mattr_accessor :supervisor_pidfile
3838
mattr_accessor :supervisor, default: false
@@ -69,8 +69,21 @@ def supervisor?
6969
supervisor
7070
end
7171

72+
def silence_queries?
73+
silence_queries
74+
end
75+
76+
# Backward compatibility: silence_polling is now an alias for silence_queries
77+
def silence_polling=(value)
78+
self.silence_queries = value
79+
end
80+
81+
def silence_polling
82+
silence_queries
83+
end
84+
7285
def silence_polling?
73-
silence_polling
86+
silence_queries?
7487
end
7588

7689
def preserve_finished_jobs?

lib/solid_queue/app_executor.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ def wrap_in_app_executor(&block)
1010
end
1111
end
1212

13+
def with_silenced_queries
14+
if SolidQueue.silence_queries? && ActiveRecord::Base.logger
15+
ActiveRecord::Base.logger.silence { yield }
16+
else
17+
yield
18+
end
19+
end
20+
1321
def handle_thread_error(error)
1422
SolidQueue.instrument(:thread_error, error: error)
1523

lib/solid_queue/dispatcher/concurrency_maintenance.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ def stop
3131
private
3232
def expire_semaphores
3333
wrap_in_app_executor do
34-
Semaphore.expired.in_batches(of: batch_size, &:delete_all)
34+
with_silenced_queries do
35+
Semaphore.expired.in_batches(of: batch_size, &:delete_all)
36+
end
3537
end
3638
end
3739

3840
def unblock_blocked_executions
3941
wrap_in_app_executor do
40-
BlockedExecution.unblock(batch_size)
42+
with_silenced_queries do
43+
BlockedExecution.unblock(batch_size)
44+
end
4145
end
4246
end
4347
end

lib/solid_queue/processes/poller.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ def poll
4343

4444
def with_polling_volume
4545
SolidQueue.instrument(:polling) do
46-
if SolidQueue.silence_polling? && ActiveRecord::Base.logger
47-
ActiveRecord::Base.logger.silence { yield }
48-
else
49-
yield
50-
end
46+
with_silenced_queries { yield }
5147
end
5248
end
5349
end

lib/solid_queue/processes/registrable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def registered?
3939

4040
def launch_heartbeat
4141
@heartbeat_task = Concurrent::TimerTask.new(execution_interval: SolidQueue.process_heartbeat_interval) do
42-
wrap_in_app_executor { heartbeat }
42+
wrap_in_app_executor { with_silenced_queries { heartbeat } }
4343
end
4444

4545
@heartbeat_task.add_observer do |_, _, error|
@@ -61,7 +61,7 @@ def heartbeat
6161
end
6262

6363
def reload_metadata
64-
wrap_in_app_executor { process&.update(metadata: metadata.compact) }
64+
wrap_in_app_executor { with_silenced_queries { process&.update(metadata: metadata.compact) } }
6565
end
6666
end
6767
end

lib/solid_queue/scheduler/recurring_schedule.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ def task_keys
4848

4949
def reschedule_dynamic_tasks
5050
wrap_in_app_executor do
51-
reload_dynamic_tasks
52-
schedule_created_dynamic_tasks
53-
unschedule_deleted_dynamic_tasks
51+
with_silenced_queries do
52+
reload_dynamic_tasks
53+
schedule_created_dynamic_tasks
54+
unschedule_deleted_dynamic_tasks
55+
end
5456
end
5557
end
5658

lib/solid_queue/supervisor/maintenance.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def stop_maintenance_task
2424
end
2525

2626
def prune_dead_processes
27-
wrap_in_app_executor { SolidQueue::Process.prune(excluding: process) }
27+
wrap_in_app_executor { with_silenced_queries { SolidQueue::Process.prune(excluding: process) } }
2828
end
2929

3030
def fail_orphaned_executions

test/unit/dispatcher_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class DispatcherTest < ActiveSupport::TestCase
3939
test "polling queries are logged" do
4040
log = StringIO.new
4141
with_active_record_logger(ActiveSupport::Logger.new(log)) do
42-
with_polling(silence: false) do
42+
with_silence_queries(false) do
4343
rewind_io(log)
4444
@dispatcher.start
4545
sleep 0.5.second
@@ -52,7 +52,7 @@ class DispatcherTest < ActiveSupport::TestCase
5252
test "polling queries can be silenced" do
5353
log = StringIO.new
5454
with_active_record_logger(ActiveSupport::Logger.new(log)) do
55-
with_polling(silence: true) do
55+
with_silence_queries(true) do
5656
rewind_io(log)
5757
@dispatcher.start
5858
sleep 0.5.second
@@ -62,9 +62,9 @@ class DispatcherTest < ActiveSupport::TestCase
6262
assert_no_match /SELECT .* FROM .solid_queue_scheduled_executions. WHERE/, log.string
6363
end
6464

65-
test "silencing polling queries when there's no Active Record logger" do
65+
test "silencing queries when there's no Active Record logger" do
6666
with_active_record_logger(nil) do
67-
with_polling(silence: true) do
67+
with_silence_queries(true) do
6868
@dispatcher.start
6969
sleep 0.5.second
7070
end
@@ -131,11 +131,11 @@ class DispatcherTest < ActiveSupport::TestCase
131131
end
132132

133133
private
134-
def with_polling(silence:)
135-
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, silence
134+
def with_silence_queries(silence)
135+
old_silence_queries, SolidQueue.silence_queries = SolidQueue.silence_queries, silence
136136
yield
137137
ensure
138-
SolidQueue.silence_polling = old_silence_polling
138+
SolidQueue.silence_queries = old_silence_queries
139139
end
140140

141141
def with_active_record_logger(logger)

test/unit/worker_test.rb

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class WorkerTest < ActiveSupport::TestCase
106106
test "polling queries are logged" do
107107
log = StringIO.new
108108
with_active_record_logger(ActiveSupport::Logger.new(log)) do
109-
with_polling(silence: false) do
109+
with_silence_queries(false) do
110110
@worker.start
111111
sleep 0.2
112112
end
@@ -118,7 +118,7 @@ class WorkerTest < ActiveSupport::TestCase
118118
test "polling queries can be silenced" do
119119
log = StringIO.new
120120
with_active_record_logger(ActiveSupport::Logger.new(log)) do
121-
with_polling(silence: true) do
121+
with_silence_queries(true) do
122122
@worker.start
123123
sleep 0.2
124124
end
@@ -127,9 +127,9 @@ class WorkerTest < ActiveSupport::TestCase
127127
assert_no_match /SELECT .* FROM .solid_queue_ready_executions. WHERE .solid_queue_ready_executions...queue_name./, log.string
128128
end
129129

130-
test "silencing polling queries when there's no Active Record logger" do
130+
test "silencing queries when there's no Active Record logger" do
131131
with_active_record_logger(nil) do
132-
with_polling(silence: true) do
132+
with_silence_queries(true) do
133133
@worker.start
134134
sleep 0.2
135135
end
@@ -140,6 +140,54 @@ class WorkerTest < ActiveSupport::TestCase
140140
assert_no_registered_processes
141141
end
142142

143+
test "heartbeat queries can be silenced" do
144+
old_heartbeat_interval, SolidQueue.process_heartbeat_interval = SolidQueue.process_heartbeat_interval, 0.2.second
145+
146+
log = StringIO.new
147+
with_active_record_logger(ActiveSupport::Logger.new(log)) do
148+
with_silence_queries(true) do
149+
@worker.start
150+
wait_for_registered_processes(1, timeout: 1.second)
151+
sleep 0.5
152+
end
153+
end
154+
155+
assert_no_match /UPDATE .solid_queue_processes. SET .last_heartbeat_at./, log.string
156+
ensure
157+
SolidQueue.process_heartbeat_interval = old_heartbeat_interval
158+
end
159+
160+
test "heartbeat queries are logged when silence_queries is false" do
161+
old_heartbeat_interval, SolidQueue.process_heartbeat_interval = SolidQueue.process_heartbeat_interval, 0.2.second
162+
163+
log = StringIO.new
164+
with_active_record_logger(ActiveSupport::Logger.new(log)) do
165+
with_silence_queries(false) do
166+
@worker.start
167+
wait_for_registered_processes(1, timeout: 1.second)
168+
sleep 0.5
169+
end
170+
end
171+
172+
assert_match /UPDATE .solid_queue_processes. SET .last_heartbeat_at./, log.string
173+
ensure
174+
SolidQueue.process_heartbeat_interval = old_heartbeat_interval
175+
end
176+
177+
test "silence_polling is backward compatible with silence_queries" do
178+
old_value = SolidQueue.silence_queries
179+
180+
SolidQueue.silence_polling = false
181+
assert_not SolidQueue.silence_queries?
182+
assert_not SolidQueue.silence_polling?
183+
184+
SolidQueue.silence_polling = true
185+
assert SolidQueue.silence_queries?
186+
assert SolidQueue.silence_polling?
187+
ensure
188+
SolidQueue.silence_queries = old_value
189+
end
190+
143191
test "run inline" do
144192
worker = SolidQueue::Worker.new(queues: "*", threads: 3, polling_interval: 0.2)
145193
worker.mode = :inline
@@ -195,11 +243,11 @@ class WorkerTest < ActiveSupport::TestCase
195243
end
196244

197245
private
198-
def with_polling(silence:)
199-
old_silence_polling, SolidQueue.silence_polling = SolidQueue.silence_polling, silence
246+
def with_silence_queries(silence)
247+
old_silence_queries, SolidQueue.silence_queries = SolidQueue.silence_queries, silence
200248
yield
201249
ensure
202-
SolidQueue.silence_polling = old_silence_polling
250+
SolidQueue.silence_queries = old_silence_queries
203251
end
204252

205253
def with_active_record_logger(logger)

0 commit comments

Comments
 (0)