llopez

llopez

Get the pid of a restarted process

I everyone,

I need help with something…

I am building an application that manages workers, these workers are simple GenServers with an state.

I have a separated process that keeps tracks of all the workers, basically this process is another GenServer that monitors the workers adding and removing pids from his state. I am using Process.monitor to do so.

I have a DynamicSupervisor that supervises the workers…

The problem is that when the supervisor restarts a worker I want to update the state of the monitor genserver by updating the old pid with the new one.

Process.monitor only notifies when the process dies and provides the old pid but I need the new pid to replace the old one.

Any Ideas? I am new in elixir

thanks

Most Liked

kokolegorille

kokolegorille

Instead of keeping pid in a separate GenServer, You might use Registry. It will allow You to map pid by key. This solution will work in local, then You might switch for :global when going distributed.

Or send a (register) message when the worker starts?!

peerreynders

peerreynders

I think that this points in the right direction, at least in the beginning. Registry is a more advanced use case. It may be best to start with a more restricted named process.

See Process.register/2 and GenServer name registration with the GenServer.start_link/3 :name option.

  • It probably makes the most sense that it’s the monitoring process that is named. Typically worker processes are kept fairly generic. So the worker processes would have to “check in” (register) with the monitoring process. In terms of supervision an additional supervisor may be required to ensure that the monitoring process is up and running before any of the workers start. A :rest_for_one supervisor could start up the monitoring process first and the DynamicSupervisor second.

  • Each worker process needs to check in with the named monitoring process before it does anything else. However a server’s init/1 callback is supposed to be kept as short/fast as possible as it will block the process that is calling start_link/3/start/3. So it would be best to return the new {:ok, state, {:continue, continue}} value from init/1.

  • That will cause the handle_continue/2 callback to run - from within the new process (not under the start_link/3/start/3 process). Here the new process can call/3 the monitoring process by name. A cast/2 wouldn’t ensure that the monitor process is 1) up and running and 2) has processed the message. Once that call/3 completes successfully the worker process is free to go about it’s regular job.

  • When the monitoring process receives a worker’s registration message it can use Process.monitor/1 to be notified when that particular process goes :DOWN so that it can be cleaned out of the monitoring process’s state.

Where Next?

Popular in Questions Top

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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
_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
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
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
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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
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
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

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New

We're in Beta

About us Mission Statement