erlangerlang-supervisor

Erlang: starting sellaprime app with supervisor returns undef error


Trying to run the example app, sellaprime app, from the programming erlang book. I tested the supervisor code with the testing function within the supervisor. So, the supervisor should be OK. The application gets loaded but the application start runs into problem with below error:

=INFO REPORT==== 31-May-2021::22:39:44.235167 ===
application: sellaprime
exited: {bad_return,
            {{sellaprime_app,start,[normal,[]]},
             {'EXIT',
                 {undef,
                     [{sellaprime_app,start,[normal,[]],[]},
                      {application_master,start_supervisor,3,
                          [{file,"application_master.erl"},{line,331}]},
                      {application_master,start_the_app,5,
                          [{file,"application_master.erl"},{line,313}]},
                      {application_master,start_it_new,7,
                          [{file,"application_master.erl"},
                           {line,299}]}]}}}}
type: temporary

Need help locating the error. Here is the file, sellaprime_app.erl:

-module(sellaprime_app).
-behaviour(application).
-export([start/2, stop/1]).

start(_Type, StartArgs) -> sellaprime_supervisor:start_link(StartArgs).
stop(_State) -> ok.

and the sellaprime_supervisor.erl:

-module(sellaprime_supervisor).
-behaviour(supervisor).

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

start() -> 
    spawn(fun() -> supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg=[]) end).
start_in_shell_for_testing() -> 
    {ok, Pid} = supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg=[]),
    unlink(Pid).

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

init([]) ->
    gen_event:swap_handler(alarm_handler, {alarm_handler, swap}, {my_alarm_handler, xyz}),
    {ok, {{one_for_one, 3, 10},
         [{tag1, 
            {area_server, start_link, []},
            permanent, 
            10000, 
            worker, 
            [area_server]},
          {tag2, 
            {prime_server, start_link, []},
            permanent, 
            10000, 
            worker, 
            [prime_server]}
        ]}}.

Solution

  • This:

    {'EXIT',
                     {undef,
                         [{sellaprime_app,start,[normal,[]],[]},
    

    is saying that there is an undef error, namely that in the module sellaprime_app there is no function defined called start(), which takes the two arguments: [normal, []]. But, if you look at your sellaprime_app.erl, there is clearly a function named start() and it's defined to take any two arguments. So what's going on?

    The problem is that the book doesn't instruct you to compile the file sellaprime_app.erl to create the required .beam file, so erlang can't find the functions therein. You will have the same problem with all the other source files. You can use the command line to compile all the .erl files in a directory, like this:

    $ erlc *.erl
    

    After you do that, you will see the .beam files in the directory. Then you can do:

    $ erl -boot start_sasl -config elog3
    

    ...followed by the rest of the commands shown in the book.

    Tools like rebar3 take care of doing all the compiling for you and organizing the .beam files into other directories and adding their paths, but that doesn't happen when you are running applications by hand.