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
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.
- 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)
Why? The existing supervision strategies have been battle tested over time.
What is it about your “manual restart” that you can’t move it into the GenServer initialization?
[erlang-questions] Custom supervisor
This is the traditional answer when trying to do custom logic on supervised process death
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
If you want a rich crash behaviour, then I agree that another process is needed. I wrote a library called Parent which can simplify this task a bit.
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
very cool! thanks for the reply. I’m definitely looking into this pattern. Anywhere that you may have seen this before?
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!
thanks for that! I will check into the library for sure as well.
Also side note, I picked up Elixir in Action a few weeks ago and it’s great!