tomtaylor
tomtaylor

Reputation: 2309

Starting dynamic simple_one_for_one workers after supervisor starts

I have a named Supervisor that supervises many simple_one_for_one workers, which perform a task at regular intervals for each user on my system.

I want the app to start one worker for each user when the Supervisor starts for the first time, and I want the app to do the same if the Supervisor restarts at a later date, for whatever reason.

At the moment I start all the children dynamically in the Application's start(type, args) callback, but if the Supervisor is restarted, that won't start all the child processes.

How do I ensure all the dynamic child workers are started immediately after the Supervisor starts/restart, at any time?

(My app is in Elixir, but the same principles apply to Erlang.)

Upvotes: 4

Views: 465

Answers (1)

Roger Lipscombe
Roger Lipscombe

Reputation: 91805

One way I've done this that works pretty well is to use another supervisor and a "restart worker". Your supervisor is a child of the new one -- it's a sibling of the restart worker. The new supervisor uses one_for_all or rest_for_one, so that if your supervisor dies, the restart worker is also restarted.

When the restart worker is restarted, it can start the dynamic workers.

Upvotes: 6

Related Questions