Skip to content

Instantly share code, notes, and snippets.

@jisaacstone
Last active July 10, 2018 18:26
Show Gist options
  • Save jisaacstone/7a698ad05e61a15b4d28 to your computer and use it in GitHub Desktop.
Save jisaacstone/7a698ad05e61a15b4d28 to your computer and use it in GitHub Desktop.
programmatic node startup in elixir
defmodule ExDistUtils do
def start_distributed(appname) do
unless Node.alive?() do
local_node_name = generate_name(appname)
{:ok, _} = Node.start(local_node_name)
end
cookie = Application.get_env(appname, :cookie)
Node.set_cookie(cookie)
end
defp generate_name(appname) do
machine = Application.get_env(appname, :machine, "localhost")
hex = :erlang.monotonic_time() |>
:erlang.phash2(256) |>
Integer.to_string(16)
String.to_atom("#{appname}-#{hex}@#{machine}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment