PhoenixMountable.Singleton (PhoenixMountable v0.2.1)

Copy Markdown View Source

Runs a process once per cluster.

Add it to a mount's children to run one instance of a process across all nodes, such as a scheduler that must not run twice:

def children(mount) do
  [
    {PhoenixMountable.Singleton,
     name: {:refresh_scheduler, PhoenixMountable.Mount.key(mount)},
     child: {MyLib.RefreshScheduler, mount: mount}}
  ]
end

Every node runs a Singleton. The first to register :name globally owns it and starts the child; the others watch the owner. When the owner's node goes down, the next one registers and starts the child. A child that exits is restarted by its owner. On a single node it simply runs the child.

whereis/1 returns the child, on whichever node it runs.

Options

  • :name (required) — a term naming the singleton across the cluster.
  • :child (required) — the child spec of the process.
  • :retry — milliseconds before restarting a child that failed to start or exited. Defaults to 1_000.

Summary

Functions

The pid of the singleton's child named name, on any node, or nil.

Functions

whereis(name)

@spec whereis(term()) :: pid() | nil

The pid of the singleton's child named name, on any node, or nil.