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.
You can find supporting documentation and usage examples here.
Simple Singleton Supervisor is available on Hex, the package can be installed by adding simple_singleton_supervisor
to your list of dependencies in mix.exs
:
def deps do
[
{:simple_singleton_supervisor, "~> MAJ.MIN"}
]
end
You can determine the latest version by running mix hex.info simple_singleton_supervisor
in your shell, or by going to the simple_singleton_supervisor
page on hex.pm.
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
Copyright (c) 2024 Yurii Zhyvaha
This library is MIT licensed. See the LICENSE.md for details.