chore(gw): add started_at/created_at field

This commit is contained in:
JianBo He 2021-08-20 10:04:29 +08:00 committed by turtleDeng
parent 75dc4ea9a2
commit 6de250741e
5 changed files with 35 additions and 24 deletions

View File

@ -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"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -17,17 +17,23 @@
-ifndef(EMQX_GATEWAY_HRL). -ifndef(EMQX_GATEWAY_HRL).
-define(EMQX_GATEWAY_HRL, 1). -define(EMQX_GATEWAY_HRL, 1).
-type instance_id() :: atom().
-type gateway_name() :: atom(). -type gateway_name() :: atom().
%% The RawConf got from emqx:get_config/1
-type rawconf() :: map().
%% @doc The Gateway defination %% @doc The Gateway defination
-type gateway() :: -type gateway() ::
#{ name := gateway_name() #{ name := gateway_name()
, descr => binary() | undefined , descr => binary() | undefined
%% Appears only in creating or detailed info %% Appears only in getting gateway info
, rawconf => map() , status => stopped | running | unloaded
%% Appears only in getting gateway status/info %% Timestamp in millisecond
, status => stopped | running , created_at => integer()
%% Timestamp in millisecond
, started_at => integer()
%% Appears only in getting gateway info
, rawconf => rawconf()
}. }.
-endif. -endif.

View File

@ -41,7 +41,7 @@ registered_gateway() ->
list() -> list() ->
emqx_gateway_sup:list_gateway_insta(). emqx_gateway_sup:list_gateway_insta().
-spec load(gateway_name(), map()) -spec load(gateway_name(), rawconf())
-> {ok, pid()} -> {ok, pid()}
| {error, any()}. | {error, any()}.
load(Name, RawConf) -> load(Name, RawConf) ->
@ -59,8 +59,9 @@ unload(Name) ->
lookup(Name) -> lookup(Name) ->
emqx_gateway_sup:lookup_gateway(Name). emqx_gateway_sup:lookup_gateway(Name).
-spec update(gateway()) -> ok | {error, any()}. -spec update(gateway_name(), rawconf()) -> ok | {error, any()}.
update(NewGateway) -> update(Name, RawConf) ->
NewGateway = #{name => Name, rawconf => RawConf},
emqx_gateway_sup:update_gateway(NewGateway). emqx_gateway_sup:update_gateway(NewGateway).
-spec start(gateway_name()) -> ok | {error, any()}. -spec start(gateway_name()) -> ok | {error, any()}.

View File

@ -33,7 +33,7 @@
, status => <<"running">> , status => <<"running">>
, started_at => <<"2021-08-19T11:45:56.006373+08:00">> , started_at => <<"2021-08-19T11:45:56.006373+08:00">>
, max_connection => 1024000 , max_connection => 1024000
, current_connection => 12 , current_connection => 1000
, listeners => [ , listeners => [
#{name => <<"lw-udp-1">>, status => <<"activing">>}, #{name => <<"lw-udp-1">>, status => <<"activing">>},
#{name => <<"lw-udp-2">>, status => <<"inactived">>} #{name => <<"lw-udp-2">>, status => <<"inactived">>}

View File

@ -21,7 +21,6 @@
-include("include/emqx_gateway.hrl"). -include("include/emqx_gateway.hrl").
%% APIs %% APIs
-export([ start_link/3 -export([ start_link/3
, info/1 , info/1
@ -44,7 +43,9 @@
ctx :: emqx_gateway_ctx:context(), ctx :: emqx_gateway_ctx:context(),
status :: stopped | running, status :: stopped | running,
child_pids :: [pid()], 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, gw = Gateway,
ctx = Ctx, ctx = Ctx,
child_pids = [], child_pids = [],
status = stopped status = stopped,
created_at = erlang:system_time(millisecond)
}, },
case cb_gateway_load(State) of case cb_gateway_load(State) of
{error, Reason} -> {error, Reason} ->
@ -116,8 +118,12 @@ do_deinit_context(Ctx) ->
cleanup_authenticators_for_gateway_insta(maps:get(auth, Ctx)), cleanup_authenticators_for_gateway_insta(maps:get(auth, Ctx)),
ok. ok.
handle_call(info, _From, State = #state{gw = Gateway, status = Status}) -> handle_call(info, _From, State = #state{gw = Gateway}) ->
{reply, Gateway#{status => Status}, State}; 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}) -> handle_call(disable, _From, State = #state{status = Status}) ->
case Status of case Status of
@ -279,7 +285,8 @@ cb_gateway_load(State = #state{gw = Gateway = #{name := GwName},
{ok, State#state{ {ok, State#state{
status = running, status = running,
child_pids = ChildPids, child_pids = ChildPids,
gw_state = GwState gw_state = GwState,
started_at = erlang:system_time(millisecond)
}} }}
end end
catch catch
@ -303,7 +310,6 @@ cb_gateway_update(NewGateway,
%% XXX: Hot-upgrade ??? %% XXX: Hot-upgrade ???
ChildPids = start_child_process(ChildPidOrSpecs), ChildPids = start_child_process(ChildPidOrSpecs),
{ok, State#state{ {ok, State#state{
status = running,
child_pids = ChildPids, child_pids = ChildPids,
gw_state = NGwState gw_state = NGwState
}} }}

View File

@ -19,10 +19,9 @@
-include("include/emqx_gateway.hrl"). -include("include/emqx_gateway.hrl").
-behavior(gen_server). -behavior(gen_server).
%% APIs for Impl. %% APIs
-export([ reg/2 -export([ reg/2
, unreg/1 , unreg/1
, list/0 , list/0
@ -100,7 +99,6 @@ call(Req) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([]) -> init([]) ->
%% TODO: Metrics ???
process_flag(trap_exit, true), process_flag(trap_exit, true),
{ok, #state{reged = #{}}}. {ok, #state{reged = #{}}}.