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
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_onesupervisor could start up the monitoring process first and theDynamicSupervisorsecond. -
Each worker process needs to check in with the named monitoring process before it does anything else. However a server’s
init/1callback is supposed to be kept as short/fast as possible as it will block the process that is callingstart_link/3/start/3. So it would be best to return the new{:ok, state, {:continue, continue}}value frominit/1. -
That will cause the
handle_continue/2callback to run - from within the new process (not under thestart_link/3/start/3process). Here the new process cancall/3the monitoring process by name. Acast/2wouldn’t ensure that the monitor process is 1) up and running and 2) has processed the message. Once thatcall/3completes 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/1to be notified when that particular process goes:DOWNso that it can be cleaned out of the monitoring process’s state.







