ekfa/apps/efka/src/efka_inetd_task_log.erl
2025-05-20 16:09:00 +08:00

137 lines
5.0 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 09. 5月 2025 16:45
%%%-------------------------------------------------------------------
-module(efka_inetd_task_log).
-author("anlicheng").
-behaviour(gen_server).
%% API
-export([start_link/0]).
-export([stash/2, flush/1, get_logs/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-record(state, {
%% #{task_id => queue:new()}
pending_map = #{}
}).
%%%===================================================================
%%% API
%%%===================================================================
-spec stash(TaskId :: integer(), Items :: binary() | [binary()]) -> no_return().
stash(TaskId, Log) when is_integer(TaskId), is_binary(Log) ->
stash(TaskId, [Log]);
stash(TaskId, Items) when is_integer(TaskId), is_list(Items) ->
{{Y, M, D}, {H, I, S}} = calendar:local_time(),
TimePrefix = iolist_to_binary(io_lib:format("[~b-~2..0b-~2..0b ~2..0b:~2..0b:~2..0b]", [Y, M, D, H, I, S])),
Log = iolist_to_binary([TimePrefix, <<" ">>, lists:join(<<" ">>, Items)]),
gen_server:cast(?SERVER, {stash, TaskId, Log}).
-spec flush(TaskId :: integer()) -> no_return().
flush(TaskId) when is_integer(TaskId) ->
gen_server:cast(?SERVER, {flush, TaskId}).
-spec get_logs(TaskId :: integer()) -> {ok, Logs :: list()}.
get_logs(TaskId) when is_integer(TaskId) ->
gen_server:call(?SERVER, {get_logs, TaskId}).
%% @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, #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({get_logs, TaskId}, _From, State = #state{pending_map = PendingMap}) ->
case maps:find(TaskId, PendingMap) of
error ->
Logs = task_log_model:get_logs(TaskId),
{reply, {ok, Logs}, State};
{ok, Q} ->
Logs = queue:to_list(Q),
{reply, {ok, Logs}, State}
end.
%% @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({stash, TaskId, Log}, State = #state{pending_map = PendingMap}) ->
Q = maps:get(TaskId, PendingMap, queue:new()),
NQ = queue:in(Log, Q),
{noreply, State#state{pending_map = maps:put(TaskId, NQ, PendingMap)}};
handle_cast({flush, TaskId}, State = #state{pending_map = PendingMap}) ->
case maps:take(TaskId, PendingMap) of
error ->
{noreply, State};
{Q, NPendingMap} ->
Logs = queue:to_list(Q),
ok = task_log_model:insert(TaskId, Logs),
{noreply, State#state{pending_map = NPendingMap}}
end.
%% @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{}) ->
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
%%%===================================================================