Last active
July 10, 2018 18:26
-
-
Save jisaacstone/7a698ad05e61a15b4d28 to your computer and use it in GitHub Desktop.
programmatic node startup in elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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