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
peerreynders

peerreynders

It is my understanding that SASL Error Logging will capture the exit reason as part of the Supervisor Report. So it’s a matter of enabling SASL logging and capturing the log with an appropriate backend.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement