%%%------------------------------------------------------------------- %%% @author anlicheng %%% @copyright (C) 2025, %%% @doc %%% %%% @end %%% Created : 13. 8月 2025 16:41 %%%------------------------------------------------------------------- -module(service_model). -author("anlicheng"). -include("efka_tables.hrl"). -behaviour(gen_server). %% API -export([start_link/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, service). -record(state, { }). %%%=================================================================== %%% API %%%=================================================================== insert(Service = #service{}) -> gen_server:call(?SERVER, {insert, Service}). change_status(ServiceId, NewStatus) when is_binary(ServiceId), is_integer(NewStatus) -> gen_server:call(?SERVER, {change_status, ServiceId, NewStatus}). -spec set_config(ServiceId :: binary(), ConfigJson :: binary()) -> ok | {error, Reason :: any()}. set_config(ServiceId, ConfigJson) when is_binary(ServiceId), is_binary(ConfigJson) -> gen_server:call(?SERVER, {set_config, ServiceId, ConfigJson}). -spec get_config_json(ServiceId :: binary()) -> error | {ok, ConfigJson :: binary()}. get_config_json(ServiceId) when is_binary(ServiceId) -> gen_server:call(?SERVER, {get_config_json, ServiceId}). -spec get_status(ServiceId :: binary()) -> Status :: integer(). get_status(ServiceId) when is_binary(ServiceId) -> gen_server:call(?SERVER, {get_status, ServiceId}). -spec get_service(ServiceId :: binary()) -> error | {ok, Service :: #service{}}. get_service(ServiceId) when is_binary(ServiceId) -> gen_server:call(?SERVER, {get_service, ServiceId}). -spec get_all_services() -> [#service{}]. get_all_services() -> gen_server:call(?SERVER, get_all_services). -spec get_all_service_ids() -> [ServiceId :: binary()]. get_all_service_ids() -> gen_server:call(?SERVER, get_all_service_ids). -spec get_running_services() -> {ok, [#service{}]} | {error, Reason :: term()}. get_running_services() -> gen_server:call(?SERVER, get_running_services). %% @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 ++ "service.dets", {ok, ?TAB} = dets:open_file(?TAB, [{file, File}, {type, bag}, {keypos, 2}]), {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, Service}, _From, State = #state{}) -> ok = dets:insert(?TAB, Service), {reply, ok, State}; handle_call({change_status, ServiceId, NewStatus}, _From, State = #state{}) -> case dets:lookup(?TAB, ServiceId) of [] -> {reply, {error, <<"service not found">>}, State}; [OldService] -> NewService = OldService#service{status = NewStatus}, ok = dets:insert(?TAB, NewService), {reply, ok, State} end; handle_call({set_config, ServiceId, ConfigJson}, _From, State = #state{}) -> case dets:lookup(?TAB, ServiceId) of [] -> {reply, {error, <<"service not found">>}, State}; [OldService] -> NewService = OldService#service{config_json = ConfigJson}, ok = dets:insert(?TAB, NewService), {reply, ok, State} end; handle_call({get_config_json, ServiceId}, _From, State = #state{}) -> case dets:lookup(?TAB, ServiceId) of [] -> {reply, error, State}; [#service{config_json = ConfigJson}] -> {reply, {ok, ConfigJson}, State} end; handle_call({get_status, ServiceId}, _From, State = #state{}) -> case dets:lookup(?TAB, ServiceId) of [] -> {reply, 0, State}; [#service{status = Status}] -> {reply, Status, State} end; handle_call(get_all_services, _From, State = #state{}) -> Items = dets:foldl(fun(Record, Acc) -> [Record|Acc] end, [], ?TAB), {reply, lists:reverse(Items), State}; handle_call(get_running_services, _From, State = #state{}) -> Items = dets:foldl(fun(Record, Acc) -> [Record|Acc] end, [], ?TAB), RunningItems = lists:filter(fun(#service{status = Status}) -> Status =:= 1 end, lists:reverse(Items)), {reply, RunningItems, 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{}) -> ok = 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 %%%===================================================================