Question

This is the most basic application I can come up with and I cannot understand why the start/2 of the application module function won't log a message. Here is what I have done:

1) app config file (test_app.app):

{application,test_app,
             [{description,"Test App"},
              {vsn,0.9},
              {applications,[kernel,stdlib]},
              {modules,[test_app,log_utils]},
              {registered,[test_app]}]}.

2) app module (test_app.erl):

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

start(_Type, _Args) ->
    log_utils:info("here at APP START 1"),
    master_sup:start_link({10,20,30}). 

stop(_State) ->
    ok.

test() ->
    log_utils:info("here at APP START 2"),
    master_sup:start_link({10,20,30}).

I then compile and test as such:

1> application:start(test_app).
ok
2> test_app:test().
==INFO REPORT==== 27-Oct-2013::19:53:29 ===
"here at APP START 2"

What I expect is that the application:start(test_app) would execute the start/2 function and log the message similarly as the test/0 function.

In truth I had a more complex example where I start a supervisor, but similarly, the APIs I created in the app module cause an error indicating that the start_link did not work. If I call a test function that starts the supervisor, then it works.

Was it helpful?

Solution

You need to an extra option to the .app file which gives the application callback module and start args. There is no implicit callback module name and if none is given then no processes will be started. The option is {mod,{CallBackMod,StartArgs}} so the whole .app file will become:

{application,test_app,
 [{description,"Test App"},
  {vsn,0.9},
  {applications,[kernel,stdlib]},
  {modules,[test_app,log_utils]},
  {registered,[test_app]},
  {mod,{test_app,[]}}]}.

The 2nd element, test_app in your case, is the name of the application and not the callback module; they don't have to be the same. If a callback is given then Mod:start/2 will be called whe the application is started and Mod:stop/1 when the application has been stopped.

Note that an application doesn't have to run any processes when it is started, for example the stdlib application doesn't.

You will find a better description here in Applications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top