简体   繁体   中英

Starting a dependency based on a configuration argument in erlang

Some of my application dependencies are used only if a given parameter is set. I need to know what is the best approach for starting those dependencies. I'm using Erlang R14B04 and I cannot use a different version.

I have two alternatives. The first one:

%% file myapp.erl
start() ->
    dep1:start(),
    dep2:start(),
    application:start(myapp),
    case application:get_env(myapp, use_app3) ->
        true ->
            dep3:start()
        _ ->
            ok
    end.

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

The second alernative:

%% file myapp.erl
start() ->
    dep1:start(),
    dep2:start(),
    application:start(myapp).

start(Type, StartArgs) ->
    case application:get_env(myapp, use_app3) ->
        true ->
            dep3:start()
        _ ->
            ok
    end.
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

Which one is the best approach to solve the problem?

I would say the first is a better choice. start/0 seems to have the concern of starting relevant applications, while start/2 starts the current application's supervision tree.

These are two separate concerns, so putting the conditional logic in start/2 seems like it would dirty up your code a bit.

I notice that in the first example, you're starting the third app after your own app. In the second example, you're starting the third app before your own app. Which does your application need to happen first?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM