3
votes

I'm using Erlang/OTP 20.0 and rebar3. When I start a new release that use cowboy 2.0.0, the release fail to start.

Here are the steps I do to build the project. What's wrong?

  1. create the release project

    $ rebar3 new release cowboy2
    
  2. add cowboy package

    {deps, [{cowboy, {git, "https://github.com/ninenines/cowboy.git", {tag, "2.0.0"}}}]}.
    
  3. add a basic dispatcher

    start(_StartType, _StartArgs) ->
        Dispatch = cowboy_router:compile([
                {'_', [
                    {"/", toppage_handler, []}
                ]}
            ]),
        {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{
            env => #{dispatch => Dispatch}
        }),
        cowboy2_sup:start_link().
    
  4. add handler. for the purpose of this example I use (https://raw.githubusercontent.com/ninenines/cowboy/master/examples/hello_world/src/toppage_handler.erl)

  5. compile and release

    $ rebar3 compile && rebar3 release
    
  6. run the application

    $ ./_build/default/rel/cowboy2/bin/cowboy2-0.1.0 console
    

The output

$ ./_build/default/rel/cowboy2/bin/cowboy2-0.1.0 console
Exec: /home/deimos/.asdf/installs/erlang/20.0/lib/erlang/erts-9.0/bin/erlexec 
-boot /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2/releases/0.1.0/cowboy2
-mode embedded -boot_var ERTS_LIB_DIR /home/deimos/.asdf/installs/erlang/20.0/lib/erlang/lib 
-config /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2/releases/0.1.0/sys.config 
-args_file /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2/releases/0.1.0/vm.args 
-pa -- console

Root: /home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2
/home/deimos/Dev/personal/cowboy2/_build/default/rel/cowboy2
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:30] [hipe] [kernel-poll:true]


=INFO REPORT==== 11-Oct-2017::21:00:52 ===
    application: cowboy2
    exited: {bad_return,
                {{cowboy2_app,start,[normal,[]]},
                 {'EXIT',
                     {undef,
                         [{cowboy_router,compile,
                              [[{'_',[{"/",toppage_handler,[]}]}]],
                              []},
                          {cowboy2_app,start,2,
                                  [{file,
                                   "/home/deimos/Dev/personal/cowboy2/_build/default/lib/cowboy2/src/cowboy2_app.erl"},
                                   {line,18}]},
                          {application_master,start_it_old,4,
                              [{file,"application_master.erl"},
                               {line,273}]}]}}}}
    type: permanent
    (...)
    Kernel pid terminated (application_controller)
     ({application_start_failure,cowboy2,{bad_return,{{cowboy2_app,start,[normal,[]]},{'EXIT',{undef,[{cowboy_router,compile,[[{'_',[{"/",toppage_handler,[]}]

Crash dump is being written to: erl_crash.dump...done
1
I am not entireley sure how things are with cowboy 2, but in cowboy 1, you had to manually start the cowboy-application inside your main process in order to use cowboy-routines, router, dispatcher and so on. I made a minimal cowboy-example a while ago to remind me of things like this. This was basically not documented. Take a look at github.com/maze-le/minimal-cowboy/blob/master/src/mincb.erl.Mathias Vonende
That was the cause of the error. I didn't put the cowboy application inside cowboy2.app.src.Yunier Rojas

1 Answers

3
votes

As @maze-le points out, the problem was that the cowboy application was not started. The solution is to add cowboy to the file apps/cowboy2/src/cowboy2.app.src also generated by the command $rebar3 new release cowboy2. I add the file for completeness.

apps/cowboy2/src/cowboy2.app.src

{application, cowboy2,
 [{description, "An OTP application"},
  {vsn, "0.1.0"},
  {registered, []},
  {mod, { cowboy2_app, []}},
  {applications,
   [kernel,
    stdlib,
    cowboy
   ]},
  {env,[]},
  {modules, []},

  {maintainers, []},
  {licenses, ["Apache 2.0"]},
  {links, []}
 ]}.