fix
This commit is contained in:
parent
1bdf105c3a
commit
2e5ddbe087
@ -265,7 +265,7 @@ json_state_running(Json) when is_binary(Json) ->
|
|||||||
-spec display_options(Options :: map()) -> ok.
|
-spec display_options(Options :: map()) -> ok.
|
||||||
display_options(Options) when is_map(Options) ->
|
display_options(Options) when is_map(Options) ->
|
||||||
logger:debug("deploy options: ~p", [iolist_to_binary(json:encode(Options))]),
|
logger:debug("deploy options: ~p", [iolist_to_binary(json:encode(Options))]),
|
||||||
lists:foreach(fun({K, V}) -> logger:debug("~p => ~p", [K, V]) end, maps:to_list(Options)),
|
%lists:foreach(fun({K, V}) -> logger:debug("~p => ~p", [K, V]) end, maps:to_list(Options)),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-spec build_stop_container_url(ContainerName :: binary(), TimeoutSeconds :: non_neg_integer()) -> string().
|
-spec build_stop_container_url(ContainerName :: binary(), TimeoutSeconds :: non_neg_integer()) -> string().
|
||||||
|
|||||||
@ -57,15 +57,6 @@ init([]) ->
|
|||||||
% modules => ['docker_events']
|
% modules => ['docker_events']
|
||||||
%},
|
%},
|
||||||
|
|
||||||
#{
|
|
||||||
id => cache_model,
|
|
||||||
start => {cache_model, start_link, []},
|
|
||||||
restart => permanent,
|
|
||||||
shutdown => 5000,
|
|
||||||
type => worker,
|
|
||||||
modules => ['cache_model']
|
|
||||||
},
|
|
||||||
|
|
||||||
#{
|
#{
|
||||||
id => service_model,
|
id => service_model,
|
||||||
start => {service_model, start_link, []},
|
start => {service_model, start_link, []},
|
||||||
|
|||||||
@ -1,144 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author anlicheng
|
|
||||||
%%% @copyright (C) 2025, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 13. 8月 2025 16:05
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(cache_model).
|
|
||||||
-author("anlicheng").
|
|
||||||
|
|
||||||
-behaviour(gen_server).
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([start_link/0]).
|
|
||||||
-export([insert/1, fetch_next/0, delete/1, get_all_cache/0]).
|
|
||||||
|
|
||||||
%% gen_server callbacks
|
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
|
||||||
|
|
||||||
-define(SERVER, ?MODULE).
|
|
||||||
-define(TAB, cache).
|
|
||||||
|
|
||||||
-record(state, {
|
|
||||||
|
|
||||||
}).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% API
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
-spec insert(Data :: binary()) -> ok | {error, Reason :: any()}.
|
|
||||||
insert(Data) when is_binary(Data) ->
|
|
||||||
gen_server:call(?SERVER, {insert, {generate_id(), Data}}).
|
|
||||||
|
|
||||||
-spec fetch_next() -> error | {ok, {integer(), binary()}}.
|
|
||||||
fetch_next() ->
|
|
||||||
gen_server:call(?SERVER, fetch_next).
|
|
||||||
|
|
||||||
-spec delete(integer()) -> ok.
|
|
||||||
delete(Id) when is_integer(Id) ->
|
|
||||||
gen_server:call(?SERVER, {delete, Id}).
|
|
||||||
|
|
||||||
-spec get_all_cache() -> [binary()].
|
|
||||||
get_all_cache() ->
|
|
||||||
gen_server:call(?SERVER, get_all_cache).
|
|
||||||
|
|
||||||
%% @doc Spawns the server and registers the local name (unique)
|
|
||||||
-spec(start_link() ->
|
|
||||||
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
|
||||||
start_link() ->
|
|
||||||
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% gen_server callbacks
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Initializes the server
|
|
||||||
-spec(init(Args :: term()) ->
|
|
||||||
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term()} | ignore).
|
|
||||||
init([]) ->
|
|
||||||
{ok, DetsDir} = application:get_env(efka, dets_dir),
|
|
||||||
File = DetsDir ++ "cache.dets",
|
|
||||||
{ok, ?TAB} = dets:open_file(?TAB, [{file, File}, {type, bag}, {keypos, 1}]),
|
|
||||||
{ok, #state{}}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Handling call messages
|
|
||||||
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
|
||||||
State :: #state{}) ->
|
|
||||||
{reply, Reply :: term(), NewState :: #state{}} |
|
|
||||||
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_call({insert, Cache}, _From, State = #state{}) ->
|
|
||||||
ok = dets:insert(?TAB, Cache),
|
|
||||||
{reply, ok, State};
|
|
||||||
handle_call(fetch_next, _From, State = #state{}) ->
|
|
||||||
case dets:first(?TAB) of
|
|
||||||
'$end_of_table' ->
|
|
||||||
{reply, error, State};
|
|
||||||
Key ->
|
|
||||||
[Entry] = dets:lookup(?TAB, Key),
|
|
||||||
{reply, {ok, Entry}, State}
|
|
||||||
end;
|
|
||||||
handle_call({delete, Id}, _From, State = #state{}) ->
|
|
||||||
ok = dets:delete(?TAB, Id),
|
|
||||||
{reply, ok, State};
|
|
||||||
|
|
||||||
handle_call(get_all_cache, _From, State = #state{}) ->
|
|
||||||
Items = dets:foldl(fun(Record, Acc) -> [Record|Acc] end, [], ?TAB),
|
|
||||||
{reply, {ok, lists:reverse(Items)}, State};
|
|
||||||
|
|
||||||
handle_call(_Request, _From, State = #state{}) ->
|
|
||||||
{reply, ok, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Handling cast messages
|
|
||||||
-spec(handle_cast(Request :: term(), State :: #state{}) ->
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_cast(_Request, State = #state{}) ->
|
|
||||||
{noreply, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Handling all non call/cast messages
|
|
||||||
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_info(_Info, State = #state{}) ->
|
|
||||||
{noreply, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc This function is called by a gen_server when it is about to
|
|
||||||
%% terminate. It should be the opposite of Module:init/1 and do any
|
|
||||||
%% necessary cleaning up. When it returns, the gen_server terminates
|
|
||||||
%% with Reason. The return value is ignored.
|
|
||||||
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
|
||||||
State :: #state{}) -> term()).
|
|
||||||
terminate(_Reason, _State = #state{}) ->
|
|
||||||
dets:close(?TAB),
|
|
||||||
ok.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Convert process state when code is changed
|
|
||||||
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
|
||||||
Extra :: term()) ->
|
|
||||||
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
|
||||||
code_change(_OldVsn, State = #state{}, _Extra) ->
|
|
||||||
{ok, State}.
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% Internal functions
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
-spec generate_id() -> integer().
|
|
||||||
generate_id() ->
|
|
||||||
os:system_time(microsecond).
|
|
||||||
@ -23,6 +23,7 @@
|
|||||||
-export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
-export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
||||||
|
|
||||||
-define(SERVER, ?MODULE).
|
-define(SERVER, ?MODULE).
|
||||||
|
-define(CACHE_TAB, cache).
|
||||||
|
|
||||||
%% 标记当前agent的状态,只有在 activated 状态下才可以正常的发送数据
|
%% 标记当前agent的状态,只有在 activated 状态下才可以正常的发送数据
|
||||||
-define(STATE_DISCONNECTED, disconnected).
|
-define(STATE_DISCONNECTED, disconnected).
|
||||||
@ -74,8 +75,13 @@ start_link() ->
|
|||||||
|
|
||||||
-spec init(list()) -> {ok, atom(), #state{}}.
|
-spec init(list()) -> {ok, atom(), #state{}}.
|
||||||
init([]) ->
|
init([]) ->
|
||||||
erlang:start_timer(0, self(), create_transport),
|
case open_cache_table() of
|
||||||
{ok, ?STATE_DISCONNECTED, #state{socket = undefined}}.
|
ok ->
|
||||||
|
erlang:start_timer(0, self(), create_transport),
|
||||||
|
{ok, ?STATE_DISCONNECTED, #state{socket = undefined}};
|
||||||
|
{error, Reason} ->
|
||||||
|
{stop, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
-spec callback_mode() -> handle_event_function.
|
-spec callback_mode() -> handle_event_function.
|
||||||
callback_mode() ->
|
callback_mode() ->
|
||||||
@ -91,7 +97,7 @@ handle_event(cast, {metric_data, RouteKey, Metric}, StateName, State = #state{so
|
|||||||
?STATE_ACTIVATED ->
|
?STATE_ACTIVATED ->
|
||||||
send_packet(Socket, [?FRAME_CAST, CastFrame]);
|
send_packet(Socket, [?FRAME_CAST, CastFrame]);
|
||||||
_ ->
|
_ ->
|
||||||
ok = cache_model:insert(<<?FRAME_CAST, CastFrame/binary>>)
|
ok = cache_insert(<<?FRAME_CAST, CastFrame/binary>>)
|
||||||
end,
|
end,
|
||||||
{keep_state, State};
|
{keep_state, State};
|
||||||
|
|
||||||
@ -142,10 +148,10 @@ handle_event(state_timeout, auth_timeout, ?STATE_AUTH, State = #state{socket = S
|
|||||||
|
|
||||||
%% 将缓存中的数据推送到服务器端
|
%% 将缓存中的数据推送到服务器端
|
||||||
handle_event(info, flush_cache, ?STATE_ACTIVATED, State = #state{socket = Socket}) ->
|
handle_event(info, flush_cache, ?STATE_ACTIVATED, State = #state{socket = Socket}) ->
|
||||||
case cache_model:fetch_next() of
|
case cache_fetch_next() of
|
||||||
{ok, {Id, Packet}} ->
|
{ok, {Id, Packet}} ->
|
||||||
send_packet(Socket, Packet),
|
send_packet(Socket, Packet),
|
||||||
cache_model:delete(Id),
|
ok = cache_delete(Id),
|
||||||
{keep_state, State, [{next_event, info, flush_cache}]};
|
{keep_state, State, [{next_event, info, flush_cache}]};
|
||||||
error ->
|
error ->
|
||||||
{keep_state, State}
|
{keep_state, State}
|
||||||
@ -244,6 +250,7 @@ handle_event(info, Info, _, State = #state{}) ->
|
|||||||
-spec terminate(term(), atom(), #state{}) -> ok.
|
-spec terminate(term(), atom(), #state{}) -> ok.
|
||||||
terminate(_Reason, _StateName, _State = #state{socket = Socket}) ->
|
terminate(_Reason, _StateName, _State = #state{socket = Socket}) ->
|
||||||
disconnect(Socket),
|
disconnect(Socket),
|
||||||
|
close_cache_table(),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-spec code_change(term(), atom(), #state{}, term()) -> {ok, atom(), #state{}}.
|
-spec code_change(term(), atom(), #state{}, term()) -> {ok, atom(), #state{}}.
|
||||||
@ -300,6 +307,48 @@ disconnect(Socket) ->
|
|||||||
schedule_reconnect() ->
|
schedule_reconnect() ->
|
||||||
erlang:start_timer(5000, self(), create_transport).
|
erlang:start_timer(5000, self(), create_transport).
|
||||||
|
|
||||||
|
-spec open_cache_table() -> ok | {error, term()}.
|
||||||
|
open_cache_table() ->
|
||||||
|
{ok, DetsDir} = application:get_env(efka, dets_dir),
|
||||||
|
File = DetsDir ++ "cache.dets",
|
||||||
|
case dets:open_file(?CACHE_TAB, [{file, File}, {type, bag}, {keypos, 1}]) of
|
||||||
|
{ok, ?CACHE_TAB} ->
|
||||||
|
ok;
|
||||||
|
{error, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec close_cache_table() -> ok.
|
||||||
|
close_cache_table() ->
|
||||||
|
case dets:close(?CACHE_TAB) of
|
||||||
|
ok ->
|
||||||
|
ok;
|
||||||
|
{error, not_owner} ->
|
||||||
|
ok
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec cache_insert(binary()) -> ok | {error, term()}.
|
||||||
|
cache_insert(Data) when is_binary(Data) ->
|
||||||
|
dets:insert(?CACHE_TAB, {generate_cache_id(), Data}).
|
||||||
|
|
||||||
|
-spec cache_fetch_next() -> error | {ok, {integer(), binary()}}.
|
||||||
|
cache_fetch_next() ->
|
||||||
|
case dets:first(?CACHE_TAB) of
|
||||||
|
'$end_of_table' ->
|
||||||
|
error;
|
||||||
|
Key ->
|
||||||
|
[Entry] = dets:lookup(?CACHE_TAB, Key),
|
||||||
|
{ok, Entry}
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec cache_delete(integer()) -> ok | {error, term()}.
|
||||||
|
cache_delete(Id) when is_integer(Id) ->
|
||||||
|
dets:delete(?CACHE_TAB, Id).
|
||||||
|
|
||||||
|
-spec generate_cache_id() -> integer().
|
||||||
|
generate_cache_id() ->
|
||||||
|
os:system_time(microsecond).
|
||||||
|
|
||||||
-spec send_result_reply(ssl:sslsocket(), integer(), binary()) -> ok.
|
-spec send_result_reply(ssl:sslsocket(), integer(), binary()) -> ok.
|
||||||
send_result_reply(Socket, PacketId, Payload) when is_binary(Payload) ->
|
send_result_reply(Socket, PacketId, Payload) when is_binary(Payload) ->
|
||||||
Packet = message_pb:encode_msg(#'ReplyFrame'{
|
Packet = message_pb:encode_msg(#'ReplyFrame'{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user