View Source SimpleSingletonSupervisor (SimpleSingletonSupervisor v0.1.0)

It allows you to run a single globally unique process in a cluster. It uses only :global and doesn't create any additional processes. Name of the supervisor is determining of the global uniqueness.

Before starting it synchronizes all nodes via :global.sync/0 and then runs a transaction to guarantee that the only one node is starting this process in the same time. If the process is started (or already running) it links that process to the local supervisor. If the process dies it will be restarted on another available node.

If the process is started on one node and the network is partitioned you would face a situation when the process is running on n partitions. When the partition is healed, only one process will be running across the cluster. All other processes will be terminated.

Be aware, in distributed systems you can't guarantee that the process will be started only and only once no matter what's happening. You still can face problems like network partitions. You can read more about it here: https://keathley.io/blog/sgp.html.

To start a process you can add SimpleSingletonSupervisor to your application's children list:

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    singleton_children = [
      {MyApp.SingletonProcess, []}
    ]

    children = [
      {SimpleSingletonSupervisor, [
        name: MyApp.SingletonSupervisor,
        strategy: :one_for_one,
        children: singleton_children
      ]}
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

SingletonSupervisor can also be used as a module-based supervisor:

defmodule MySingletonSupervisor do
  @moduledoc false

  use Supervisor

  def start_link(init_arg) do
    SimpleSingletonSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  @impl Supervisor
  def init(_init_arg) do
    children = [
      {MyApp.SingletonProcess, []}
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end
end

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts a module-based singleton supervisor process with the given module and init_arg.

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

start_link(module, init_arg, options \\ [])

View Source
@spec start_link(module(), term(), Keyword.t()) :: Supervisor.on_start()

Starts a module-based singleton supervisor process with the given module and init_arg.