adobroskok

adobroskok

Trapping exit reason in Supervisor

I need help with seem to be a trivial task. My application root is Supervisor and it starts couple of GenServer based workers. I am trying to find a way to catch worker termination reason in my Supervisor, but for some reason I can’t find a way to do this. After reading through Hexdocs I figured out that with GenServer I can define terminate callback in GenServer, but it is not always guaranteed to be called. As supervisor is responsible for restarting terminated children I based on exit reason (:normal, :shutdown,…) I assume there should be a way to catch it.
Thanks in advance for your help.

Most Liked

voltone

voltone

Supervisors are critical to the application’s reliability, so it is recommended to always use OTP’s standard (proven, simple, robust) supervisors rather than roll your own.

If some part of your application needs to be notified when processes terminate, add a GenServer that uses Process.monitor/1 to track other processes. This is orthogonal to the supervision hierarchy and therefore any issues in your monitoring code will not impact supervision.

ndac_todoroki

ndac_todoroki

I am pretty late here, but I’m afraid one can not overwrite Supervisors handle_info/2, as well as handle_call and handle_cast, because while Supervisor does not define those callbacks.

defmodule Sup do
  use Supervisor

  def handle_info(any, state) do
    IO.inspect any
  end
end

# try messaging
send(sup_pid, :hello)

Trying the above does not work, it will say something like
01:52:12.737 [error] Supervisor received unexpected message: :hello

The handle_info handle_call the Supervisor can receive seems to be defined here, and Elixir is just calling that inside def which_children, do: ....

idi527

idi527

Maybe you can overwrite handle_info/2 in your supervisor module?

require Logger

def handle_info({:EXIT, pid, reason}, state) do
  Logger.info("A child process died: #{reason}")
  case restart_child(pid, reason, state) do # it's a private function ... that's a problem
    {:ok, state} ->
	  {:noreply, state}
    {:shutdown, state} ->
	  {:stop, :shutdown, state}
  end
end

def handle_info(msg, state) do
  Logger:error("Supervisor received unexpected message: #{inspect(msg)}")
  {:noreply, state}
end

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement