跳到內容 跳到搜尋
方法
C
R
T
W
已包含的模組

類別公共方法

register_hook(hook, outer: false)

註冊物件,以便在執行「執行」和「完成」步驟時呼叫它。

hook.complete 將傳遞從 hook.run 傳回的值,而且只會在先呼叫 run 時呼叫它。(大多數情況下,表示如果在之前的 to_run 區塊中發生例外狀況,便不會呼叫它;所有正常的 to_complete 區塊都會在該情況下呼叫。)

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

執行此執行緒。

傳回執行個體,其 complete! 方法必須在執行工作後呼叫。

如果可行,優先使用 wrap

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

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

# 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")

將提供區塊中的工作當成執行緒來執行。

# 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

執行個體公共方法

complete!()

完成正在執行的執行緒。此方法必須在任何呼叫 run! 所傳回結果上呼叫一次,只限一次。

如果可行,優先使用 wrap

# File activesupport/lib/active_support/execution_wrapper.rb, line 135
def complete!
  complete
ensure
  IsolatedExecutionState.delete(self.class.active_key)
end