214 lines
9.1 KiB
Erlang
214 lines
9.1 KiB
Erlang
%%%-------------------------------------------------------------------
|
||
%%% @author anlicheng
|
||
%%% @copyright (C) 2025, <COMPANY>
|
||
%%% @doc
|
||
%%% 1. 需要管理服务的整个生命周期,包括: 启动,停止
|
||
%%% 2. 需要监控服务的状态,通过port的方式
|
||
%%% 3. 服务的启动和关闭,需要在更高的层级控制
|
||
%%% @end
|
||
%%% Created : 18. 4月 2025 16:50
|
||
%%%-------------------------------------------------------------------
|
||
-module(efka_container).
|
||
-author("anlicheng").
|
||
-include("efka_tables.hrl").
|
||
|
||
-behaviour(gen_server).
|
||
|
||
-define(STATUS_RUNNING, running).
|
||
-define(STATUS_STOPPED, stopped).
|
||
|
||
%% API
|
||
-export([start_link/2]).
|
||
-export([get_name/1, get_pid/1, attach_channel/3]).
|
||
-export([invoke/3]).
|
||
-export([metric_data/4, send_event/3]).
|
||
|
||
%% gen_server callbacks
|
||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||
|
||
-record(state, {
|
||
container_id :: binary(),
|
||
%% 通道id信息
|
||
channel_pid :: pid() | undefined,
|
||
%% 数据上传的时候,用来管理容器和微服务
|
||
meta_tag :: binary(),
|
||
|
||
inflight = #{},
|
||
%% 容器的运行状态
|
||
status = ?STATUS_STOPPED,
|
||
|
||
%% 映射关系: #{Ref => Fun}
|
||
callbacks = #{}
|
||
}).
|
||
|
||
%%%===================================================================
|
||
%%% API
|
||
%%%===================================================================
|
||
|
||
-spec get_name(ContainerId :: binary()) -> atom().
|
||
get_name(ContainerId) when is_binary(ContainerId) ->
|
||
list_to_atom("efka_container:" ++ binary_to_list(ContainerId)).
|
||
|
||
-spec get_pid(ContainerId :: binary()) -> undefined | pid().
|
||
get_pid(ContainerId) when is_binary(ContainerId) ->
|
||
whereis(get_name(ContainerId)).
|
||
|
||
-spec invoke(Pid :: pid(), Ref :: reference(), Payload :: binary()) -> no_return().
|
||
invoke(Pid, Ref, Payload) when is_pid(Pid), is_reference(Ref), is_binary(Payload) ->
|
||
gen_server:cast(Pid, {invoke, Ref, self(), Payload}).
|
||
|
||
-spec metric_data(Pid :: pid(), DeviceUUID :: binary(), RouteKey :: binary(), Metric :: binary()) -> no_return().
|
||
metric_data(Pid, DeviceUUID, RouteKey, Metric) when is_pid(Pid), is_binary(DeviceUUID), is_binary(RouteKey), is_binary(Metric) ->
|
||
gen_server:cast(Pid, {metric_data, DeviceUUID, RouteKey, Metric}).
|
||
|
||
-spec send_event(Pid :: pid(), EventType :: integer(), Params :: binary()) -> no_return().
|
||
send_event(Pid, EventType, Params) when is_pid(Pid), is_integer(EventType), is_binary(Params) ->
|
||
gen_server:cast(Pid, {send_event, EventType, Params}).
|
||
|
||
-spec attach_channel(Pid :: pid(), ChannelPid :: pid(), MetaTag :: binary()) -> ok | {error, Reason :: binary()}.
|
||
attach_channel(Pid, ChannelPid, MetaTag) when is_pid(Pid), is_pid(ChannelPid), is_binary(MetaTag) ->
|
||
gen_server:call(Pid, {attach_channel, ChannelPid, MetaTag}).
|
||
|
||
%% @doc Spawns the server and registers the local name (unique)
|
||
-spec(start_link(Name :: atom(), ContainerId :: binary()) ->
|
||
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
||
start_link(Name, ContainerId) when is_atom(Name), is_binary(ContainerId) ->
|
||
gen_server:start_link({local, Name}, ?MODULE, [ContainerId], []).
|
||
|
||
%%%===================================================================
|
||
%%% 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([ContainerId]) ->
|
||
%% supervisor进程通过exit(ChildPid, shutdown)调用的时候,确保terminate函数被调用
|
||
erlang:process_flag(trap_exit, true),
|
||
case efka_docker_command:is_container_running(ContainerId) of
|
||
true ->
|
||
efka_docker_events:monitor_container(self(), ContainerId),
|
||
{ok, #state{container_id = ContainerId, status = ?STATUS_RUNNING}};
|
||
false ->
|
||
efka_docker_events:monitor_container(self(), ContainerId),
|
||
{ok, #state{container_id = ContainerId, status = ?STATUS_STOPPED}}
|
||
end.
|
||
|
||
%% @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{}}).
|
||
%% 绑定channel
|
||
handle_call({attach_channel, ChannelPid, MetaTag}, _From, State = #state{channel_pid = OldChannelPid, container_id = ContainerId}) ->
|
||
case is_pid(OldChannelPid) andalso is_process_alive(OldChannelPid) of
|
||
false ->
|
||
erlang:monitor(process, ChannelPid),
|
||
lager:debug("[efka_service] service_id: ~p, channel attched", [ContainerId]),
|
||
{reply, ok, State#state{channel_pid = ChannelPid, meta_tag = MetaTag}};
|
||
true ->
|
||
{reply, {error, <<"channel exists">>}, State}
|
||
end;
|
||
|
||
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({metric_data, DeviceUUID, RouteKey, Metric}, State = #state{container_id = ContainerId, meta_tag = MetaTag}) ->
|
||
lager:debug("[efka_service] container_id: ~p, meta: ~p, device_uuid: ~p, route_key: ~p, metric data: ~p",
|
||
[ContainerId, MetaTag, DeviceUUID, RouteKey, Metric]),
|
||
%% 这里的数据需要转换成和meta相关的数据
|
||
efka_remote_agent:metric_data(MetaTag, DeviceUUID, RouteKey, Metric),
|
||
{noreply, State};
|
||
|
||
handle_cast({send_event, EventType, Params}, State = #state{container_id = ContainerId, meta_tag = MetaTag}) ->
|
||
efka_remote_agent:event(MetaTag, EventType, Params),
|
||
lager:debug("[efka_service] send_event, container_id: ~p, meta: ~p, event_type: ~p, params: ~p", [ContainerId, MetaTag, EventType, Params]),
|
||
{noreply, State};
|
||
|
||
%% 推送配置项目
|
||
handle_cast({invoke, Ref, ReceiverPid, Payload}, State = #state{channel_pid = ChannelPid, inflight = Inflight}) ->
|
||
case is_pid(ChannelPid) andalso is_process_alive(ChannelPid) of
|
||
true ->
|
||
ws_channel:invoke(ChannelPid, Ref, self(), Payload),
|
||
{noreply, State#state{inflight = maps:put(Ref, ReceiverPid, Inflight)}};
|
||
false ->
|
||
ReceiverPid ! {service_reply, Ref, {error, <<"channel is not alive">>}},
|
||
{reply, State}
|
||
end;
|
||
|
||
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{}}).
|
||
%% 处理channel的回复
|
||
handle_info({channel_reply, Ref, Reply}, State = #state{inflight = Inflight, callbacks = Callbacks}) ->
|
||
case maps:take(Ref, Inflight) of
|
||
error ->
|
||
{noreply, State};
|
||
{ReceiverPid, NInflight} ->
|
||
ReceiverPid ! {service_reply, Ref, Reply},
|
||
|
||
{noreply, State#state{inflight = NInflight, callbacks = trigger_callback(Ref, Callbacks)}}
|
||
end;
|
||
|
||
handle_info({docker_events, start}, State) ->
|
||
{noreply, State#state{status = ?STATUS_RUNNING}};
|
||
|
||
handle_info({docker_events, stop}, State) ->
|
||
{noreply, State#state{status = ?STATUS_STOPPED}};
|
||
|
||
%% 处理channel进程的退出
|
||
handle_info({'DOWN', _Ref, process, ChannelPid, Reason}, State = #state{channel_pid = ChannelPid, container_id = ContainerId}) ->
|
||
lager:debug("[efka_service] service_id: ~p, channel exited: ~p", [ContainerId, Reason]),
|
||
{noreply, State#state{channel_pid = undefined, inflight = #{}}}.
|
||
|
||
%% @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{container_id = ContainerId}) ->
|
||
lager:debug("[efka_service] service_id: ~p, terminate with reason: ~p", [ContainerId, Reason]),
|
||
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 trigger_callback(Ref :: reference(), Callbacks :: map()) -> NewCallbacks :: map().
|
||
trigger_callback(Ref, Callbacks) ->
|
||
case maps:take(Ref, Callbacks) of
|
||
error ->
|
||
Callbacks;
|
||
{Fun, NCallbacks} ->
|
||
catch Fun(),
|
||
NCallbacks
|
||
end. |