123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::ExecutionWrapper

Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, Callbacks
Inherits: Object
Defined in: activesupport/lib/active_support/execution_wrapper.rb

Constant Summary

Callbacks - Included

CALLBACK_FILTER_TYPES

Callbacks - Attributes & Methods

Class Attribute Summary

Class Method Summary

DescendantsTracker - self

Instance Method Summary

Callbacks - Included

#run_callbacks

Runs the callbacks for the given event.

#halted_callback_hook

A hook invoked every time a before callback is halted.

Class Attribute Details

.__callbacks (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 69

class_attribute :__callbacks, instance_writer: false, instance_predicate: false, default: {}

.active?Boolean (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 118

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

Class Method Details

.active_key

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 114

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

.error_reporter

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 110

def self.error_reporter # :nodoc:
  ActiveSupport.error_reporter
end

.perform

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 100

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

.register_hook(hook, outer: false)

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.)

[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 50

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

.run!(reset: false)

Run this execution.

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

Where possible, prefer .wrap.

[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 66

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

.to_complete(*args, &block)

[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 21

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

.to_run(*args, &block)

[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 17

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

.wrap(source: "application.active_support")

Perform the work in the supplied block as an execution.

[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 86

def self.wrap(source: "application.active_support")
  return yield if active?

  instance = run!
  begin
    yield
  rescue => error
    error_reporter&.report(error, handled: false, source: source)
    raise
  ensure
    instance.complete!
  end
end

Instance Attribute Details

#__callbacks (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/callbacks.rb', line 69

class_attribute :__callbacks, instance_writer: false, instance_predicate: false, default: {}

Instance Method Details

#complete

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 141

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

#complete!

Complete this in-flight execution. This method must be called exactly once on the result of any call to .run!.

Where possible, prefer .wrap.

[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 135

def complete!
  complete
ensure
  IsolatedExecutionState.delete(self.class.active_key)
end

#hook_state (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 146

def hook_state
  @_hook_state ||= {}
end

#run

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 127

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

#run!

This method is for internal use only.
[ GitHub ]

  
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 122

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