Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang, how to load applications with their dependencies

I have some apps in my cluster, I need to start some of them sometimes on different hosts.

The story is that the Erlang cluster is already running, so even though I have my .app resource file per application stating which applications should be started before mine, this only works to create a startup script, not to start an app in a already running node.

At the moment I have a custom routine that uses application:get_key(Application,applications) to extract the dependencies and start them separately before starting the given application.

I was wondering if there isn't a better way of doing this.

like image 569
Arkaitz Jimenez Avatar asked May 08 '12 16:05

Arkaitz Jimenez


2 Answers

Since Erlang R16B02, there is also application:ensure_all_started.

like image 63
Roger Lipscombe Avatar answered Oct 26 '22 18:10

Roger Lipscombe


Frankly, the standard tools for doing this in Erlang are unnecessarily annoying right now. I tend to put the following boiler-plate in my application callback module:

-module(myapp_app).
-export([start/0]).

start() -> a_start(myapp, permanent).

a_start(App, Type) ->
    start_ok(App, Type, application:start(App, Type)).

start_ok(_App, _Type, ok) -> ok;
start_ok(_App, _Type, {error, {already_started, _App}}) -> ok;
start_ok(App, Type, {error, {not_started, Dep}}) ->
    ok = a_start(Dep, Type),
    a_start(App, Type);
start_ok(App, _Type, {error, Reason}) ->
    erlang:error({app_start_failed, App, Reason}).

You can then add -s myapp_app to your erlang command line and this will start the app and all its dependencies recursively. Why this function isn't in the application module I don't know :)


There is a working example of this custom erlang app startup code in my Erlang Factory 2012 SFBay example app.

like image 37
archaelus Avatar answered Oct 26 '22 17:10

archaelus