chore(gw): fix gatway enable/1 not working

This commit is contained in:
JianBo He 2021-09-16 17:17:20 +08:00
parent d8176f4378
commit a9e32ac106
3 changed files with 56 additions and 55 deletions

View File

@ -100,11 +100,17 @@ init([Gateway, Ctx, _GwDscrptr]) ->
status = stopped, status = stopped,
created_at = erlang:system_time(millisecond) created_at = erlang:system_time(millisecond)
}, },
case cb_gateway_load(State) of case maps:get(enable, Config, true) of
{error, Reason} -> false ->
{stop, {load_gateway_failure, Reason}}; ?LOG(info, "Skipp to start ~s gateway due to disabled", [GwName]),
{ok, NState} -> {ok, State};
{ok, NState} true ->
case cb_gateway_load(State) of
{error, Reason} ->
{stop, {load_gateway_failure, Reason}};
{ok, NState} ->
{ok, NState}
end
end. end.
handle_call(info, _From, State) -> handle_call(info, _From, State) ->
@ -235,10 +241,12 @@ do_init_authn([], Names) ->
Names; Names;
do_init_authn([{_ChainName, _AuthConf = #{enable := false}}|More], Names) -> do_init_authn([{_ChainName, _AuthConf = #{enable := false}}|More], Names) ->
do_init_authn(More, Names); do_init_authn(More, Names);
do_init_authn([{ChainName, AuthConf}|More], Names) -> do_init_authn([{ChainName, AuthConf}|More], Names) when is_map(AuthConf) ->
_ = application:ensure_all_started(emqx_authn), _ = application:ensure_all_started(emqx_authn),
do_create_authn_chain(ChainName, AuthConf), do_create_authn_chain(ChainName, AuthConf),
do_init_authn(More, [ChainName|Names]). do_init_authn(More, [ChainName|Names]);
do_init_authn([_BadConf|More], Names) ->
do_init_authn(More, Names).
authns(GwName, Config) -> authns(GwName, Config) ->
Listeners = maps:to_list(maps:get(listeners, Config, #{})), Listeners = maps:to_list(maps:get(listeners, Config, #{})),
@ -358,39 +366,33 @@ cb_gateway_load(State = #state{name = GwName,
ctx = Ctx}) -> ctx = Ctx}) ->
Gateway = detailed_gateway_info(State), Gateway = detailed_gateway_info(State),
try
case maps:get(enable, Config, true) of AuthnNames = init_authn(GwName, Config),
false -> NCtx = Ctx#{auth => AuthnNames},
?LOG(info, "Skipp to start ~s gateway due to disabled", [GwName]); #{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName),
true -> case CbMod:on_gateway_load(Gateway, NCtx) of
try {error, Reason} ->
AuthnNames = init_authn(GwName, Config), do_deinit_authn(AuthnNames),
NCtx = Ctx#{auth => AuthnNames}, throw({callback_return_error, Reason});
#{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName), {ok, ChildPidOrSpecs, GwState} ->
case CbMod:on_gateway_load(Gateway, NCtx) of ChildPids = start_child_process(ChildPidOrSpecs),
{error, Reason} -> {ok, State#state{
do_deinit_authn(AuthnNames), ctx = NCtx,
throw({callback_return_error, Reason}); authns = AuthnNames,
{ok, ChildPidOrSpecs, GwState} -> status = running,
ChildPids = start_child_process(ChildPidOrSpecs), child_pids = ChildPids,
{ok, State#state{ gw_state = GwState,
ctx = NCtx, stopped_at = undefined,
authns = AuthnNames, started_at = erlang:system_time(millisecond)
status = running, }}
child_pids = ChildPids, end
gw_state = GwState, catch
stopped_at = undefined, Class : Reason1 : Stk ->
started_at = erlang:system_time(millisecond) ?LOG(error, "Failed to load ~s gateway (~0p, ~0p) "
}} "crashed: {~p, ~p}, stacktrace: ~0p",
end [GwName, Gateway, Ctx,
catch Class, Reason1, Stk]),
Class : Reason1 : Stk -> {error, {Class, Reason1, Stk}}
?LOG(error, "Failed to load ~s gateway (~0p, ~0p) "
"crashed: {~p, ~p}, stacktrace: ~0p",
[GwName, Gateway, Ctx,
Class, Reason1, Stk]),
{error, {Class, Reason1, Stk}}
end
end. end.
cb_gateway_update(Config, cb_gateway_update(Config,

View File

@ -25,20 +25,19 @@
-define(CONF_DEFAULT, <<" -define(CONF_DEFAULT, <<"
gateway.coap { gateway.coap {
idle_timeout = 30s idle_timeout = 30s
enable_stats = false enable_stats = false
mountpoint = \"\" mountpoint = \"\"
notify_type = qos notify_type = qos
connection_required = true connection_required = true
subscribe_qos = qos1 subscribe_qos = qos1
publish_qos = qos1 publish_qos = qos1
authentication = undefined authentication = undefined
listeners.udp.default {
listeners.udp.default { bind = 5683
bind = 5683 }
} }
} ">>).
">>).
-define(HOST, "127.0.0.1"). -define(HOST, "127.0.0.1").
-define(PORT, 5683). -define(PORT, 5683).

View File

@ -33,7 +33,7 @@ gateway.lwm2m {
xml_dir = \"../../lib/emqx_gateway/src/lwm2m/lwm2m_xml\" xml_dir = \"../../lib/emqx_gateway/src/lwm2m/lwm2m_xml\"
lifetime_min = 1s lifetime_min = 1s
lifetime_max = 86400s lifetime_max = 86400s
qmode_time_windonw = 22 qmode_time_window = 22
auto_observe = false auto_observe = false
mountpoint = \"lwm2m/%u\" mountpoint = \"lwm2m/%u\"
update_msg_publish_condition = contains_object_list update_msg_publish_condition = contains_object_list