From a9e32ac10615ab805680543ebbd0414f40abb029 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Thu, 16 Sep 2021 17:17:20 +0800 Subject: [PATCH] chore(gw): fix gatway enable/1 not working --- .../src/emqx_gateway_insta_sup.erl | 82 ++++++++++--------- .../emqx_gateway/test/emqx_coap_api_SUITE.erl | 27 +++--- .../test/emqx_lwm2m_api_SUITE.erl | 2 +- 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl index 238bcaa1f..0863b67d5 100644 --- a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl +++ b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl @@ -100,11 +100,17 @@ init([Gateway, Ctx, _GwDscrptr]) -> status = stopped, created_at = erlang:system_time(millisecond) }, - case cb_gateway_load(State) of - {error, Reason} -> - {stop, {load_gateway_failure, Reason}}; - {ok, NState} -> - {ok, NState} + case maps:get(enable, Config, true) of + false -> + ?LOG(info, "Skipp to start ~s gateway due to disabled", [GwName]), + {ok, State}; + true -> + case cb_gateway_load(State) of + {error, Reason} -> + {stop, {load_gateway_failure, Reason}}; + {ok, NState} -> + {ok, NState} + end end. handle_call(info, _From, State) -> @@ -235,10 +241,12 @@ do_init_authn([], Names) -> Names; do_init_authn([{_ChainName, _AuthConf = #{enable := false}}|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), 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) -> Listeners = maps:to_list(maps:get(listeners, Config, #{})), @@ -358,39 +366,33 @@ cb_gateway_load(State = #state{name = GwName, ctx = Ctx}) -> Gateway = detailed_gateway_info(State), - - case maps:get(enable, Config, true) of - false -> - ?LOG(info, "Skipp to start ~s gateway due to disabled", [GwName]); - true -> - try - AuthnNames = init_authn(GwName, Config), - NCtx = Ctx#{auth => AuthnNames}, - #{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName), - case CbMod:on_gateway_load(Gateway, NCtx) of - {error, Reason} -> - do_deinit_authn(AuthnNames), - throw({callback_return_error, Reason}); - {ok, ChildPidOrSpecs, GwState} -> - ChildPids = start_child_process(ChildPidOrSpecs), - {ok, State#state{ - ctx = NCtx, - authns = AuthnNames, - status = running, - child_pids = ChildPids, - gw_state = GwState, - stopped_at = undefined, - started_at = erlang:system_time(millisecond) - }} - end - catch - 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 + try + AuthnNames = init_authn(GwName, Config), + NCtx = Ctx#{auth => AuthnNames}, + #{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName), + case CbMod:on_gateway_load(Gateway, NCtx) of + {error, Reason} -> + do_deinit_authn(AuthnNames), + throw({callback_return_error, Reason}); + {ok, ChildPidOrSpecs, GwState} -> + ChildPids = start_child_process(ChildPidOrSpecs), + {ok, State#state{ + ctx = NCtx, + authns = AuthnNames, + status = running, + child_pids = ChildPids, + gw_state = GwState, + stopped_at = undefined, + started_at = erlang:system_time(millisecond) + }} + end + catch + 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. cb_gateway_update(Config, diff --git a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl index 83521f5cd..846cfc88f 100644 --- a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl @@ -25,20 +25,19 @@ -define(CONF_DEFAULT, <<" gateway.coap { - idle_timeout = 30s - enable_stats = false - mountpoint = \"\" - notify_type = qos - connection_required = true - subscribe_qos = qos1 - publish_qos = qos1 - authentication = undefined - - listeners.udp.default { - bind = 5683 - } - } - ">>). + idle_timeout = 30s + enable_stats = false + mountpoint = \"\" + notify_type = qos + connection_required = true + subscribe_qos = qos1 + publish_qos = qos1 + authentication = undefined + listeners.udp.default { + bind = 5683 + } +} +">>). -define(HOST, "127.0.0.1"). -define(PORT, 5683). diff --git a/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl b/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl index 081f11005..a875aceb6 100644 --- a/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl @@ -33,7 +33,7 @@ gateway.lwm2m { xml_dir = \"../../lib/emqx_gateway/src/lwm2m/lwm2m_xml\" lifetime_min = 1s lifetime_max = 86400s - qmode_time_windonw = 22 + qmode_time_window = 22 auto_observe = false mountpoint = \"lwm2m/%u\" update_msg_publish_condition = contains_object_list