Skip to content

Instantly share code, notes, and snippets.

@rogeralsing
Forked from jisaacstone/elixir_distributed.ex
Created June 11, 2016 16:53
Show Gist options
  • Save rogeralsing/fcb099cb1704194e66254e51d985623c to your computer and use it in GitHub Desktop.
Save rogeralsing/fcb099cb1704194e66254e51d985623c 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