From 6de250741eeb896311b9be440bdade4995ee4733 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 20 Aug 2021 10:04:29 +0800 Subject: [PATCH] chore(gw): add started_at/created_at field --- apps/emqx_gateway/include/emqx_gateway.hrl | 18 ++++++++---- apps/emqx_gateway/src/emqx_gateway.erl | 7 +++-- apps/emqx_gateway/src/emqx_gateway_api.erl | 2 +- .../src/emqx_gateway_insta_sup.erl | 28 +++++++++++-------- .../src/emqx_gateway_registry.erl | 4 +-- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/apps/emqx_gateway/include/emqx_gateway.hrl b/apps/emqx_gateway/include/emqx_gateway.hrl index 9099ecd4d..d959eac8b 100644 --- a/apps/emqx_gateway/include/emqx_gateway.hrl +++ b/apps/emqx_gateway/include/emqx_gateway.hrl @@ -1,5 +1,5 @@ %%-------------------------------------------------------------------- -%% Copyright (c) 2017-2021 EMQ Technologies Co., Ltd. All Rights Reserved. +%% Copyright (c) 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. @@ -17,17 +17,23 @@ -ifndef(EMQX_GATEWAY_HRL). -define(EMQX_GATEWAY_HRL, 1). --type instance_id() :: atom(). -type gateway_name() :: atom(). +%% The RawConf got from emqx:get_config/1 +-type rawconf() :: map(). + %% @doc The Gateway defination -type gateway() :: #{ name := gateway_name() , descr => binary() | undefined - %% Appears only in creating or detailed info - , rawconf => map() - %% Appears only in getting gateway status/info - , status => stopped | running + %% Appears only in getting gateway info + , status => stopped | running | unloaded + %% Timestamp in millisecond + , created_at => integer() + %% Timestamp in millisecond + , started_at => integer() + %% Appears only in getting gateway info + , rawconf => rawconf() }. -endif. diff --git a/apps/emqx_gateway/src/emqx_gateway.erl b/apps/emqx_gateway/src/emqx_gateway.erl index 01d5897ff..3462f4d11 100644 --- a/apps/emqx_gateway/src/emqx_gateway.erl +++ b/apps/emqx_gateway/src/emqx_gateway.erl @@ -41,7 +41,7 @@ registered_gateway() -> list() -> emqx_gateway_sup:list_gateway_insta(). --spec load(gateway_name(), map()) +-spec load(gateway_name(), rawconf()) -> {ok, pid()} | {error, any()}. load(Name, RawConf) -> @@ -59,8 +59,9 @@ unload(Name) -> lookup(Name) -> emqx_gateway_sup:lookup_gateway(Name). --spec update(gateway()) -> ok | {error, any()}. -update(NewGateway) -> +-spec update(gateway_name(), rawconf()) -> ok | {error, any()}. +update(Name, RawConf) -> + NewGateway = #{name => Name, rawconf => RawConf}, emqx_gateway_sup:update_gateway(NewGateway). -spec start(gateway_name()) -> ok | {error, any()}. diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index 0aca3c8f3..eea26b3a0 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -33,7 +33,7 @@ , status => <<"running">> , started_at => <<"2021-08-19T11:45:56.006373+08:00">> , max_connection => 1024000 - , current_connection => 12 + , current_connection => 1000 , listeners => [ #{name => <<"lw-udp-1">>, status => <<"activing">>}, #{name => <<"lw-udp-2">>, status => <<"inactived">>} diff --git a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl index 2edccd033..7bb3069d5 100644 --- a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl +++ b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl @@ -21,7 +21,6 @@ -include("include/emqx_gateway.hrl"). - %% APIs -export([ start_link/3 , info/1 @@ -40,11 +39,13 @@ ]). -record(state, { - gw :: gateway(), - ctx :: emqx_gateway_ctx:context(), - status :: stopped | running, + gw :: gateway(), + ctx :: emqx_gateway_ctx:context(), + status :: stopped | running, child_pids :: [pid()], - gw_state :: emqx_gateway_impl:state() | undefined + gw_state :: emqx_gateway_impl:state() | undefined, + created_at :: integer(), + started_at :: integer() | undefined }). %%-------------------------------------------------------------------- @@ -92,7 +93,8 @@ init([Gateway, Ctx0, _GwDscrptr]) -> gw = Gateway, ctx = Ctx, child_pids = [], - status = stopped + status = stopped, + created_at = erlang:system_time(millisecond) }, case cb_gateway_load(State) of {error, Reason} -> @@ -116,8 +118,12 @@ do_deinit_context(Ctx) -> cleanup_authenticators_for_gateway_insta(maps:get(auth, Ctx)), ok. -handle_call(info, _From, State = #state{gw = Gateway, status = Status}) -> - {reply, Gateway#{status => Status}, State}; +handle_call(info, _From, State = #state{gw = Gateway}) -> + GwInfo = Gateway#{status => State#state.status, + created_at => State#state.created_at, + started_at => State#state.started_at + }, + {reply, GwInfo, State}; handle_call(disable, _From, State = #state{status = Status}) -> case Status of @@ -159,7 +165,7 @@ handle_call({update, NewGateway}, _From, State = #state{ %% Running -> update handle_call({update, NewGateway}, _From, State = #state{gw = Gateway, - status = running}) -> + status = running}) -> case maps:get(name, NewGateway, undefined) == maps:get(name, Gateway, undefined) of true -> @@ -279,7 +285,8 @@ cb_gateway_load(State = #state{gw = Gateway = #{name := GwName}, {ok, State#state{ status = running, child_pids = ChildPids, - gw_state = GwState + gw_state = GwState, + started_at = erlang:system_time(millisecond) }} end catch @@ -303,7 +310,6 @@ cb_gateway_update(NewGateway, %% XXX: Hot-upgrade ??? ChildPids = start_child_process(ChildPidOrSpecs), {ok, State#state{ - status = running, child_pids = ChildPids, gw_state = NGwState }} diff --git a/apps/emqx_gateway/src/emqx_gateway_registry.erl b/apps/emqx_gateway/src/emqx_gateway_registry.erl index b311073a9..cfa6d424a 100644 --- a/apps/emqx_gateway/src/emqx_gateway_registry.erl +++ b/apps/emqx_gateway/src/emqx_gateway_registry.erl @@ -19,10 +19,9 @@ -include("include/emqx_gateway.hrl"). - -behavior(gen_server). -%% APIs for Impl. +%% APIs -export([ reg/2 , unreg/1 , list/0 @@ -100,7 +99,6 @@ call(Req) -> %%-------------------------------------------------------------------- init([]) -> - %% TODO: Metrics ??? process_flag(trap_exit, true), {ok, #state{reged = #{}}}.