класс ActiveSupport::ExecutionWrapper
Атрибуты
Публичные методы класса
# File activesupport/lib/active_support/execution_wrapper.rb, line 112 def self.error_reporter @error_reporter ||= ActiveSupport::ErrorReporter.new end
# 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 и complete.
hook.complete будет передано значение, возвращённое hook.run, и будет вызвано только в том случае, если run был ранее вызван. (В основном, это означает, что он не будет вызван, если в предшествующем блоке to_run произошла ошибка; все обычные блоки to_complete вызываются в этом случае.)
# File activesupport/lib/active_support/execution_wrapper.rb, line 67
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 Выполнить данное выполнение.
Возвращает экземпляр, метод complete! которого обязательно должен быть вызван после выполнения работы.
В возможности, используйте wrap.
# File activesupport/lib/active_support/execution_wrapper.rb, line 22 def self.to_complete(*args, &block) set_callback(:complete, *args, &block) end
# File activesupport/lib/active_support/execution_wrapper.rb, line 18 def self.to_run(*args, &block) set_callback(:run, *args, &block) end
# File activesupport/lib/active_support/execution_wrapper.rb, line 84
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 Выполнить работу в предоставленном блоке как выполнение.
Публичные методы экземпляра
# File activesupport/lib/active_support/execution_wrapper.rb, line 140 def complete! complete ensure self.class.active.delete(IsolatedExecutionState.unique_id) end
Завершить текущее выполнение. Этот метод обязательно должен быть вызван ровно один раз на результате любого вызова run!.
В возможности, используйте wrap.
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.