Class: ActiveSupport::ExecutionWrapper

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
activesupport/lib/active_support/execution_wrapper.rb

Direct Known Subclasses

Executor, Reloader

Defined Under Namespace

Classes: CompleteHook, RunHook

Constant Summary collapse

Null =

:nodoc:

Object.new

Constants included from Callbacks

Callbacks::CALLBACK_FILTER_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#run_callbacks

Methods included from Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Class Method Details

.active?Boolean

:nodoc:

Returns:

  • (Boolean)

119
120
121
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 119

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

.active_keyObject

:nodoc:


115
116
117
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 115

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

.error_reporterObject


111
112
113
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 111

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

.performObject

:nodoc:


101
102
103
104
105
106
107
108
109
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 101

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

.register_hook(hook, outer: false) ⇒ Object

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


51
52
53
54
55
56
57
58
59
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 51

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) ⇒ Object

Run this execution.

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

Where possible, prefer wrap.


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 67

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) ⇒ Object


22
23
24
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 22

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

.to_run(*args, &block) ⇒ Object


18
19
20
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 18

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

.wrapObject

Perform the work in the supplied block as an execution.


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 87

def self.wrap
  return yield if active?

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

Instance Method Details

#completeObject

:nodoc:


142
143
144
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 142

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

#complete!Object

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

Where possible, prefer wrap.


136
137
138
139
140
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 136

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

#runObject

:nodoc:


128
129
130
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 128

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

#run!Object

:nodoc:


123
124
125
126
# File 'activesupport/lib/active_support/execution_wrapper.rb', line 123

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