Why is my cowboy server not running? - erlang

I'm trying to run my server. I copied my src files into a fresh cowboy install, this is the error I'm getting.
~/cowboy_ninja:.ls src
action_handler.erl gate.beam resolution.beam
arena.beam gate.erl resolution.erl
arena.erl guestbook.beam temple.beam
cowboy_ninja_app.erl guestbook.erl temple.erl
cowboy_ninja_sup.erl hello_handler.erl
~/cowboy_ninja:.
cat src/cowboy_ninja_app.erl
-module(cowboy_ninja_app).
-behaviour(application).
-export([start/2]).
-export([stop/1]).
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/action", action_handler, []},
{"/join", hello_handler, []}]}
]),
{ok, _} = cowboy:start_clear(my_http_listener, 100,
[{port, 8080}],
#{env => #{dispatch => Dispatch}}
),
{ok, Pid} = gate:start_link(),
register(gate, Pid),
{ok, Guestbook} = guestbook:start_link(),
register(guestbook, Guestbook),
gate:action(Pid, {fight}),
cowboy_ninja_sup:start_link().
stop(_State) ->
ok.
See below.
~/cowboy_ninja:.gmake run
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
/Users/quantum/cowboy_ninja/ebin
/Users/quantum/cowboy_ninja/deps
/usr/local/Cellar/erlang/19.1/lib/erlang/lib
/Users/quantum/cowboy_ninja/apps
/Users/quantum/cowboy_ninja/_rel
===> Resolved cowboy_ninja_release-1
===> Including Erts from /usr/local/Cellar/erlang/19.1/lib/erlang
===> release successfully created!
===> tarball /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/cowboy_ninja_release-1.tar.gz successfully created!
Exec: /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/erts-8.1/bin/erlexec -boot /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/releases/1/cowboy_ninja_release -mode embedded -boot_var ERTS_LIB_DIR /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/erts-8.1/../lib -config /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/releases/1/sys.config -args_file /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release/releases/1/vm.args -- console
Root: /Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release
/Users/quantum/cowboy_ninja/_rel/cowboy_ninja_release
heart_beat_kill_pid = 52128
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
=INFO REPORT==== 1-Jan-2017::17:03:57 ===
application: cowboy_ninja
exited: {bad_return,
{{cowboy_ninja_app,start,[normal,[]]},
{'EXIT',
{undef,
[{cowboy_router,compile,
[[{'_',
[{"/action",action_handler,[]},
{"/join",hello_handler,[]}]}]],
[]},
{cowboy_ninja_app,start,2,
[{file,"src/cowboy_ninja_app.erl"},{line,8}]},
{application_master,start_it_old,4,
[{file,"application_master.erl"},
{line,273}]}]}}}}
type: permanent
{"Kernel pid terminated",application_controller,"{application_start_failure,cowboy_ninja,{bad_return,{{cowboy_ninja_app,start,[normal,[]]},{'EXIT',{undef,[{cowboy_router,compile,[[{'_',[{\"/action\",action_handler,[]},{\"/join\",hello_handler,[]}]}]],[]},{cowboy_ninja_app,start,2,[{file,\"src/cowboy_ninja_app.erl\"},{line,8}]},{application_master,start_it_old,4,[{file,\"application_master.erl\"},{line,273}]}]}}}}}"}
Kernel pid terminated (application_controller) ({application_start_failure,cowboy_ninja,{bad_return,{{cowboy_ninja_app,start,[normal,[]]},{'EXIT',{undef,[{cowboy_router,compile,[[{'_',[{"/action",acti
heart: Sun Jan 1 17:03:58 2017: Erlang is crashing .. (waiting for crash dump file)
heart: Sun Jan 1 17:03:58 2017: Would reboot. Terminating.
gmake: *** [erlang.mk:6448: run] Error 1
It looks like it can't find my files? What is this error message saying?

You need to create a cowboy_ninja.app file and list all the applications you are using (cowboy, etc.). Relx will look at that file to see which applications it needs to include in your release.
http://learnyousomeerlang.com/building-otp-applications
Currently it is not including the cowboy application or any of it's compiled modules into your release.
Here is an example from the cowboy examples
{application, 'hello_world', [
{description, "Cowboy Hello World example"},
{vsn, "1"},
{modules, ['hello_world_app','hello_world_sup','toppage_handler']},
{registered, [hello_world_sup]},
{applications, [kernel,stdlib,cowboy]},
{mod, {hello_world_app, []}},
{env, []}
]}.
You also need to tell relx where to look for your applications (in your relx.config).
{lib_dirs, [
"/usr/local/lib/elixir/lib/*/ebin", "./_build/prod/lib/*/ebin", "./deps/*/ebin"]}.
If you are not using a build tool for your app take a look at erlang.mk. it automatically creates an app file for you.

Related

rebar3 undef when running release console

I've been playing around with rebar3 (3.15.0) just to get it to run with the basic templates, and running into issues just trying to get a simple Hello World type example program going. I am starting with the template command to build a new release:
rebar3 new release myfirstproj.
It works fine when I run rebar3 shell but gives me the error below when running the release command: myfirstproj.cmd console after building it with rebar3 release.
The only thing I've added to the base template is a io:format("Hello world!") in the supervisor.. It seems like the undef below is indicating it can't find the myfirstproj_app:start function.. any ideas why it doesn't work here but works in the rebar3 shell?
OTP 23 [erts-11.0] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:30]
=CRASH REPORT==== 21-Apr-2021::22:39:52.740000 ===
crasher:
initial call: application_master:init/4
pid: <0.85.0>
registered_name: []
exception exit: {bad_return,
{{myfirstproj_app,start,[normal,[]]},
{'EXIT',
{undef,
[{myfirstproj_app,start,[normal,[]],[]},
{application_master,start_it_old,4,
[{file,"application_master.erl"},
{line,277}]}]}}}}
in function application_master:init/4 (application_master.erl, line 138)
ancestors: [<0.83.0>]
message_queue_len: 1
messages: [{'EXIT',<0.85.0>,normal}]
links: [<0.83.0>,<0.44.0>]
dictionary: []
trap_exit: true
status: running
heap_size: 376
stack_size: 28
reductions: 224
neighbours:
=INFO REPORT==== 21-Apr-2021::22:39:21.439000 ===
application: myfirstproj
exited: {bad_return,
{{myfirstproj_app,start,[normal,[]]},
{'EXIT',
{undef,
[{myfirstproj_app,start,[normal,[]],[]},
{application_master,start_it_old,4,
[{file,"application_master.erl"},
{line,277}]}]}}}}
type: permanent
My app code hasn't been changed from the template:
%%%-------------------------------------------------------------------
%% #doc myfirstproj public API
%% #end
%%%-------------------------------------------------------------------
-module(myfirstproj_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
myfirstproj_sup:start_link().
stop(_State) ->
ok.
%% internal functions
Same with my app.src:
{application, myfirstproj,
[{description, "An OTP application"},
{vsn, "0.1.0"},
{registered, []},
{mod, {myfirstproj_app, []}},
{applications,
[kernel,
stdlib
]},
{env,[]},
{modules, []},
{licenses, ["Apache 2.0"]},
{links, []}
]}.
The rebar.config hasn't been modified from the template.
myfirstproj_sup.erl:
%%%-------------------------------------------------------------------
%% #doc myfirstproj top level supervisor.
%% #end
%%%-------------------------------------------------------------------
-module(myfirstproj_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
-define(SERVER, ?MODULE).
start_link() ->
io:format("Hey, we started!"),
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
init([]) ->
SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1},
%ChildSpecs = [{worker1,
%{file_watcher, start_link, []}, permanent, 1000, worker, [file_watcher]}],
ChildSpecs = [],
{ok, {SupFlags, ChildSpecs}}.
%% internal functions

How do you use Gun as a Cowboy client?

I followed the Getting Started instructions for Cowboy, and I've got Cowboy running and listening on port 8080, and I got the Hello Erlang! response when I entered http://localhost:8080 in my browser. Now, how do I use Gun to connect to Cowboy?
I read the Gun docs, and it says to add "Gun as an erlang.mk dependency". So I downloaded erlang.mk:
~/erlang_programs/my_gun$ curl -O https://erlang.mk/erlang.mk
and following the Erlang.mk User Guide, I created an application:
~/erlang_programs/my_gun$ gmake -f erlang.mk bootstrap
Then I added gun as a dependency to the Makefile:
PROJECT = my_gun
PROJECT_DESCRIPTION = New project
PROJECT_VERSION = 0.1.0
DEPS = gun
include erlang.mk
Then I compiled:
~/erlang_programs/my_gun$ gmake
gmake[1]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
ERLC cow_cookie.erl cow_date.erl cow_hpack.erl cow_http.erl cow_http2.erl cow_http_hd.erl cow_http_te.erl cow_mimetypes.erl cow_multipart.erl cow_qs.erl cow_spdy.erl cow_sse.erl cow_uri.erl cow_ws.erl
APP cowlib
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
DEPEND ranch.d
ERLC ranch.erl ranch_acceptor.erl ranch_acceptors_sup.erl ranch_app.erl ranch_conns_sup.erl ranch_listener_sup.erl ranch_protocol.erl ranch_server.erl ranch_ssl.erl ranch_sup.erl ranch_tcp.erl ranch_transport.erl
APP ranch
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
DEPEND gun.d
ERLC gun.erl gun_app.erl gun_content_handler.erl gun_data.erl gun_http.erl gun_http2.erl gun_spdy.erl gun_sse.erl gun_sup.erl gun_ws.erl gun_ws_handler.erl
APP gun
GEN rebar.config
gmake[1]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
DEPEND my_gun.d
ERLC my_gun_app.erl my_gun_sup.erl
APP my_gun
GEN /Users/7stud/erlang_programs/my_gun/.erlang.mk/relx
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
/Users/7stud/erlang_programs/my_gun/ebin
/Users/7stud/erlang_programs/my_gun/deps
/Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang/lib
/Users/7stud/erlang_programs/my_gun/apps
===> Resolved my_gun_release-1
===> rendering builtin_hook_status hook to "/Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/bin/hooks/builtin/status"
===> Including Erts from /Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang
===> release successfully created!
===> tarball /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/my_gun_release-1.tar.gz successfully created!
But when I switch to the erlang shell and try to start gun, I get an error:
~/erlang_programs/my_gun$ erl
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V8.2 (abort with ^G)
1> application:ensure_all_started(gun).
{error,{gun,{"no such file or directory","gun.app"}}}
Can someone post a simple example of how to use Gun (or any other http client with websocket support) to connect to Cowboy?
Okay, I made some progress. I deleted the my_gun directory, recreated the directory, redownloaded erlang.mk, and created a release with the following command:
~/erlang_programs/my_gun$ gmake -f erlang.mk bootstrap-lib bootstrap-rel
Then I added the gun dependency to the Makefile (as described above). Then I did:
~/erlang_programs/my_gun$ gmake run
If there are no errors in the code, then an erlang shell will launch. In the erlang shell, I entered the following code (see the tip below to avoid having to type all the code in the shell):
(my_gun#127.0.0.1)1> application:ensure_all_started(gun).
{ok,[]}
(my_gun#127.0.0.1)2> {ok, ConnPid} = gun:open("localhost", 8080).
=PROGRESS REPORT==== 10-Jul-2017::05:21:58 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.366.0>},{mfa,{inet_gethost_native,init,[[]]}}]
{ok,<0.364.0>}
=PROGRESS REPORT==== 10-Jul-2017::05:21:58 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.365.0>},
{id,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
(my_gun#127.0.0.1)3> {ok, Protocol} = gun:await_up(ConnPid).
{ok,http}
(my_gun#127.0.0.1)4> gun:ws_upgrade(ConnPid, "/websocket").
#Ref<0.0.3.244>
(my_gun#127.0.0.1)5> receive
(my_gun#127.0.0.1)5> {gun_ws_upgrade, ConnPid, ok, Headers} ->
(my_gun#127.0.0.1)5> upgrade_success(ConnPid);
(my_gun#127.0.0.1)5> {gun_response, ConnPid, _, _, Status, Headers} ->
(my_gun#127.0.0.1)5> exit({ws_upgrade_failed, Status, Headers});
(my_gun#127.0.0.1)5> {gun_error, ConnPid, StreamRef, Reason} ->
(my_gun#127.0.0.1)5> exit({ws_upgrade_failed, Reason})
(my_gun#127.0.0.1)5> after 1000 ->
(my_gun#127.0.0.1)5> exit(timeout)
(my_gun#127.0.0.1)5> end.
=CRASH REPORT==== 10-Jul-2017::05:25:17 ===
crasher:
initial call: gun:proc_lib_hack/5
pid: <0.364.0>
registered_name: []
exception exit: {{owner_gone,normal},
[{gun,loop,1,[{file,"src/gun.erl"},{line,706}]},
{gun,proc_lib_hack,5,[{file,"src/gun.erl"},{line,535}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,247}]}]}
in function gun:proc_lib_hack/5 (src/gun.erl, line 540)
ancestors: [gun_sup,<0.343.0>]
messages: []
links: [<0.344.0>]
dictionary: []
trap_exit: false
status: running
heap_size: 2586
stack_size: 27
reductions: 10857
neighbours:
=SUPERVISOR REPORT==== 10-Jul-2017::05:25:17 ===
Supervisor: {local,gun_sup}
Context: child_terminated
Reason: {{owner_gone,normal},
[{gun,loop,1,[{file,"src/gun.erl"},{line,706}]},
{gun,proc_lib_hack,5,[{file,"src/gun.erl"},{line,535}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,247}]}]}
Offender: [{pid,<0.364.0>},
{id,gun},
{mfargs,{gun,start_link,undefined}},
{restart_type,temporary},
{shutdown,5000},
{child_type,worker}]
** exception exit: {ws_upgrade_failed,404,
[{<<"content-length">>,<<"0">>},
{<<"date">>,
<<"Mon, 10 Jul 2017 11:22:38 GMT">>},
{<<"server">>,<<"Cowboy">>}]}
(my_gun#127.0.0.1)6>
Now, I'm getting a 404 error. Cowboy is running, and when I enter http://localhost:8080 in my browser, I see a response message. Why is Gun giving me a 404 error?
Next I tried using the instructions in the Gun docs to make a GET request:
StreamRef = gun:get(ConnPid, "/").
case gun:await(ConnPid, StreamRef) of
{response, fin, Status, Headers} ->
no_data;
{response, nofin, Status, Headers} ->
{ok, Body} = gun:await_body(ConnPid, StreamRef),
io:format("~s~n", [Body])
end.
and that was successful:
=PROGRESS REPORT==== 10-Jul-2017::06:36:14 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.367.0>},{mfa,{inet_gethost_native,init,[[]]}}]
=PROGRESS REPORT==== 10-Jul-2017::06:36:14 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.366.0>},
{id,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
Hello Erlang!
ok
(my_gun#127.0.0.1)2>
The response means that I was able to use Gun to interact with a Cowboy server--but I want to use websockets. Any ideas what I am doing wrong?
tip:
To avoid having to type all that code in the gun shell, I created the file ~/erlang_programs/my_gun/src/my.erl:
-module(my).
-compile(export_all).
get() ->
{ok, _} = application:ensure_all_started(gun),
{ok, ConnPid} = gun:open("localhost", 8080),
{ok, _Protocol} = gun:await_up(ConnPid),
StreamRef = gun:get(ConnPid, "/"),
case gun:await(ConnPid, StreamRef) of
{response, fin, _Status, _Headers} ->
no_data;
{response, nofin, _Status, _Headers} ->
{ok, Body} = gun:await_body(ConnPid, StreamRef),
io:format("~s~n", [Body])
end.
ws() ->
{ok, _} = application:ensure_all_started(gun),
{ok, ConnPid} = gun:open("localhost", 8080),
{ok, _Protocol} = gun:await_up(ConnPid),
gun:ws_upgrade(ConnPid, "/websocket"),
receive
{gun_ws_upgrade, ConnPid, ok, Headers} ->
upgrade_success(ConnPid, Headers);
{gun_response, ConnPid, _, _, Status, Headers} ->
exit({ws_upgrade_failed, Status, Headers});
{gun_error, _ConnPid, _StreamRef, Reason} ->
exit({ws_upgrade_failed, Reason})
%% More clauses here as needed.
after 1000 ->
exit(timeout)
end,
gun:shutdown(ConnPid).
upgrade_success(ConnPid, Headers) ->
io:format("Upgraded ~w. Success!~nHeaders:~n~p~n",
[ConnPid, Headers]).
Then the make (or gmake) command:
~/erlang_programs/my_gun$ gmake run
will compile everything in the src/ directory and alert you to any errors. Once the gun shell successfully launches in response to gmake run, you can for instance do:
(my_gun#127.0.0.1)1> my:get().
=PROGRESS REPORT==== 10-Jul-2017::06:36:14 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.367.0>},{mfa,{inet_gethost_native,init,[[]]}}]
=PROGRESS REPORT==== 10-Jul-2017::06:36:14 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.366.0>},
{id,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
Hello Erlang!
ok
(my_gun#127.0.0.1)2>
Response to comment:
Since you're getting a 404, I guess you don't have a websocket handler
defined in the cowboy routes.
You are right. I only had the handler shown in the Cowboy Getting Started guide. Now, I've added the websocket setup code and a websocket handler to cowboy. I now have the routes:
hello_erlang_app.erl:
-module(hello_erlang_app).
-behaviour(application).
-export([start/2]).
-export([stop/1]).
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}] },
{'_', [{"/websocket", myws_handler, []}] }
]),
{ok, _} = cowboy:start_clear(my_http_listener,
[{port, 8080}],
#{env => #{dispatch => Dispatch} }
),
hello_erlang_sup:start_link().
stop(_State) ->
ok.
Here's my handler:
-module(myws_handler).
-compile(export_all).
init(Req, State) ->
{cowboy_websocket, Req, State}. %Perform websocket setup
websocket_handle({text, Msg}, State) ->
{
reply,
{text, <<"Server received: ", Msg/binary>>, State} %%Error in format here, too!
};
websocket_handle(_Data, State) ->
{ok, State}.
But I'm still getting a 404 error when I execute my:ws() in the gun shell:
-module(my).
-compile(export_all).
get() ->
{ok, _} = application:ensure_all_started(gun),
{ok, ConnPid} = gun:open("localhost", 8080),
{ok, _Protocol} = gun:await_up(ConnPid),
StreamRef = gun:get(ConnPid, "/"),
case gun:await(ConnPid, StreamRef) of
{response, fin, _Status, _Headers} ->
no_data;
{response, nofin, _Status, _Headers} ->
{ok, Body} = gun:await_body(ConnPid, StreamRef),
io:format("~s~n", [Body])
end.
ws() ->
{ok, _} = application:ensure_all_started(gun),
{ok, ConnPid} = gun:open("localhost", 8080),
{ok, _Protocol} = gun:await_up(ConnPid),
gun:ws_upgrade(ConnPid, "/websocket"),
receive
{gun_ws_upgrade, ConnPid, ok, Headers} ->
upgrade_success(ConnPid, Headers);
{gun_response, ConnPid, _, _, Status, Headers} ->
exit({ws_upgrade_failed, Status, Headers});
{gun_error, _ConnPid, _StreamRef, Reason} ->
exit({ws_upgrade_failed, Reason})
%% More clauses here as needed.
after 1000 ->
exit(timeout)
end,
gun:shutdown(ConnPid).
upgrade_success(ConnPid, Headers) ->
io:format("Upgraded ~w. Success!~nHeaders:~n~w~n",
[ConnPid, Headers]).
Here's the output:
~/erlang_programs/my_gun$ gmake run
gmake[1]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
GEN rebar.config
gmake[1]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
DEPEND my_gun.d
ERLC my.erl
APP my_gun
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
/Users/7stud/erlang_programs/my_gun/ebin
/Users/7stud/erlang_programs/my_gun/deps
/Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang/lib
/Users/7stud/erlang_programs/my_gun/apps
/Users/7stud/erlang_programs/my_gun/_rel
===> Resolved my_gun_release-1
===> rendering builtin_hook_status hook to "/Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/bin/hooks/builtin/status"
===> Including Erts from /Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang
===> release successfully created!
===> tarball /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/my_gun_release-1.tar.gz successfully created!
Exec: /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/erts-8.2/bin/erlexec -boot /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/my_gun_release -mode embedded -boot_var ERTS_LIB_DIR /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/lib -config /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/sys.config -args_file /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/vm.args -pa -- console
Root: /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release
/Users/7stud/erlang_programs/my_gun/_rel/my_gun_release
heart_beat_kill_pid = 32843
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
=PROGRESS REPORT==== 10-Jul-2017::16:26:05 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.353.0>},
{id,alarm_handler},
{mfargs,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::16:26:05 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.352.0>},
{id,sasl_safe_sup},
{mfargs,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe]}},
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
=PROGRESS REPORT==== 10-Jul-2017::16:26:05 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.354.0>},
{id,release_handler},
{mfargs,{release_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::16:26:05 ===
application: sasl
started_at: 'my_gun#127.0.0.1'
=PROGRESS REPORT==== 10-Jul-2017::16:26:05 ===
supervisor: {local,runtime_tools_sup}
started: [{pid,<0.360.0>},
{id,ttb_autostart},
{mfargs,{ttb_autostart,start_link,[]}},
{restart_type,temporary},
{shutdown,3000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::16:26:05 ===
application: runtime_tools
started_at: 'my_gun#127.0.0.1'
Eshell V8.2 (abort with ^G)
(my_gun#127.0.0.1)1> my:ws().
=PROGRESS REPORT==== 10-Jul-2017::16:26:08 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.367.0>},{mfa,{inet_gethost_native,init,[[]]}}]
=PROGRESS REPORT==== 10-Jul-2017::16:26:08 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.366.0>},
{id,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
=CRASH REPORT==== 10-Jul-2017::16:26:08 ===
crasher:
initial call: gun:proc_lib_hack/5
pid: <0.365.0>
registered_name: []
exception exit: {{owner_gone,normal},
[{gun,loop,1,[{file,"src/gun.erl"},{line,706}]},
{gun,proc_lib_hack,5,[{file,"src/gun.erl"},{line,535}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,247}]}]}
in function gun:proc_lib_hack/5 (src/gun.erl, line 540)
ancestors: [gun_sup,<0.345.0>]
messages: []
links: [<0.346.0>]
dictionary: []
trap_exit: false
status: running
heap_size: 610
stack_size: 27
reductions: 1042
neighbours:
=SUPERVISOR REPORT==== 10-Jul-2017::16:26:08 ===
Supervisor: {local,gun_sup}
Context: child_terminated
Reason: {{owner_gone,normal},
[{gun,loop,1,[{file,"src/gun.erl"},{line,706}]},
{gun,proc_lib_hack,5,[{file,"src/gun.erl"},{line,535}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,247}]}]}
Offender: [{pid,<0.365.0>},
{id,gun},
{mfargs,{gun,start_link,undefined}},
{restart_type,temporary},
{shutdown,5000},
{child_type,worker}]
** exception exit: {ws_upgrade_failed,404,
[{<<"content-length">>,<<"0">>},
{<<"date">>,
<<"Mon, 10 Jul 2017 22:26:08 GMT">>},
{<<"server">>,<<"Cowboy">>}]}
in function my:ws/0 (src/my.erl, line 30)
(my_gun#127.0.0.1)2>
I saved all my files and restarted cowboy and gun, so the changes I made to the code are being executed, but I still get the 404 error.
I compared the format of my routes to the routes in the example that spawn_think linked to in the comments, and my format was wrong. Here is what I have now:
-module(hello_erlang_app).
-behaviour(application).
-export([start/2]).
-export([stop/1]).
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/", hello_handler, []},
{"/websocket", myws_handler, []}
]}
]),
{ok, _} = cowboy:start_clear(my_http_listener,
[{port, 8080}],
#{env => #{dispatch => Dispatch} }
),
hello_erlang_sup:start_link().
stop(_State) ->
ok.
And after adjusting one of the control sequences in the io:format() statement in my gun client:
-module(my).
-compile(export_all).
get() ->
...
ws() ->
...
upgrade_success(ConnPid, Headers) ->
io:format("Upgraded ~w. Success!~nHeaders:~n~p~n", %% <*** CHANGED ~w to ~p
[ConnPid, Headers]).
here is the output:
~/erlang_programs/my_gun$ gmake run
gmake[1]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
GEN rebar.config
gmake[1]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
DEPEND my_gun.d
ERLC my.erl
APP my_gun
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
/Users/7stud/erlang_programs/my_gun/ebin
/Users/7stud/erlang_programs/my_gun/deps
/Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang/lib
/Users/7stud/erlang_programs/my_gun/apps
/Users/7stud/erlang_programs/my_gun/_rel
===> Resolved my_gun_release-1
===> rendering builtin_hook_status hook to "/Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/bin/hooks/builtin/status"
===> Including Erts from /Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang
===> release successfully created!
===> tarball /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/my_gun_release-1.tar.gz successfully created!
Exec: /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/erts-8.2/bin/erlexec -boot /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/my_gun_release -mode embedded -boot_var ERTS_LIB_DIR /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/lib -config /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/sys.config -args_file /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/vm.args -pa -- console
Root: /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release
/Users/7stud/erlang_programs/my_gun/_rel/my_gun_release
heart_beat_kill_pid = 34141
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
=PROGRESS REPORT==== 10-Jul-2017::16:50:53 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.353.0>},
{id,alarm_handler},
{mfargs,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::16:50:53 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.352.0>},
{id,sasl_safe_sup},
{mfargs,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe]}},
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
=PROGRESS REPORT==== 10-Jul-2017::16:50:53 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.354.0>},
{id,release_handler},
{mfargs,{release_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::16:50:53 ===
application: sasl
started_at: 'my_gun#127.0.0.1'
=PROGRESS REPORT==== 10-Jul-2017::16:50:53 ===
supervisor: {local,runtime_tools_sup}
started: [{pid,<0.360.0>},
{id,ttb_autostart},
{mfargs,{ttb_autostart,start_link,[]}},
{restart_type,temporary},
{shutdown,3000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::16:50:53 ===
application: runtime_tools
started_at: 'my_gun#127.0.0.1'
Eshell V8.2 (abort with ^G)
(my_gun#127.0.0.1)1> my:ws().
=PROGRESS REPORT==== 10-Jul-2017::16:50:57 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.367.0>},{mfa,{inet_gethost_native,init,[[]]}}]
=PROGRESS REPORT==== 10-Jul-2017::16:50:57 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.366.0>},
{id,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
Upgraded <0.365.0>. Success!
Headers:
[{<<"connection">>,<<"Upgrade">>},
{<<"date">>,<<"Mon, 10 Jul 2017 22:50:56 GMT">>},
{<<"sec-websocket-accept">>,<<"S1w7rWXToZefi/NOEcDAEDb4yEU=">>},
{<<"server">>,<<"Cowboy">>},
{<<"upgrade">>,<<"websocket">>}]
ok
(my_gun#127.0.0.1)2>
I've reached the forum's limit on text, so see my answer for how I was actually able to send and receive data using a websocket.
A summary of the issues/suggestions from the comments discussion
For running your Erlang app (in this case including gun as a dependency), you need to have the path to beam and .app files discoverable to the shell. When using erlang.mk, the easiest way to do that would be running the release using make run
On Cowboy side, to have Websocket handled correctly, you need to have a handler defined in your cowboy routes. See example in Cowboy Websocket example
See also Gun user guide for more details about handling Websocket in Gun
Success!
~/erlang_programs/my_gun/src/my.erl:
-module(my).
-compile(export_all).
get() ->
...
ws() ->
{ok, _} = application:ensure_all_started(gun),
{ok, ConnPid} = gun:open("localhost", 8080),
{ok, _Protocol} = gun:await_up(ConnPid),
gun:ws_upgrade(ConnPid, "/websocket"),
receive
{gun_ws_upgrade, ConnPid, ok, Headers} ->
upgrade_success(ConnPid, Headers);
{gun_response, ConnPid, _, _, Status, Headers} ->
exit({ws_upgrade_failed, Status, Headers});
{gun_error, _ConnPid, _StreamRef, Reason} ->
exit({ws_upgrade_failed, Reason})
%% More clauses here as needed.
after 1000 ->
exit(timeout)
end,
upgrade_success(ConnPid, Headers) ->
io:format("Upgraded ~w. Success!~nHeaders:~n~p~n",
[ConnPid, Headers]),
gun:ws_send(ConnPid, {text, "It's raining!"}),
receive
{gun_ws, ConnPid, {text, Msg} } ->
io:format("~s~n", [Msg])
end.
On the cowboy side:
-module(myws_handler).
-compile(export_all).
init(Req, State) ->
{cowboy_websocket, Req, State}. %Perform websocket setup
websocket_handle({text, Msg}, State) ->
{
reply,
{text, io_lib:format("Server received: ~s", [Msg]) },
State
};
websocket_handle(_Other, State) -> %Ignore
{ok, State}.
Here's the output:
~/erlang_programs/my_gun$ gmake run
gmake[1]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/cowlib'
gmake[2]: Entering directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
gmake[2]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/ranch'
GEN rebar.config
gmake[1]: Leaving directory '/Users/7stud/erlang_programs/my_gun/deps/gun'
DEPEND my_gun.d
ERLC my.erl
APP my_gun
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
/Users/7stud/erlang_programs/my_gun/ebin
/Users/7stud/erlang_programs/my_gun/deps
/Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang/lib
/Users/7stud/erlang_programs/my_gun/apps
/Users/7stud/erlang_programs/my_gun/_rel
===> Resolved my_gun_release-1
===> rendering builtin_hook_status hook to "/Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/bin/hooks/builtin/status"
===> Including Erts from /Users/7stud/.evm/erlang_versions/otp_src_19.2/lib/erlang
===> release successfully created!
===> tarball /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/my_gun_release-1.tar.gz successfully created!
Exec: /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/erts-8.2/bin/erlexec -boot /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/my_gun_release -mode embedded -boot_var ERTS_LIB_DIR /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/lib -config /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/sys.config -args_file /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release/releases/1/vm.args -pa -- console
Root: /Users/7stud/erlang_programs/my_gun/_rel/my_gun_release
/Users/7stud/erlang_programs/my_gun/_rel/my_gun_release
heart_beat_kill_pid = 38883
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
=PROGRESS REPORT==== 10-Jul-2017::18:04:38 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.353.0>},
{id,alarm_handler},
{mfargs,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::18:04:38 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.352.0>},
{id,sasl_safe_sup},
{mfargs,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe]}},
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
=PROGRESS REPORT==== 10-Jul-2017::18:04:38 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.354.0>},
{id,release_handler},
{mfargs,{release_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::18:04:38 ===
application: sasl
started_at: 'my_gun#127.0.0.1'
=PROGRESS REPORT==== 10-Jul-2017::18:04:38 ===
supervisor: {local,runtime_tools_sup}
started: [{pid,<0.360.0>},
{id,ttb_autostart},
{mfargs,{ttb_autostart,start_link,[]}},
{restart_type,temporary},
{shutdown,3000},
{child_type,worker}]
=PROGRESS REPORT==== 10-Jul-2017::18:04:38 ===
application: runtime_tools
started_at: 'my_gun#127.0.0.1'
Eshell V8.2 (abort with ^G)
(my_gun#127.0.0.1)1> my:ws().
=PROGRESS REPORT==== 10-Jul-2017::18:04:41 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.367.0>},{mfa,{inet_gethost_native,init,[[]]}}]
=PROGRESS REPORT==== 10-Jul-2017::18:04:41 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.366.0>},
{id,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
Upgraded <0.365.0>. Success!
Headers:
[{<<"connection">>,<<"Upgrade">>},
{<<"date">>,<<"Tue, 11 Jul 2017 00:04:40 GMT">>},
{<<"sec-websocket-accept">>,<<"pYv8QeeJfzQgaS/x8flZHyrIexk=">>},
{<<"server">>,<<"Cowboy">>},
{<<"upgrade">>,<<"websocket">>}]
Server received: It's raining!
ok
(my_gun#127.0.0.1)2>

Erlang aborts with sasl application_start_failure

I cross compiled erlang for arm system.
Later, installed Erlang into target as bellow:
./Install -sasl <ERL_ROOT>
When I check the Erlang on the target getting bellow error:
$ erl
Erlang:
=INFO REPORT==== 1-Jan-2015::19:30:44 ===
application: sasl
exited: {bad_return,
{{sasl,start,[normal,[]]},
{'EXIT',
{undef,
[{sasl,start,[normal,[]],[]},
{application_master,start_it_old,4,
[{file,"application_master.erl"},
{line,273}]}]}}}}
type: permanent
Eshell V8.0 (abort with ^G)
1> {"Kernel pid terminated",application_controller,
"{application_start_failure,sasl,{bad_return,{{sasl,start,[normal,[]]},
{'EXIT',{undef,[{sasl,start,[normal,[]],[]},
{application_master,start_it_old,4,[{file,\"application_master.erl\"},
{line,273}]}]}}}}}"}
Crash dump is being written to: erl_crash.dump...done
Kernel pid terminated (application_controller)
({application_start_failure,sasl,
{bad_return,
{{sasl,start,[normal,[]]},{'EXIT',{undef,[{sasl,start,[normal,[]],[]},{application_master,start_it_old,4,[{f
Does anyone know about this error?
Thanks in advance.
This part of the error message:
{application_master,start_it_old,4,[{file,\"application_master.erl\"},{line,273}]}
shows the place where the error is found. If you loo at the code of application_master.erl you find:
start_it_old(Tag, From, Type, ApplData) ->
{M,A} = ApplData#appl_data.mod,
----> the line where the error message is built
case catch M:start(Type, A) of
{ok, Pid} ->
link(Pid),
From ! {Tag, {ok, self()}},
loop_it(From, Pid, M, []);
{ok, Pid, AppState} ->
link(Pid),
From ! {Tag, {ok, self()}},
loop_it(From, Pid, M, AppState);
{'EXIT', normal} ->
From ! {Tag, {error, {{'EXIT',normal},{M,start,[Type,A]}}}};
{error, Reason} ->
From ! {Tag, {error, {Reason, {M,start,[Type,A]}}}};
----> the case statement which manages the result
Other ->
From ! {Tag, {error, {bad_return,{{M,start,[Type,A]},Other}}}}
end.
so you know the the result of the evaluation of M:start(Type,A) has returned {'EXIT',{undef,[{sasl,start,[normal,[]],[]}. This means that the VM is not able to locate the file sasl.beam. So either it does not exist (the erlang libraries compilation is not completed) or something is wrong in the path (the file should be at "Installation_Path/../lib/sasl-x.y/ebin/sasl.beam"

Erlang/OTP app using rebar returns crash dump when releasing

I followed the tutorial many times and, created a small app using rebar and release it successfully. But when I come to the main application (calendarApp), crash dump was returned. I made some changes but the result is the same. I would like to know what I am doing wrong, and how I can fix this.
At the end, when I do start and attach, it returns "Node is not running!".
I will show you:
sonu#sonu-Ideapad-Z570:~/calendarApp/rel$ ./calendarApp/bin/calendarApp console
Exec: /home/sonu/release/calendarApp/rel/calendarApp/erts-5.10.4/bin/erlexec -boot /home/sonu/release/calendarApp/rel/calendarApp/releases/1/calendarApp -mode embedded -config /home/sonu/release/calendarApp/rel/calendarApp/releases/1/sys.config -args_file /home/sonu/release/calendarApp/rel/calendarApp/releases/1/vm.args -- console
Root: /home/sonu/release/calendarApp/rel/calendarApp
Erlang R16B03 (erts-5.10.4) [source] [smp:4:4] [async-threads:10] [kernel-poll:false]
{global,calendarApp_sup} (<0.216.0>) starting....
{global,calendarApp_server} (<0.217.0>) starting ......
Eshell V5.10.4 (abort with ^G)
(calendarApp#127.0.0.1)1>
=INFO REPORT==== 19-Jan-2016::09:50:45 ===
application: calendarApp
exited: {{shutdown,
{failed_to_start_child,calendarApp_server,
{undef,
[{mnesia,create_schema,
[['calendarApp#127.0.0.1']],
[]},
{calendarApp_db,init,0,
[{file,"src/calendarApp_db.erl"},{line,7}]},
{calendarApp_server,init,1,
[{file,"src/calendarApp_server.erl"},{line,49}]},
{gen_server,init_it,6,
[{file,"gen_server.erl"},{line,304}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,239}]}]}}},
{calendarApp_app,start,[normal,[]]}}
type: permanent
{"Kernel pid terminated",application_controller," {application_start_failure,calendarApp,{{shutdown,{failed_to_start_child,calendarApp_server,{undef,[{mnesia,create_schema,[['calendarApp#127.0.0.1']],[]},{calendarApp_db,init,0,[{file,\"src/calendarApp_db.erl\"},{line,7}]},{calendarApp_server,init,1,[{file,\"src/calendarApp_server.erl\"},{line,49}]},{gen_server,init_it,6,[{file,\"gen_server.erl\"},{line,304}]},{proc_lib,init_p_do_apply,3,[{file,\"proc_lib.erl\"},{line,239}]}]}}},{calendarApp_app,start,[normal,[]]}}}"}
Crash dump was written to: erl_crash.dump
Kernel pid terminated (application_controller) ({application_start_failure,calendarApp,{{shutdown,{failed_to_start_child,calendarApp_server,{undef,[{mnesia,create_schema, [['calendarApp#127.0.0.1']],
sonu#sonu-Ideapad-Z570:~/calendarApp/rel$ ./calendarApp/bin/calendarApp console
If anyone knows the problem, please help me to fix this.

gen_server and the run-time errors

I have a run-time error in the init part of a gen_server.
- Init begin by process_flag(trap_exit,true)
- gen_server is part of a supervision tree
I try to print the reason in the terminate module but it seems to exit elsewhere.
- why terminate is not called ?
The application stops with shutdown as reason.
- How and where to catch the run-time error ?
The terminate callback is normally called in this situation, namely because you have trapped exits.
The only place where this is not the case is if the crash happens in the init-function. In that case, the responsibility is on the supervisor, who usually terminates itself as a result. Then this error crawls up the supervisor tree until it ends up terminating your whole application.
Usually, the supervisor will log a supervisor report with the context set to start_error. This is your hint that the part of the supervision tree has problems you should handle. You should check for this, because you may have the wrong assumption on where the error occurs.
EDITED FROM HERE
Your problem is that you don't know about SASL at all. Study it. Here is an example of how to use it.
Hoisted code from your example:
First, the bahlonga needed to tell Erlang we have a gen_server.
-module(foo).
-behaviour(gen_server).
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
We hack the #state{} record so it can be used with your code
-record(state, { name, port, socket_listen }).
Basic start_linkage...
start_link() ->
gen_server:start_link({local, foo}, ?MODULE, [], []).
Your init function, spawn problem included.
init([]) ->
Port = 3252,
Name = "foo",
Above we have hacked a bit for the sake of simplification...
process_flag(trap_exit, true),
erlang:error(blabla),
Opts = [binary, {reuseaddr, true},
{backlog,5}, {packet, 0}, {active, false}, {nodelay, true}],
case gen_tcp:listen(Port,Opts) of
{ok,Socket_Listen} ->
logger:fmsg("--> [~s,init] Socket_Listen crée = ~p",
[Name,Socket_Listen]),
{ok,handle_accept(#state{socket_listen=Socket_Listen})};
{error, Reason} ->
logger:fmsg("--> [~s,init] Erreur, Raison =~p",
[Name,Reason]), {stop, Reason}
end.
Hacks for missing functions....
handle_accept(_) ->
#state{}.
The rest is just the basics..., so I omit them.
Now for foo_sup the supervisor for foo:
-module(foo_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
-define(SERVER, ?MODULE).
Basic start link...
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
Basic ChildSpec. Get the foo child up and running...
init([]) ->
FooCh = {foo, {foo, start_link, []},
permanent, 2000, worker, [foo]},
{ok, {{one_for_all,0,1}, [FooCh]}}.
Boot Erlang with SASL enabled:
jlouis#illithid:~$ erl -boot start_sasl
Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
=PROGRESS REPORT==== 9-Dec-2010::01:01:51 ===
[..]
Eshell V5.8.3 (abort with ^G)
Let us try to spawn the supervisor...
1> foo_sup:start_link().
And we get this:
=CRASH REPORT==== 9-Dec-2010::01:05:48 ===
crasher:
initial call: foo:init/1
pid: <0.58.0>
registered_name: []
exception exit: {blabla,[{foo,init,1},
{gen_server,init_it,6},
{proc_lib,init_p_do_apply,3}]}
Above we see that we have a crash in foo:init/1 due to an exception blabla.
in function gen_server:init_it/6
ancestors: [foo_sup,<0.45.0>]
messages: []
links: [<0.57.0>]
dictionary: []
trap_exit: true
status: running
heap_size: 233
stack_size: 24
reductions: 108
neighbours:
And now the supervisor gets to report about the problem!
=SUPERVISOR REPORT==== 9-Dec-2010::01:05:48 ===
Supervisor: {local,foo_sup}
Context: start_error
The context is exactly as I said it would be...
Reason: {blabla,[{foo,init,1},
{gen_server,init_it,6},
{proc_lib,init_p_do_apply,3}]}
And with the expected reason.
Offender: [{pid,undefined},
{name,foo},
{mfargs,{foo,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]

Resources