-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up after a failure in a run callback #24422
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,32 @@ def self.to_complete(*args, &block) | |
set_callback(:complete, *args, &block) | ||
end | ||
|
||
# Register an object to be invoked during both the +run+ and | ||
# +complete+ steps. | ||
# | ||
# +hook.complete+ will be passed the value returned from +hook.run+, | ||
# and will only be invoked if +run+ has previously been called. | ||
# (Mostly, this means it won't be invoked if an exception occurs in | ||
# a preceding +to_run+ block; all ordinary +to_complete+ blocks are | ||
# invoked in that situation.) | ||
def self.register_hook(hook, outer: false) | ||
if outer | ||
run_args = [prepend: true] | ||
complete_args = [:after] | ||
else | ||
run_args = complete_args = [] | ||
end | ||
|
||
to_run(*run_args) do | ||
hook_state[hook] = hook.run | ||
end | ||
to_complete(*complete_args) do | ||
if hook_state.key?(hook) | ||
hook.complete hook_state[hook] | ||
end | ||
end | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice API. The |
||
# Run this execution. | ||
# | ||
# Returns an instance, whose +complete!+ method *must* be invoked | ||
|
@@ -29,19 +55,27 @@ def self.run! | |
if active? | ||
Null | ||
else | ||
new.tap(&:run!) | ||
new.tap do |instance| | ||
success = nil | ||
begin | ||
instance.run! | ||
success = true | ||
ensure | ||
instance.complete! unless success | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Perform the work in the supplied block as an execution. | ||
def self.wrap | ||
return yield if active? | ||
|
||
state = run! | ||
instance = run! | ||
begin | ||
yield | ||
ensure | ||
state.complete! | ||
instance.complete! | ||
end | ||
end | ||
|
||
|
@@ -74,5 +108,10 @@ def complete! | |
ensure | ||
self.class.active.delete Thread.current | ||
end | ||
|
||
private | ||
def hook_state | ||
@_hook_state ||= {} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
require 'abstract_unit' | ||
|
||
class ExecutorTest < ActiveSupport::TestCase | ||
class DummyError < RuntimeError | ||
end | ||
|
||
def test_wrap_invokes_callbacks | ||
called = [] | ||
executor.to_run { called << :run } | ||
|
@@ -35,6 +38,20 @@ def test_separated_calls_invoke_callbacks | |
assert_equal [:run, :body, :complete], called | ||
end | ||
|
||
def test_exceptions_unwind | ||
called = [] | ||
executor.to_run { called << :run_1 } | ||
executor.to_run { raise DummyError } | ||
executor.to_run { called << :run_2 } | ||
executor.to_complete { called << :complete } | ||
|
||
assert_raises(DummyError) do | ||
executor.wrap { called << :body } | ||
end | ||
|
||
assert_equal [:run_1, :complete], called | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤘 |
||
|
||
def test_avoids_double_wrapping | ||
called = [] | ||
executor.to_run { called << :run } | ||
|
@@ -51,6 +68,96 @@ def test_avoids_double_wrapping | |
assert_equal [:run, :early, :body, :late, :complete], called | ||
end | ||
|
||
def test_hooks_carry_state | ||
supplied_state = :none | ||
|
||
hook = Class.new do | ||
define_method(:run) do | ||
:some_state | ||
end | ||
|
||
define_method(:complete) do |state| | ||
supplied_state = state | ||
end | ||
end.new | ||
|
||
executor.register_hook(hook) | ||
|
||
executor.wrap { } | ||
|
||
assert_equal :some_state, supplied_state | ||
end | ||
|
||
def test_nil_state_is_sufficient | ||
supplied_state = :none | ||
|
||
hook = Class.new do | ||
define_method(:run) do | ||
nil | ||
end | ||
|
||
define_method(:complete) do |state| | ||
supplied_state = state | ||
end | ||
end.new | ||
|
||
executor.register_hook(hook) | ||
|
||
executor.wrap { } | ||
|
||
assert_equal nil, supplied_state | ||
end | ||
|
||
def test_exception_skips_uninvoked_hook | ||
supplied_state = :none | ||
|
||
hook = Class.new do | ||
define_method(:run) do | ||
:some_state | ||
end | ||
|
||
define_method(:complete) do |state| | ||
supplied_state = state | ||
end | ||
end.new | ||
|
||
executor.to_run do | ||
raise DummyError | ||
end | ||
executor.register_hook(hook) | ||
|
||
assert_raises(DummyError) do | ||
executor.wrap { } | ||
end | ||
|
||
assert_equal :none, supplied_state | ||
end | ||
|
||
def test_exception_unwinds_invoked_hook | ||
supplied_state = :none | ||
|
||
hook = Class.new do | ||
define_method(:run) do | ||
:some_state | ||
end | ||
|
||
define_method(:complete) do |state| | ||
supplied_state = state | ||
end | ||
end.new | ||
|
||
executor.register_hook(hook) | ||
executor.to_run do | ||
raise DummyError | ||
end | ||
|
||
assert_raises(DummyError) do | ||
executor.wrap { } | ||
end | ||
|
||
assert_equal :some_state, supplied_state | ||
end | ||
|
||
def test_separate_classes_can_wrap | ||
other_executor = Class.new(ActiveSupport::Executor) | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question about this. is
run
executed when the application boots?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No; the executor calls
run
/complete
strictly in pairs around "user" code.