1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/execution_wrapper.rb
Matthew Draper 291a098c11 Directly support stateful executor hooks
Also, make sure to call the +complete+ hooks if +run+ fails.
2016-04-05 06:57:01 +09:30

117 lines
2.6 KiB
Ruby

require 'active_support/callbacks'
module ActiveSupport
class ExecutionWrapper
include ActiveSupport::Callbacks
Null = Object.new # :nodoc:
def Null.complete! # :nodoc:
end
define_callbacks :run
define_callbacks :complete
def self.to_run(*args, &block)
set_callback(:run, *args, &block)
end
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
# Run this execution.
#
# Returns an instance, whose +complete!+ method *must* be invoked
# after the work has been performed.
#
# Where possible, prefer +wrap+.
def self.run!
if active?
Null
else
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?
instance = run!
begin
yield
ensure
instance.complete!
end
end
class << self # :nodoc:
attr_accessor :active
end
def self.inherited(other) # :nodoc:
super
other.active = Concurrent::Hash.new
end
self.active = Concurrent::Hash.new
def self.active? # :nodoc:
@active[Thread.current]
end
def run! # :nodoc:
self.class.active[Thread.current] = true
run_callbacks(:run)
end
# Complete this in-flight execution. This method *must* be called
# exactly once on the result of any call to +run!+.
#
# Where possible, prefer +wrap+.
def complete!
run_callbacks(:complete)
ensure
self.class.active.delete Thread.current
end
private
def hook_state
@_hook_state ||= {}
end
end
end