You can configure the :kernel
application to wait for cluster formation before starting your application during node start up. The sync_nodes_optional
configuration specifies which nodes to attempt to connect to within the sync_nodes_timeout
window, defined in milliseconds, before continuing with startup. There is also a sync_nodes_mandatory
setting which can be used to enforce all nodes are connected within the timeout window or else the node terminates.
config :kernel,
sync_nodes_optional: [:"[email protected]", :"[email protected]"],
sync_nodes_timeout: 30_000
The sync_nodes_timeout
can be configured as :infinity
to wait indefinitely for all nodes to
connect. All involved nodes must have the same value for sync_nodes_timeout
.
This approach will only work for Elixir releases. You will need to use Erlang's sys.config
file for development purposes.
The Erlang equivalent of the :kernerl
mix config, as above, is:
[{kernel,
[
{sync_nodes_optional, ['[email protected]', '[email protected]']},
{sync_nodes_timeout, 30000}
]}
].
-
Run an Erlang Port Mapper Daemon (epmd):
$ epmd -d
-
Start an
iex
console per node:$ MIX_ENV=test iex --name [email protected] --erl "-config cluster/node1.sys.config" -S mix
$ MIX_ENV=test iex --name [email protected] --erl "-config cluster/node2.sys.config" -S mix
$ MIX_ENV=test iex --name [email protected] --erl "-config cluster/node3.sys.config" -S mix
The node specific <node>.sys.config
files ensure the cluster is formed before starting the application, assuming this occurs within the 30 seconds timeout.