檢視原始碼 Supervisor.Spec (Elixir v1.16.2)

此模組已棄用。請改用 Supervisor 模組中所述的新子規格。

用於建立子規格的過時函數。

此模組中的函數已棄用,且無法與 Elixir v1.5 中所引入的基於模組的子規格搭配使用。請改參閱 Supervisor 文件。

用於定義監督規格的便利函數。

範例

透過使用此模組中的函數,可以指定要在監督下使用的子項,並使用 Supervisor.start_link/2 啟動

import Supervisor.Spec

children = [
  worker(MyWorker, [arg1, arg2, arg3]),
  supervisor(MySupervisor, [arg1])
]

Supervisor.start_link(children, strategy: :one_for_one)

有時,定義由模組支援的監督可能會很方便

defmodule MySupervisor do
  use Supervisor

  def start_link(arg) do
    Supervisor.start_link(__MODULE__, arg)
  end

  def init(arg) do
    children = [
      worker(MyWorker, [arg], restart: :temporary)
    ]

    supervise(children, strategy: :simple_one_for_one)
  end
end

請注意,在這種情況下,我們不必明確匯入 Supervisor.Spec,因為 use Supervisor 會自動執行此動作。定義基於模組的監督可能會很有用,例如,在 Supervisor.init/1 回呼中執行初始化任務。

監督和工作人員選項

在上述範例中,我們定義了工作人員和監督的規格。這些規格(工作人員和監督皆適用)接受下列選項

  • :id - 監督內部用於識別子規格的名稱;預設為子工作人員/監督的指定模組名稱

  • :function - 在子項上呼叫以啟動它的函數

  • :restart - 定義終止的子處理程序應重新啟動的原子(請參閱下方的「重新啟動值」區段)

  • :shutdown - 定義子程序應如何終止的原子(請參閱以下的「關閉值」區段)

  • :modules - 應為包含一個元素 [module] 的清單,其中 module 僅在子程序為 SupervisorGenServer 時才是回呼模組的名稱;如果子程序為 GenEvent:modules 應為 :dynamic

重新啟動值 (:restart)

:restart 選項支援下列重新啟動值

  • :permanent - 子程序總是重新啟動

  • :temporary - 子程序從不重新啟動(即使監督程序的策略為 :rest_for_one:one_for_all

  • :transient - 僅在子程序異常終止時重新啟動子程序,亦即,退出原因不為 :normal:shutdown{:shutdown, term}

請注意,達到最大重新啟動強度的監督程序將以 :shutdown 原因退出。在此情況下,監督程序僅在子程序規格定義為將 :restart 選項設定為 :permanent(預設值)時才會重新啟動。

關閉值 (:shutdown)

:shutdown 選項支援下列關閉值

  • :brutal_kill - 使用 Process.exit(child, :kill) 無條件終止子程序

  • :infinity - 如果子程序為監督程序,這是給予子樹足夠時間關閉的機制;也可以小心地用於工作程序

  • 非負整數 - 監督程序指示子程序透過呼叫 Process.exit(child, :shutdown) 終止的毫秒數,然後等待退出訊號。如果在指定時間內未收到退出訊號,將使用 Process.exit(child, :kill) 無條件終止子程序

摘要

類型

支援的 ID 值

支援的模組值

支援的重新啟動值

支援的關閉值

監控器規格

支援的策略

支援的工作人員值

函數

接收要監控的 children(工作人員或監控器)清單和一組 options

將指定的 module 定義為監控器,並使用指定的引數啟動。

將指定的 module 定義為工作人員,並使用指定的引數啟動。

類型

@type child_id() :: term()

支援的 ID 值

@type modules() :: :dynamic | [module()]

支援的模組值

@type restart() :: :permanent | :transient | :temporary

支援的重新啟動值

@type shutdown() :: timeout() | :brutal_kill

支援的關閉值

@type spec() ::
  {child_id(), start_fun :: {module(), atom(), [term()]}, restart(), shutdown(),
   worker(), modules()}

監控器規格

@type strategy() :: :simple_one_for_one | :one_for_one | :one_for_all | :rest_for_one

支援的策略

@type worker() :: :worker | :supervisor

支援的工作人員值

函數

連結至此函數

supervise(children, options)

檢視原始碼
此函數已棄用。請改用 Supervisor 模組中概述的新子規格。
@spec supervise([spec()],
  strategy: strategy(),
  max_restarts: non_neg_integer(),
  max_seconds: pos_integer()
) :: {:ok, tuple()}

接收要監控的 children(工作人員或監控器)清單和一組 options

傳回包含監控器規格的元組。此元組可用作 Supervisor.init/1 回呼函式的傳回值,以實作基於模組的監控器。

範例

supervise(children, strategy: :one_for_one)

選項

  • :strategy - 重新啟動策略選項。它可以是 :one_for_one:rest_for_one:one_for_all:simple_one_for_one。您可以在 Supervisor 模組文件瞭解更多關於策略的資訊。

  • :max_restarts - 在時間範圍內允許的最大重新啟動次數。預設為 3

  • :max_seconds - :max_restarts 適用的時間範圍。預設為 5

:strategy 選項為必填,預設在 5 秒內允許最多 3 次重新啟動。查看 Supervisor 模組以取得可用策略的詳細說明。

連結至此函數

supervisor(module, args, options \\ [])

檢視原始碼
此函數已棄用。請改用 Supervisor 模組中概述的新子規格。
@spec supervisor(
  module(),
  [term()],
  restart: restart(),
  shutdown: shutdown(),
  id: term(),
  function: atom(),
  modules: modules()
) :: spec()

將指定的 module 定義為監控器,並使用指定的引數啟動。

supervisor(module, [], restart: :permanent)

預設情況下,會在指定的模組上呼叫 start_link 函式。整體而言,選項的預設值為

[
  id: module,
  function: :start_link,
  restart: :permanent,
  shutdown: :infinity,
  modules: [module]
]

請參閱 Supervisor.Spec 模組中的「Supervisor 和工作人員選項」部分,以取得更多關於可用選項的資訊。

連結至此函數

worker(module, args, options \\ [])

檢視原始碼
此函數已棄用。請改用 Supervisor 模組中概述的新子規格。
@spec worker(
  module(),
  [term()],
  restart: restart(),
  shutdown: shutdown(),
  id: term(),
  function: atom(),
  modules: modules()
) :: spec()

將指定的 module 定義為工作人員,並使用指定的引數啟動。

worker(ExUnit.Runner, [], restart: :permanent)

預設情況下,會在指定的模組上呼叫 start_link 函式。整體而言,選項的預設值為

[
  id: module,
  function: :start_link,
  restart: :permanent,
  shutdown: 5000,
  modules: [module]
]

請參閱 Supervisor.Spec 模組中的「Supervisor 和工作人員選項」部分,以取得更多關於可用選項的資訊。