axelson

axelson

Scenic Core Team

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

axelson

Scenic Core Team

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

axelson

Scenic Core Team

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.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement