axelson
What is an easy way to form a dynamic cluster?
Is there an easy way to form a BEAM cluster with dynamic membership? Specifically I want to form the cluster from multiple instances of the same escript/program, this means that the name of each node needs to be unique, but names cannot be pre-generated. I could try to connect with sequentially increasing names, but is there an easier/better way? It seems like I could use GitHub - bitwalker/libcluster: Automatic cluster formation/healing for Elixir applications · GitHub but I don’t see a Cluster.Strategy that seems appropriate
Most Liked
axelson
Okay, I’ve come up with this little snippet. Is there anything that could be improved with it?
def start_node(number) when number > 20, do: raise "Too many instances found"
def start_node(number \\ 0) do
node_name = node_name(number)
case Node.start(node_name, :shortnames) do
{:error, _error} ->
start_node(number + 1)
{:ok, _pid} ->
IO.puts("Started node with name #{inspect(node_name)}")
Node.set_cookie(node_name, :cookie)
for num <- Range.new(0, number) do
Node.connect(node_name)
end
end
end
def node_name(number) do
{:ok, hostname} = :inet.gethostname()
"elixirls-#{number}@#{hostname}"
|> String.to_atom()
end
Edit: added a max number
axelson
This is only for a local machine, so I think short names should be fine, in fact if I could I would ignore the hostname completely in this code. Also Node.start/3 and Node.connect/1 both only accept atoms so the only way that I see to avoid String.to_atom/1 is to pregenerate a set number of node names. Which could be a good idea, but if I were to do that I’d prefer to instead just limit the number that start_node will recurse to, a max of 20 would probably be a good idea.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









