Bulduing erlang application with rebar3 shows error failed to boot the app_name for reason

I have been learning how to build erlang application with rebar3. What I have done is firstely; creating new app template using the below command

rebar3 new app chat

then, I copied my hole project to the src folder of the template that I created. Finally, I altered the chat_sup.erl to look like below:

-module(chat_sup).
 -behaviour(supervisor).
 -export([start_link/0]).
-export([init/1]).

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

init([]) ->
    SupFlags = #{strategy => one_for_all,
                 intensity => 3,
                 period => 3},
    ChildSpecs = [
        #{id => worker, start => {chatapp_server, start_link, []}}
    ],
    {ok, {SupFlags, ChildSpecs}}.


where, chatapp_server the server file to be supervised and it looks like below

-module(chatapp_server).
-export([start/0, start_link/0, stop/0,wind_rooms/0]).
-export([init/0]).
-include("chatapp_config.hrl").

start() -> 
	register(?SERVER, spawn(?MODULE, init, [])).

start_link() ->
	register(?SERVER, spawn_link(?MODULE, init, [])).
     
......................
     .........................
         ............................
	end.

I kept the chat_app.erl as it with no changes which is:

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

start(_StartType, _StartArgs) ->
    chat_sup:start_link().

stop(_State) ->
    ok.

when I run the project using:

rebar3 shell

I got the follwoing errors:

PS F:\chat> rebar3 shell
===> Verifying dependencies

===> Analyzing applications

===> Compiling chat
Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]
=SUPERVISOR REPORT==== 8-Aug-2024::03:50:48.655000 ===
supervisor: {local,chat_sup}
errorContext: start_error
reason: true
offender: [{pid,undefined},
{id,worker},
{mfargs,{chatapp_server,start_link,}},
{restart_type,permanent},
{significant,false},
{shutdown,5000},
{child_type,worker}]
===> Failed to boot chat for reason {{shutdown,
{failed_to_start_child,worker,true}},
{chat_app,start,[normal,]}}
=INFO REPORT==== 8-Aug-2024::03:50:48.656000 ===
application: chat
exited: {{shutdown,{failed_to_start_child,worker,true}},
{chat_app,start,[normal,]}}
type: temporary
=CRASH REPORT==== 8-Aug-2024::03:50:48.656000 ===
crasher:
initial call: application_master:init/3
pid: <0.229.0>
registered_name:
exception exit: {{shutdown,{failed_to_start_child,worker,true}},
{chat_app,start,[normal,]}}
in function application_master:init/3 (application_master.erl, line 143)
ancestors: [application_controller,<0.10.0>]
message_queue_len: 1
messages: [{‘EXIT’,<0.230.0>,normal}]
links: [<0.45.0>]
dictionary:
trap_exit: true
status: running
heap_size: 233
stack_size: 29
reductions: 68
neighbours:
Eshell V15.0 (press Ctrl+G to abort, type help(). for help)

what the wrong I am doing here please ?

It looks like you aren’t writing the gen_server correctly. I would follow along this example for gen_servers:

https://www.erlang.org/doc/system/gen_server_concepts.html#example

Then for the supervisor you can reference this example:

https://www.erlang.org/doc/system/sup_princ.html#example

My project is not a gen_server, it is special process,so, i want to make an application of it using rebar3.

You are right, you don’t need a supervisor’s child to implement the gen_server behaviour, but you need your child’s start function to return something that its supervisor can understand.

You’ve specified chatapp_server:start_link as your child’s start function, and this one calls register/2 which return type according to its spec (here) tells you it always returns true.

If you check the docs you’ll see that your child start function should return {ok, Pid} or {ok, Pid, Info}.

The start function must create and link to the child process, and must return {ok,Child} or {ok,Child,Info} , where Child is the pid of the child process and Info any term that is ignored by the supervisor.

1 Like

Actually, and for some reasons, I have to use special process not gen_server. So, how to reformulate the child’s start function in order to be understood by the supervisor please ?

Sounds like an assignment :confused:

Quick Google search should have done the trick erlang - How to run a supervisor with workers which are not gen_servers? - Stack Overflow

Actually, it is not!.Anyway, based on your prevouis reply, I altered the chatapp_server module into

start_link() ->
         Pid = spawn_link(?MODULE, init, []),
	 register(?SERVER,Pid),
         {ok,Pid}.

And the application started just fine. However, when I try to stop the app using

application:stop(chat).

I get the follwoing error report

=ERROR REPORT==== 9-Aug-2024::16:22:40.215000 ===
Error in process <0.89.0> with exit value:
{{badmatch,},
[{chatapp_server,loop,1,
[{file,“f:/chat/src/chatapp_server.erl”},{line,294}]}]}
=SUPERVISOR REPORT==== 9-Aug-2024::16:22:40.215000 ===
supervisor: {local,chat_sup}
errorContext: shutdown_error
reason: {{badmatch,},
[{chatapp_server,loop,1,
[{file,“f:/chat/src/chatapp_server.erl”},
{line,294}]}]}
offender: [{pid,<0.89.0>},
{id,worker},
{mfargs,{chatapp_server,start_link,}},
{restart_type,permanent},
{significant,false},
{shutdown,5000},
{child_type,worker}]
=INFO REPORT==== 9-Aug-2024::16:22:40.238000 ===
application: chat
exited: stopped
type: temporary
ok

from error report, it looks that the application is stopped, but why I am geeting that error please ?

Sounds like an assignment :confused:

Hey @falsal, I realized this response of mine is not adequate specially for someone trying to learn a technology I enjoy so much. Sorry for that. Did you manage to make this work?

1 Like

The problem is that start MFA in supervisor’s child spec returns true (call to register/2 always returns true, while in [the docs] it says that start should return {ok, ChildPid}.

So, the fix should look something like:

-module(chatapp_server).
-export([start/0, start_link/0, stop/0,wind_rooms/0]).
-export([init/0]).
-include("chatapp_config.hrl").

start() ->
        Pid = spawn(?MODULE, init, []),
        register(?SERVER, Pid),
        {ok, Pid}.

start_link() ->
       Pid = spawn_link(?MODULE, init, []),
       register(?SERVER, Pid),
       {ok, Pid}.

No problem! actually, I fixed it based on your first reply long time ago . Same solution as @mmin
Thank you both. I appreciate your helps. However, I have posted another topic about the same matter here . I hope you have a look at it please !!

1 Like

Many Thanks