raymondboswel

raymondboswel

Creating a supervised global singleton genserver

We have a process that is registered using:

def start_link do
  case GenServer.start_link(__MODULE__, :ok, name: {:global, __MODULE__}) do
    {:ok, pid} ->
      Logger.info "---- #{__MODULE__} worker started"
      {:ok, pid}
    {:error, {:already_started, pid}} ->
      Logger.info "---- #{__MODULE__} worker already running"
      {:ok, pid}
  end
end

Currently, if we kill the node running the singleton, the other node doesn’t seem to notice that the node is down, and the killed processes aren’t restarted.

E.g Supervisor.which_children on the remaining node returns the remote pid #PID<4554, 234, 0>, and :global.whereis_name returns :undefined

What we would like to see is that the supervisor restarts the process on the remaining node, but so far our attempts have been fruitless.

Marked As Solved

dom

dom

This is a good example: https://github.com/arjan/singleton

Also Liked

dideler

dideler

Another option, which I’ve seen in a codebase, is to lazily restart the GenServer when a client call notices that it’s down. This is instead of monitoring the process on every node and handling the :DOWN message as Singleton does.

e.g.

## Client API
def foo(name \\ __MODULE__) do
  case GenServer.whereis({:global, name}) do
    nil ->
      # restart on an available node, e.g. via Supervisor.restart_child 
      GenServer.call({:global, name}, :foo)
    pid ->
      GenServer.call(pid, :foo)
  end
end

## Server callbacks
def handle_call(:foo, _from, state) do
  # update state and reply
end

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement