class ActiveSupport::ExecutionWrapper

def self.active? # :nodoc:

:nodoc:
def self.active? # :nodoc:
  IsolatedExecutionState.key?(active_key)
end

def self.active_key # :nodoc:

:nodoc:
def self.active_key # :nodoc:
  @active_key ||= :"active_execution_wrapper_#{object_id}"
end

def self.error_reporter

def self.error_reporter
  @error_reporter ||= ActiveSupport::ErrorReporter.new
end

def self.perform # :nodoc:

:nodoc:
def self.perform # :nodoc:
  instance = new
  instance.run
  begin
    yield
  ensure
    instance.complete
  end
end

def self.register_hook(hook, outer: false)

invoked in that situation.)
a preceding +to_run+ block; all ordinary +to_complete+ blocks are
(Mostly, this means it won't be invoked if an exception occurs in
and will only be invoked if +run+ has previously been called.
+hook.complete+ will be passed the value returned from +hook.run+,

+complete+ steps.
Register an object to be invoked during both the +run+ and
def self.register_hook(hook, outer: false)
  if outer
    to_run RunHook.new(hook), prepend: true
    to_complete :after, CompleteHook.new(hook)
  else
    to_run RunHook.new(hook)
    to_complete CompleteHook.new(hook)
  end
end

def self.run!(reset: false)

Where possible, prefer +wrap+.

after the work has been performed.
Returns an instance, whose +complete!+ method *must* be invoked

Run this execution.
def self.run!(reset: false)
  if reset
    lost_instance = IsolatedExecutionState.delete(active_key)
    lost_instance&.complete!
  else
    return Null if active?
  end
  new.tap do |instance|
    success = nil
    begin
      instance.run!
      success = true
    ensure
      instance.complete! unless success
    end
  end
end

def self.to_complete(*args, &block)

def self.to_complete(*args, &block)
  set_callback(:complete, *args, &block)
end

def self.to_run(*args, &block)

def self.to_run(*args, &block)
  set_callback(:run, *args, &block)
end

def self.wrap

Perform the work in the supplied block as an execution.
def self.wrap
  return yield if active?
  instance = run!
  begin
    yield
  rescue => error
    error_reporter.report(error, handled: false)
    raise
  ensure
    instance.complete!
  end
end

def complete # :nodoc:

:nodoc:
def complete # :nodoc:
  run_callbacks(:complete)
end

def complete!

Where possible, prefer +wrap+.

exactly once on the result of any call to +run!+.
Complete this in-flight execution. This method *must* be called
def complete!
  complete
ensure
  IsolatedExecutionState.delete(self.class.active_key)
end

def hook_state

def hook_state
  @_hook_state ||= {}
end

def run # :nodoc:

:nodoc:
def run # :nodoc:
  run_callbacks(:run)
end

def run! # :nodoc:

:nodoc:
def run! # :nodoc:
  IsolatedExecutionState[self.class.active_key] = self
  run
end