简体   繁体   English

在主管启动后启动动态simple_one_for_one工作人员

[英]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. 我有一个名为Supervisor ,监督许多simple_one_for_one工作者,它们定期为我的系统上的每个用户执行任务。

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. 我希望应用程序在Supervisor第一次启动时为每个用户启动一个worker,如果Supervisor在以后重新启动,我希望应用程序执行相同操作,无论出于何种原因。

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. 目前,我在Applicationstart(type, args)回调中动态启动所有子项,但如果Supervisor重新启动,则不会启动所有子进程。

How do I ensure all the dynamic child workers are started immediately after the Supervisor starts/restart, at any time? Supervisor启动/重启后,如何确保所有动态子工作者立即启动?

(My app is in Elixir, but the same principles apply to Erlang.) (我的应用程序在Elixir中,但同样的原则适用于Erlang。)

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. 新主管使用one_for_allrest_for_one ,这样如果您的主管死亡,重启工作人员也会重新启动。

When the restart worker is restarted, it can start the dynamic workers. 重新启动重新启动工作程序时,它可以启动动态工作程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM