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
rhcarvalho
Hi all, I would like to gather some feedback before a more intentional proposal to add a new :depth option when specifying a Git depende...
New
rekkice
I’m building an editor integration to evaluate Elixir code in an IEx session. While Code.eval_string/3 allows tracking variable bindings,...
New
kip
Sumary of proposal DateTime.from_iso8601/3 adjusted to: set all numeric fields to the values as parsed (not shifted to UTC), preservi...
New
pdgonzalez872
Hi! There has been some discussion about hiring/jobs on here and I thought about running this by everyone. I wanted to try to help recr...
New
dli
Ecto currently supports some data-modifying WITH statements / CTEs for Postgres: Options: […] :operation - one of :all , :update_all ,...
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
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
sezaru
When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex: alias __MODULE__.{Im...
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

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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