简体   繁体   English

基于erlang中的配置参数启动依赖项

[英]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. 我正在使用Erlang R14B04,不能使用其他版本。

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. start / 0似乎与启动相关应用程序有关,而start / 2启动当前应用程序的监视树。

These are two separate concerns, so putting the conditional logic in start/2 seems like it would dirty up your code a bit. 这是两个独立的问题,因此将条件逻辑放在start / 2中似乎会使您的代码变脏。

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? 您的应用程序首先需要发生什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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