Search code examples
processerlangerlang-supervisor

Dynamic supervisor starts itself as a child supervisor


I need a supervisor who can start two kinds of childen.

the first is a supervisor child, like the supervisor, can i start a child with the same module like the supervisor?

For Example:

-module(test_sup).
-behaviour(supervisor).

-export([start_link/0]).
-export([init/1]).

start_link() ->
     supervisor:start_link({local,?MODULE}, ?MODULE, []).

init(_Args) ->
        RestartStrategy = {one_for_one, 10, 60},
        {ok, {RestartStrategy, 

        [{sup,
        {sup, start_link, []},
        permanent, infinity, supervisor, [sup]},

  ]}}.

in the supervisor module i init a child with the same module, is it possible?

the second kind of child is a normal worker, with a own module, this is not the problem. But can i starts dynamic this two different kinds in the supervisor module?


Solution

  • In your comment, you said that you want to start up a process for every subfolder you run into. OTP created the simple_one_for_one supervisor for that.

    In the start_link of the worker process, you can supply the directory path you want the process to index as the argument.

    You can look at my full text search engine tutorial for a working example of a simple_one_for_one supervisor.