stevensonmt
In the Elixir for Programmers course by @pragdave he asks the student to come up with a means of monitoring when children of a dynamic supervisor “goes away”. By “goes away” I assume that means exits gracefully or the node crashes out. I can easily monitor when nodes connect by implementing handle_info and when the the call to DynamicSupervisor.start_child occurs I follow it with a call to send(pid, DynamicSupervisor.which_children(my_supervisor_name)). I don’t yet have a graceful exit implemented for the game so right now I only have to handle when the node crashes out. Since this occurs without a message being sent to the supervisor I don’t know how to trigger a reaction to the event. Any suggestions?
===========
Actually it occurs to me that I might not have been killing or exiting the node in a reasonable way. I’m starting the supervisor in an IEX session, then in another terminal starting the child process. Then I ctrl-c to kill the IEX session of the child process. When running the observer on the supervisor I do not see any message coming into the supervisor, but the child process PID persists. So maybe as far as the VM is concerned nothing has happened?
The information in the observer for the child processes that are no longer active gives “status waiting” in Process Information tab but in the State tab “status” is “running” and Logged Events is an empty list.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
joaoevangelista
Disclamer: I have no idea if it is a correct usage.
What you could do is monitor the child when you start it
Now when you kill the child the process that is monitoring, it will receive a
{:DOWN, ref, :process, object, reason}message that you can act upon.So to wrap this behaviour you could introduce a GenServer that delegates start child to the underlying supervisor, and monitors the children.
Example
You can paste the example on a iex session
stevensonmt
Thanks but that doesn’t seem to work. The child process persists with no message sent to the supervisor.
When the observer is running and I kill the child-spawning IEX session, I see a “Node node_name down” message box, but the processes it was running do not change. I’d like my supervisor to be able to receive that “node down” message and respond to it.
joaoevangelista
The child will persist, since the supervisor is monitoring and restarting it when it dies.
The supervisor will not get a message that you can act upon, thats why we setup a monitor on another Process, to be notified when it dies.
To terminate a child, without the supervisor restarting it you need to call DynamicSupervisor.terminate_child/2 (see updated example above)
This will make that your GenServer gets a message on the handle_info callback that you can act upon
joaoevangelista
Nodes are VMs, if you start two iex sessions that is two nodes (vms)
stevensonmt
Sorry for being so slow to get it. Thank you for your help. I understand the two nodes are separate VMs, I was simply pointing out that the VM running the parent process was getting some sort of message when the VM running the child process went down. If the observer can see that message I assume there is a way to read that message for my application as well.
I also appreciate you pointing out
terminate_child/2but my problem is figuring out how to trigger calling that.stevensonmt
Actually I think I just realized what is wrong with my thinking. Both processes are running on the same VM. The client node can disconnect but the game process persists.
I think I need to send the pid of the client node process that calls for the game to start.
Right now the client does this to start a new game:
and then
MyGame.new_game/0calls the server’sstart_game/0function that is under the supervisor.I think I need
MyGame.new_game/1that takes a pid from the client via:rpc.call(@my_game_server, MyGame, :new_game, [self()])and then I can haveServer.start_game/1that takes that pid and callsProcess.monitor(pid).Maybe.
joaoevangelista
Oh so you are also dealing with distribution, you could start the game on one node and call functions of it using rpc, yes. If you need to see if the client node disconnects from the game node, you’ll need to use Node.monitor/2. I thought you wanted to log when a child of the dynamic supervisor exited.
stevensonmt
Yes, I was confused and asked the wrong question.
stevensonmt
Is there a way to tell the Supervisor to monitor the Node? When calling
Node.monitor/2in mystart_game/0function I don’t think the Supervisor is monitoring but rather the parent of the Supervisor. How do I find which process is doing the monitoring?joaoevangelista
You can use a genserver to monitor the Node, the process where you call
Node.monitor/2will be the one responsible to monitoring it. If you call on a GenServer callback it will be the GenServer, then you can listen for messages on handle_info callback.