nhpip

nhpip

Supervisors - why not have an option to pass an anonymous. function / function capture too?

So the other day I was rearranging the supervisor hierarchy of our product and I had a thought. In addition to creating a child spec with hard-coded values (restart strategy, interval etc) why not have an option to pass an anonymous. function / function capture too. This would take the reason for the crash along with the state of prior crashes as arguments and can be used to handle special cases.

The function could override the provided default restart options, or perform additional tasks / permit finer-grained control. For example with one_for_all configuration one could alert peer processes prior to restarting. I realize that some of this could be done via another process that called Process.monitor/1, but that would handle the crash in parallel with the supervisor. You would also need to ensure the provided function didn’t crash or block the supervisor.

e.g.

sup_fun = 
   fn {%RuntimeError{message: "ooops"}, _}, %{current_restarts: restarts, ....} -> 
         # Logic for oops
        {:restart, :one_for_one}

      {%RuntimeError{message: "big ooops"}, _}, %{current_restarts: restarts, ....} ->
         # Logic for big oops
        {:restart, :one_for_all}

       _, _ -> :default ## Do the provisioned actions
end

....

% My Erlang is a bit rusty
handle_info({'EXIT', Pid, Reason}, #state{handler_fun = Fun} = State) ->
  SupPid = self()
  FunPid = spawn(fun() -> SupPid ! {actions, self(), Fun(Reason, State)} end)
  receive
      {actions, FunPid, Actions} -> 
         handle_crash(Pid, Reason, Actions, State)
  after
    2000 -> 
         handle_crash(Pid, Reason, default, State)    
  end

I guess there’s some complexity since Elixir leverages the OTP supervisor. Maybe this is more of an Erlang question?

Most Liked

gregvaughn

gregvaughn

I hope you’re right. I enjoyed using it for my first 3 years of working in Elixir. (“hot topic” I see what you did there :smirk: )

marciol

marciol

I think that it will be a hot topic when we start to get Elixir people talking about self-hosting and things like that, which is becoming a hot topic in some other communities.

gregvaughn

gregvaughn

I suspect this is related to hot code loading, which was important to Erlang/OTP development even though the Elixir community mostly avoids it. Anonymous function references can become invalid when the module they were defined in is updated in the BEAM.

Where Next?

Popular in Proposals: Ideas Top

rmoorman
Current situation Currently, the structure of the HTML returned by phoenix is determined by the layouts (components/layouts/[root,app].ht...
New
bamorim
Story behind Recently, I gave a talk on a meetup about improving performance of Phoenix applications and the example app was a LiveView ...
New
7rans
I implemented Access behavior for a struct today. Pseudo-code… defmodule MyStruct do defstruct data: %{} @behaviour Access # ... ...
New
bartblast
This could resolve to {[a: 1, b: 2]}. Was it ever considered to allow such syntax? Notice this: {:abc, a: 1, b: 2} and this: my_fun(:abc,...
New
mortenlund
Hi! I would like to suggest a new callback in the lifecycle of the Live Component which is unmount. Sometimes it is nice to be able to ...
New
Flo0807
Hello everyone! Phoenix LiveView v0.18 introduced the special attributes :let, :for and :if. In addition to the :if special attribute, I...
New
cheerfulstoic
I feel like Elixir is getting big enough and old enough that I’m starting to experience problems with conflicting dependencies. An examp...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement