Skip to content

Commit dd2f0fe

Browse files
hmsrosa
authored andcommitted
Add lifecycle hooks to Dispatcher and Scheduler
I'm switching over to Rails Semantic Logger and for that to work with forking code it requires a post fork action. With this change, all 4 process types have start and stop hooks and it becomes possible to do on_*_start { post_fork_action } and on_*_stop { pre_stop_action }
1 parent 44e4be2 commit dd2f0fe

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

lib/solid_queue.rb

+16
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ def on_worker_stop(...)
5151
Worker.on_stop(...)
5252
end
5353

54+
def on_dispatcher_start(...)
55+
Dispatcher.on_start(...)
56+
end
57+
58+
def on_dispatcher_stop(...)
59+
Dispatcher.on_stop(...)
60+
end
61+
62+
def on_scheduler_start(...)
63+
Scheduler.on_start(...)
64+
end
65+
66+
def on_scheduler_stop(...)
67+
Scheduler.on_stop(...)
68+
end
69+
5470
def supervisor?
5571
supervisor
5672
end

lib/solid_queue/dispatcher.rb

+3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
module SolidQueue
44
class Dispatcher < Processes::Poller
5+
include LifecycleHooks
56
attr_accessor :batch_size, :concurrency_maintenance
67

8+
after_boot :run_start_hooks
79
after_boot :start_concurrency_maintenance
810
before_shutdown :stop_concurrency_maintenance
11+
after_shutdown :run_stop_hooks
912

1013
def initialize(**options)
1114
options = options.dup.with_defaults(SolidQueue::Configuration::DISPATCHER_DEFAULTS)

lib/solid_queue/scheduler.rb

+3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
module SolidQueue
44
class Scheduler < Processes::Base
55
include Processes::Runnable
6+
include LifecycleHooks
67

78
attr_accessor :recurring_schedule
89

10+
after_boot :run_start_hooks
911
after_boot :schedule_recurring_tasks
1012
before_shutdown :unschedule_recurring_tasks
13+
before_shutdown :run_stop_hooks
1114

1215
def initialize(recurring_tasks:, **options)
1316
@recurring_schedule = RecurringSchedule.new(recurring_tasks)

test/integration/lifecycle_hooks_test.rb

+15-5
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,30 @@ class LifecycleHooksTest < ActiveSupport::TestCase
1212
SolidQueue.on_worker_start { JobResult.create!(status: :hook_called, value: :worker_start) }
1313
SolidQueue.on_worker_stop { JobResult.create!(status: :hook_called, value: :worker_stop) }
1414

15-
pid = run_supervisor_as_fork(workers: [ { queues: "*" } ])
15+
SolidQueue.on_dispatcher_start { JobResult.create!(status: :hook_called, value: :dispatcher_start) }
16+
SolidQueue.on_dispatcher_stop { JobResult.create!(status: :hook_called, value: :dispatcher_stop) }
17+
18+
SolidQueue.on_scheduler_start { JobResult.create!(status: :hook_called, value: :scheduler_start) }
19+
SolidQueue.on_scheduler_stop { JobResult.create!(status: :hook_called, value: :scheduler_stop) }
20+
21+
pid = run_supervisor_as_fork(workers: [ { queues: "*" } ], dispatchers: [ { batch_size: 100 } ], skip_recurring: false)
1622
wait_for_registered_processes(4)
1723

1824
terminate_process(pid)
1925
wait_for_registered_processes(0)
2026

2127
results = skip_active_record_query_cache do
22-
assert_equal 4, JobResult.count
23-
JobResult.last(4)
28+
assert_equal 8, JobResult.count
29+
JobResult.last(8)
2430
end
2531

26-
assert_equal "hook_called", results.map(&:status).first
27-
assert_equal [ "start", "stop", "worker_start", "worker_stop" ], results.map(&:value).sort
32+
assert_equal({ "hook_called" => 8 }, results.map(&:status).tally)
33+
assert_equal %w[start stop worker_start worker_stop dispatcher_start dispatcher_stop scheduler_start scheduler_stop].sort, results.map(&:value).sort
2834
ensure
2935
SolidQueue::Supervisor.clear_hooks
3036
SolidQueue::Worker.clear_hooks
37+
SolidQueue::Dispatcher.clear_hooks
38+
SolidQueue::Scheduler.clear_hooks
3139
end
3240

3341
test "handle errors on lifecycle hooks" do
@@ -48,5 +56,7 @@ class LifecycleHooksTest < ActiveSupport::TestCase
4856
SolidQueue.on_thread_error = previous_on_thread_error
4957
SolidQueue::Supervisor.clear_hooks
5058
SolidQueue::Worker.clear_hooks
59+
SolidQueue::Dispatcher.clear_hooks
60+
SolidQueue::Scheduler.clear_hooks
5161
end
5262
end

0 commit comments

Comments
 (0)