I have a GenServer (MyGenServer) which is globally registered.
There’s a situation where this process is killed from another place in my app `Process.exit(<MyGenServer_pid>, :kill).
I’m unable to trap errors in that GenServer since my process is just killed.
When my global GenServer process is killed, what is the best way to handle that killed process by:
performing some conditional checks of other stuff in my app
conditionally restarting MyGenServer depending on the outcome.
What I’m Trying Now
I created a new custom Supervisor for this global GenServer process.
This restarts my process when it gets killed, but I’d like to manually handle its restart instead of relying on a supervision strategy.
I was looking into trapping the :DOWN resulting from the supervisor’s child process being down. However, I noticed I’m not able to define handle_info callbacks in my supervisor.
Questions
Is there a suggested way to handle a killed child process from within its Supervisor?
Is there another pattern more appropriate for this? (I was looking into possibly having a GenServer that monitors my global genserver process and handling :DOWN messages in there)
Maybe I’m missing something, but couldn’t you just not use :kill? That way you could trap exits and handle whatever logic you want from there or terminate. Maybe you specifically want to get a clean slate by getting rid of that process?
Otherwise, what you said. Have some sort of “manager” GenServer take care of it
Just putting my feelers out there for potential solutions. Some tricky bits in this particular example that made me want to explore potential of handling before attempting to restart again. Spared those details as they are out of scope for what I hoped to obtain from asking the question.
Totally agree with you that it’s best to use restart strategies in most scenarios though.
Thanks for that link, if I go down this path, I think I’ll go with a sort of manager process monitoring my genserver as opposed to supervisor
Unfortunately I can’t control how the process is being exited in this case. In an ideal world I would totally go that route though.
I’m checking out a few things now including the manager solution. thanks for your reply!