Search code examples
erlangerlang-supervisor

How to run a supervisor with workers which are not gen_servers?


Hello i am trying to run a supervisor whose workers are not gen_server(s). My supervisor is defined in the same module as the worker for brevity:

I keep getting this error and i have tried putting the MFA attribute in [ ] to no avail.I have also put the ChildSpec in [ ]. What am i missing ?

I do not want my supervisor to have any workers when starting it.

Error

>  X=sup:start_link().
> ** exception error: no match of right hand side value {error,
>                                                        {bad_start_spec,[]}}
>      in function  sup:start_link/0 (c:/Erlang/ProcessPool/sup.erl, line 6)
> =CRASH REPORT==== 5-Apr-2020::22:20:32.918000 ===   crasher:
>     initial call: supervisor:sup/1
>     pid: <0.280.0>
>     registered_name: []
>     exception exit: {bad_start_spec,[]}
>       in function  gen_server:init_it/6 (gen_server.erl, line 358)
>     ancestors: [<0.273.0>]
>     message_queue_len: 0
>     messages: []
>     links: [<0.273.0>]
>     dictionary: []
>     trap_exit: true
>     status: running
>     heap_size: 376
>     stack_size: 27
>     reductions: 205   neighbours:

Module

-module(sup).
-behaviour(supervisor).
-compile([export_all]).

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

init(_Args) ->
     RestartStrategy = {simple_one_for_one, 10, 60},
    {ok, {RestartStrategy,[]}}.

add(Sup,Value)->                #adding child
    ChildSpec = {
                  ch1, 
                  {sup, start, [Value]},
                  permanent,
                  brutal_kill, 
                  worker, 
                  [ch1]
                },
    supervisor:start_child(Sup,ChildSpec).


start([Value])->                                    #child's start_link equivalent (non-genserver)
    spawn_link(?MODULE,initworker,[self(),Value]).

initworker(From,Value)->                            #child's init
    receive 
       MSG->From ! {got_msg,Value,MSG}
    end.

Solution

  • As you using simple_one_for_one, You should define ChildSpec in init and all children use the same ChildSpec .

    If you need different, then use 'one_for_one' strategy instead.

    For simple_one_for_one:

    The second argument of supervisor:start_child/2 must be a list which will be combined with default arguments to child start function arguments defined in ChildSpec.

    Here I quickly modified the code to make it work for you.

    -module(sup).
    -behaviour(supervisor).
    -compile([export_all]).
    
    start_link() ->
         {ok, Pid} = supervisor:start_link({local, ?MODULE}, ?MODULE, []),
         Pid.
    
    init(_Args) ->
         RestartStrategy = {simple_one_for_one, 10, 60},
         ChildSpec = {
                      ch1, 
                      {sup, start, []},
                      permanent,
                      brutal_kill, 
                      worker,
                      [sup]
                    },
        {ok, {RestartStrategy,[ChildSpec]}}.
    
    add(Sup,Value)->                
        supervisor:start_child(Sup,[Value]).
    
    
    start(Value)->                                    
        P = spawn(?MODULE, initworker,[]),
        P!{self(),Value},
        {ok,P}.
    
    initworker()->                            
        receive 
           {From, MSG} -> io:format(" Value is ~p~n", [MSG])
        end.