2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/callbacks"
|
2016-02-21 20:25:52 -05:00
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
class ExecutionWrapper
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
2016-03-03 09:04:29 -05:00
|
|
|
Null = Object.new # :nodoc:
|
|
|
|
def Null.complete! # :nodoc:
|
|
|
|
end
|
|
|
|
|
2016-02-21 20:25:52 -05:00
|
|
|
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
|
|
|
|
|
2017-01-13 01:09:56 -05:00
|
|
|
RunHook = Struct.new(:hook) do # :nodoc:
|
2016-10-02 11:04:27 -04:00
|
|
|
def before(target)
|
|
|
|
hook_state = target.send(:hook_state)
|
|
|
|
hook_state[hook] = hook.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-13 01:09:56 -05:00
|
|
|
CompleteHook = Struct.new(:hook) do # :nodoc:
|
2016-10-02 11:04:27 -04:00
|
|
|
def before(target)
|
|
|
|
hook_state = target.send(:hook_state)
|
|
|
|
if hook_state.key?(hook)
|
|
|
|
hook.complete hook_state[hook]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias after before
|
|
|
|
end
|
|
|
|
|
2016-04-04 16:41:28 -04:00
|
|
|
# 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
|
2016-10-02 11:04:27 -04:00
|
|
|
to_run RunHook.new(hook), prepend: true
|
|
|
|
to_complete :after, CompleteHook.new(hook)
|
2016-04-04 16:41:28 -04:00
|
|
|
else
|
2016-10-02 11:04:27 -04:00
|
|
|
to_run RunHook.new(hook)
|
|
|
|
to_complete CompleteHook.new(hook)
|
2016-04-04 16:41:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-21 20:25:52 -05:00
|
|
|
# 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!
|
2016-03-03 09:04:29 -05:00
|
|
|
if active?
|
|
|
|
Null
|
|
|
|
else
|
2016-04-04 16:41:28 -04:00
|
|
|
new.tap do |instance|
|
|
|
|
success = nil
|
|
|
|
begin
|
|
|
|
instance.run!
|
|
|
|
success = true
|
|
|
|
ensure
|
|
|
|
instance.complete! unless success
|
|
|
|
end
|
|
|
|
end
|
2016-03-03 09:04:29 -05:00
|
|
|
end
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Perform the work in the supplied block as an execution.
|
|
|
|
def self.wrap
|
|
|
|
return yield if active?
|
|
|
|
|
2016-04-04 16:41:28 -04:00
|
|
|
instance = run!
|
2016-02-21 20:25:52 -05:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2016-04-04 16:41:28 -04:00
|
|
|
instance.complete!
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self # :nodoc:
|
|
|
|
attr_accessor :active
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.inherited(other) # :nodoc:
|
|
|
|
super
|
2016-03-03 09:04:29 -05:00
|
|
|
other.active = Concurrent::Hash.new
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
|
|
|
|
2016-03-03 09:04:29 -05:00
|
|
|
self.active = Concurrent::Hash.new
|
2016-02-21 20:25:52 -05:00
|
|
|
|
|
|
|
def self.active? # :nodoc:
|
2016-03-03 09:04:29 -05:00
|
|
|
@active[Thread.current]
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def run! # :nodoc:
|
2016-03-03 09:04:29 -05:00
|
|
|
self.class.active[Thread.current] = true
|
2016-02-21 20:25:52 -05:00
|
|
|
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)
|
2016-03-03 09:04:29 -05:00
|
|
|
ensure
|
|
|
|
self.class.active.delete Thread.current
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
2016-04-04 16:41:28 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def hook_state
|
|
|
|
@_hook_state ||= {}
|
|
|
|
end
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
|
|
|
end
|