-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from rails/start-stop-callbacks
Implement `start` and `stop` lifecycle hooks for the supervisor and workers
- Loading branch information
Showing
8 changed files
with
154 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidQueue | ||
module LifecycleHooks | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
mattr_reader :lifecycle_hooks, default: { start: [], stop: [] } | ||
end | ||
|
||
class_methods do | ||
def on_start(&block) | ||
self.lifecycle_hooks[:start] << block | ||
end | ||
|
||
def on_stop(&block) | ||
self.lifecycle_hooks[:stop] << block | ||
end | ||
|
||
def clear_hooks | ||
self.lifecycle_hooks[:start] = [] | ||
self.lifecycle_hooks[:stop] = [] | ||
end | ||
end | ||
|
||
private | ||
def run_start_hooks | ||
run_hooks_for :start | ||
end | ||
|
||
def run_stop_hooks | ||
run_hooks_for :stop | ||
end | ||
|
||
def run_hooks_for(event) | ||
self.class.lifecycle_hooks.fetch(event, []).each do |block| | ||
block.call | ||
rescue Exception => exception | ||
handle_thread_error(exception) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class LifecycleHooksTest < ActiveSupport::TestCase | ||
self.use_transactional_tests = false | ||
|
||
test "run lifecycle hooks" do | ||
SolidQueue.on_start { JobResult.create!(status: :hook_called, value: :start) } | ||
SolidQueue.on_stop { JobResult.create!(status: :hook_called, value: :stop) } | ||
|
||
SolidQueue.on_worker_start { JobResult.create!(status: :hook_called, value: :worker_start) } | ||
SolidQueue.on_worker_stop { JobResult.create!(status: :hook_called, value: :worker_stop) } | ||
|
||
pid = run_supervisor_as_fork(load_configuration_from: { workers: [ { queues: "*" } ] }) | ||
wait_for_registered_processes(4) | ||
|
||
terminate_process(pid) | ||
wait_for_registered_processes(0) | ||
|
||
results = skip_active_record_query_cache do | ||
assert_equal 4, JobResult.count | ||
JobResult.last(4) | ||
end | ||
|
||
assert_equal "hook_called", results.map(&:status).first | ||
assert_equal [ "start", "stop", "worker_start", "worker_stop" ], results.map(&:value).sort | ||
ensure | ||
SolidQueue::Supervisor.clear_hooks | ||
SolidQueue::Worker.clear_hooks | ||
end | ||
|
||
test "handle errors on lifecycle hooks" do | ||
previous_on_thread_error, SolidQueue.on_thread_error = SolidQueue.on_thread_error, ->(error) { JobResult.create!(status: :error, value: error.message) } | ||
SolidQueue.on_start { raise RuntimeError, "everything is broken" } | ||
|
||
pid = run_supervisor_as_fork | ||
wait_for_registered_processes(4) | ||
|
||
terminate_process(pid) | ||
wait_for_registered_processes(0) | ||
|
||
result = skip_active_record_query_cache { JobResult.last } | ||
|
||
assert_equal "error", result.status | ||
assert_equal "everything is broken", result.value | ||
ensure | ||
SolidQueue.on_thread_error = previous_on_thread_error | ||
SolidQueue::Supervisor.clear_hooks | ||
SolidQueue::Worker.clear_hooks | ||
end | ||
end |
File renamed without changes.