I have a logic module that tells a supervisor to start child processes. I need to store those childrens pid in the logic modules state. But I also need to update a childs pid if the supervisor restarts it.
So I can't use the return value pid from the start_child call, since that will only give me the pid on the first start, not the restarts. Right now I make the child process call a register function (updates state with new pid) in the logic module from the childs init function. That way the logic module can update the pid in its state whenever a process is restarted. The logic module is a gen_server and I'm doing a cast when i register the child process.
Can anyone see a problem with this and are there any other more "proper" way of doing it?
One problem is that you have the ChildPid
and the child might be dead by now. So sending it a message through a cast
will mean the message is lost. And through a call
you will crash yourself with an {'EXIT', noproc}
unless you catch it out of the call
. Your solution must take into account that a Pid
might be long gone the instant you send a message. Usually by ignoring that the message is lost, by crashing yourself, or by remedying the problem and then go on.
There are a couple of options. This is a loose list:
monitor
on the child. That way you know if it dies.supervisor:which_children
to find the relevant child.gproc
kernel/stdlib
). Use the global structure to track the pids in question.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With