Skip to content

Commit 8d1e27c

Browse files
committed
Refactor a bit the Poller vs. Runnable modules
Make the loop be part of Poller. Allow to have other other Runnable processes that don't need an infinite loop. I'm still not super happy with these concerns. This needs more work that will come when I properly implement async mode. Right now this is all interleaved in the modules and it shouldn't be.
1 parent e104a5f commit 8d1e27c

File tree

5 files changed

+59
-41
lines changed

5 files changed

+59
-41
lines changed

lib/solid_queue/dispatcher.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module SolidQueue
44
class Dispatcher < Processes::Base
5-
include Processes::Runnable, Processes::Poller
5+
include Processes::Poller
66

77
attr_accessor :batch_size, :concurrency_maintenance, :recurring_schedule
88

@@ -20,13 +20,9 @@ def initialize(**options)
2020
end
2121

2222
private
23-
def run
23+
def poll
2424
batch = dispatch_next_batch
25-
26-
unless batch.size > 0
27-
procline "waiting"
28-
interruptible_sleep(polling_interval)
29-
end
25+
batch.size
3026
end
3127

3228
def dispatch_next_batch
@@ -51,6 +47,10 @@ def unload_recurring_schedule
5147
recurring_schedule.unload_tasks
5248
end
5349

50+
def set_procline
51+
procline "waiting"
52+
end
53+
5454
def metadata
5555
super.merge(batch_size: batch_size, concurrency_maintenance_interval: concurrency_maintenance&.interval, recurring_schedule: recurring_schedule.tasks.presence )
5656
end

lib/solid_queue/processes/poller.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,39 @@ module SolidQueue::Processes
44
module Poller
55
extend ActiveSupport::Concern
66

7+
include Runnable
8+
79
included do
810
attr_accessor :polling_interval
911
end
1012

1113
private
14+
def run
15+
if mode.async?
16+
@thread = Thread.new { start_loop }
17+
else
18+
start_loop
19+
end
20+
end
21+
22+
def start_loop
23+
loop do
24+
break if shutting_down?
25+
26+
wrap_in_app_executor do
27+
unless poll > 0
28+
interruptible_sleep(polling_interval)
29+
end
30+
end
31+
end
32+
ensure
33+
run_callbacks(:shutdown) { shutdown }
34+
end
35+
36+
def poll
37+
raise NotImplementedError
38+
end
39+
1240
def with_polling_volume
1341
if SolidQueue.silence_polling?
1442
ActiveRecord::Base.logger.silence { yield }

lib/solid_queue/processes/runnable.rb

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module Runnable
88

99
def start
1010
@stopping = false
11-
1211
run_callbacks(:boot) { boot }
13-
start_loop
12+
13+
run
1414
end
1515

1616
def stop
@@ -26,28 +26,12 @@ def mode
2626
end
2727

2828
def boot
29-
register_signal_handlers if supervised?
30-
SolidQueue.logger.info("[SolidQueue] Starting #{self}")
31-
end
32-
33-
def start_loop
34-
if mode.async?
35-
@thread = Thread.new { do_start_loop }
36-
else
37-
do_start_loop
29+
if supervised?
30+
register_signal_handlers
31+
set_procline
3832
end
39-
end
4033

41-
def do_start_loop
42-
loop do
43-
break if shutting_down?
44-
45-
wrap_in_app_executor do
46-
run
47-
end
48-
end
49-
ensure
50-
run_callbacks(:shutdown) { shutdown }
34+
SolidQueue.logger.info("[SolidQueue] Starting #{self}")
5135
end
5236

5337
def shutting_down?
@@ -70,6 +54,9 @@ def all_work_completed?
7054
false
7155
end
7256

57+
def set_procline
58+
end
59+
7360
def running_inline?
7461
mode.inline?
7562
end

lib/solid_queue/processes/supervised.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def supervised_by(process)
1414
end
1515

1616
private
17+
def set_procline
18+
procline "waiting"
19+
end
20+
1721
def supervisor_went_away?
1822
supervised? && supervisor&.pid != ::Process.ppid
1923
end

lib/solid_queue/worker.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module SolidQueue
44
class Worker < Processes::Base
5-
include Processes::Runnable, Processes::Poller
5+
include Processes::Poller
66

77
attr_accessor :queues, :pool
88

@@ -15,22 +15,17 @@ def initialize(**options)
1515
end
1616

1717
private
18-
def run
19-
polled_executions = poll
20-
21-
if polled_executions.size > 0
22-
procline "performing #{polled_executions.count} jobs"
23-
24-
polled_executions.each do |execution|
18+
def poll
19+
claim_executions.then do |executions|
20+
executions.each do |execution|
2521
pool.post(execution)
2622
end
27-
else
28-
procline "waiting for jobs in #{queues.join(",")}"
29-
interruptible_sleep(polling_interval)
23+
24+
executions.size
3025
end
3126
end
3227

33-
def poll
28+
def claim_executions
3429
with_polling_volume do
3530
SolidQueue::ReadyExecution.claim(queues, pool.idle_threads, process.id)
3631
end
@@ -47,6 +42,10 @@ def all_work_completed?
4742
SolidQueue::ReadyExecution.aggregated_count_across(queues).zero?
4843
end
4944

45+
def set_procline
46+
procline "waiting for jobs in #{queues.join(",")}"
47+
end
48+
5049
def metadata
5150
super.merge(queues: queues.join(","), thread_pool_size: pool.size)
5251
end

0 commit comments

Comments
 (0)