Starting children of dynamic supervisors on application startup?

Hi!

I’m working on an application which has two dynamic supervisors.
Dynamic supervisors are required, as the application should be able to start children at runtime. Below you’ll find the init function of the “main” supervisor:

  def init(_) do
      children = [
        supervisor(MyApp.OneThing.Supervisor, []),
        supervisor(MyApp.OtherThing.Supervisor, []),
        worker(MyApp.Coordinator, [])
    ]
    supervise(children, strategy: :one_for_one)
end

For some persistence reasons, the application persists the state of each child using Ecto in conjunction with PostgreSQL. Now, I want to load the stored states of each child on application startup and use it to start a “new” child with the previously stored state. In order to solve this, I’ve setup the simple GenServer MyApp.Coordinator which load the stored states from database and then starts a child for each entry. This solution works as expected, but in order to improve my Elixir/Erlang skills:

Is there another, maybe more idiomatic way to start children of dynamic supervisors on application startup?

Many thanks for your thoughts…

2 Likes

The usual way I do it is to occasionally serialize my GenServer’s/Whatevers state into mnesia (or ecto in this case) every once-in-a-while as well as on terminate, then just read it in on load. Good to keep a few good recent states too in case something bad happens and you can look back to see what. I’ve not seen a library use this style yet, always done it manually, not hard to do.

3 Likes

Your Coordinator solution is what I use, though I usually call it a Manager, but the concept is the same. It works well because you can then put the handling of starting and stopping children in a separate process and let the supervisors simply supervise.

In terms of serialization I think what @OvermindDL1 said seems good. Pushing a whole blob of state into mnesia is comfortable because it’ll store arbitrary erlang terms anyway, so you don’t need to necessarily care about massaging the data before.

3 Likes