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.

D4no0

D4no0

People fear what they cannot understand. Once a “phoenix” framework comes for hot-code reloading this will become the new hot topic. I think we both lack abstraction and education on how to use it yet, but at the same time we have some infrastructure in place to facilitate the adoption.

Where Next?

Popular in Proposals: Ideas Top

MeerKatDev
many times we do stuff like (e.g. test setups, json views in phoenix) aaa = ... bbb = ... ccc = ... %{aaa: aaa, bbb: bbb, ccc: ccc} and...
New
cevado
Being able to build nested relations in a schemaless changeset would be helpful to deal with complex forms on Phoenix without the need to...
New
markevans
Hi! I’m excited about everything that’s going on re. gradual typing and am really pleased to see that Jose and the team seem to be think...
New
mikesax
I was writing code that included a function I would call later (so it wasn’t called yet). I got a warning, and I thought I could put an u...
New
RobinBoers
Hi. We had a few issues in our project regarding mix tasks, where we expected the items in the @requirements module tag to have been exec...
New
winsalva
Hello all. First of all i’m running this using termux on an android phone. Running mix assets.setup shows this message 06:54:08.450 [de...
New
nhpip
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...
New
cevado
IEx is a very powerfull shell and it would be awesome to have all this power integrated inside a code editor. Clojure enables something l...
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
dkuku
This is a proposal to make the map key mismatch errors a bit better: Every time I have a typo It’s very challenging for me even when I u...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement