Best way to restore the dynamic children of a supervisor when the supervisor restarts

I have a Dynamic supervisor that supervisors many child workers, based on a list. Whats the ideal way to have this this supervisor restart the same list of child workers whenever the supervisor itself restarts?

Maybe store the initial state (required for “recreation”) of each child process (whenever it’s added to the supervisor) in an ets table belonging to a process which is not affected by supervisor dying (so that the table is not deallocated when the supervisor dies), read the states from there on supervisor start up thus recreating the supervisor’s child processes it had before dying?

But I usually try to structure my supervision trees such that supervisors don’t die except for very exceptional situations (then everything is started from scratch).

Which process is responsible for starting the children? Can’t this process also restart them after a crash?

The children were started in application.ex immediately after the supervision tree was created in the start function.

If you know what children you have at application start, why do you use a DynamicSupervisor?

From the documentation

A supervisor that starts children dynamically.

The Supervisor module was designed to handle mostly static children that are started in the given order when the supervisor starts. A DynamicSupervisor starts with no children. Instead, children are started on demand via start_child/2. When a dynamic supervisor terminates, all children are shutdown at the same time, with no guarantee of ordering.

So if you know them at startup, I would use a Supervisor?

Maybe if you can show some code that would help.

1 Like