forked from dgiot/dgiot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emqx_app.erl
134 lines (111 loc) · 3.76 KB
/
emqx_app.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
%%--------------------------------------------------------------------
%% Copyright (c) 2017-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_app).
-behaviour(application).
-export([ start/2
, stop/1
, get_description/0
, get_release/0
]).
-define(APP, emqx).
-include("emqx_release.hrl").
%%--------------------------------------------------------------------
%% Application callbacks
%%--------------------------------------------------------------------
start(_Type, _Args) ->
set_backtrace_depth(),
print_otp_version_warning(),
print_banner(),
%% Load application first for ekka_mnesia scanner
_ = load_ce_modules(),
ekka:start(),
{ok, Sup} = emqx_sup:start_link(),
ok = start_autocluster(),
ok = emqx_plugins:init(),
_ = emqx_plugins:load(),
_ = start_ce_modules(),
emqx_boot:is_enabled(listeners) andalso (ok = emqx_listeners:start()),
register(emqx, self()),
ok = emqx_alarm_handler:load(),
print_vsn(),
{ok, Sup}.
-spec(stop(State :: term()) -> term()).
stop(_State) ->
ok = emqx_alarm_handler:unload(),
emqx_boot:is_enabled(listeners)
andalso emqx_listeners:stop().
set_backtrace_depth() ->
Depth = application:get_env(?APP, backtrace_depth, 16),
_ = erlang:system_flag(backtrace_depth, Depth),
ok.
-ifndef(EMQX_ENTERPRISE).
load_ce_modules() ->
application:load(emqx_modules).
start_ce_modules() ->
application:ensure_all_started(emqx_modules).
-else.
load_ce_modules() ->
ok.
start_ce_modules() ->
ok.
-endif.
%%--------------------------------------------------------------------
%% Print Banner
%%--------------------------------------------------------------------
-if(?OTP_RELEASE> 22).
print_otp_version_warning() -> ok.
-else.
print_otp_version_warning() ->
io:format("WARNING: Running on Erlang/OTP version ~p. Recommended: 23~n",
[?OTP_RELEASE]).
-endif. % OTP_RELEASE
-ifndef(TEST).
print_banner() ->
io:format("Starting ~s on node ~s~n", [?APP, node()]).
print_vsn() ->
io:format("~s ~s is running now!~n", [get_description(), get_release()]).
-else. % TEST
print_vsn() ->
ok.
print_banner() ->
ok.
-endif. % TEST
get_description() ->
{ok, Descr0} = application:get_key(?APP, description),
case os:getenv("EMQX_DESCRIPTION") of
false -> Descr0;
"" -> Descr0;
Str -> string:strip(Str, both, $\n)
end.
get_release() ->
case lists:keyfind(emqx_vsn, 1, ?MODULE:module_info(compile)) of
false -> %% For TEST build or depedency build.
release_in_macro();
{_, Vsn} -> %% For emqx release build
VsnStr = release_in_macro(),
1 = string:str(Vsn, VsnStr), %% assert
Vsn
end.
release_in_macro() ->
element(2, ?EMQX_RELEASE).
%%--------------------------------------------------------------------
%% Autocluster
%%--------------------------------------------------------------------
start_autocluster() ->
ekka:callback(prepare, fun emqx:shutdown/1),
ekka:callback(reboot, fun emqx:reboot/0),
_ = ekka:autocluster(?APP), %% returns 'ok' or a pid or 'any()' as in spec
ok.