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");
%% 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.

View File

@ -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()}.

View File

@ -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">>}

View File

@ -21,7 +21,6 @@
-include("include/emqx_gateway.hrl").
%% APIs
-export([ start_link/3
, info/1
@ -44,7 +43,9 @@
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
@ -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
}}

View File

@ -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 = #{}}}.