service管理

This commit is contained in:
anlicheng 2025-09-29 16:24:04 +08:00
parent 5a573be0fc
commit 6a75597134
4 changed files with 33 additions and 7 deletions

View File

@ -8,6 +8,9 @@
%%%-------------------------------------------------------------------
-author("anlicheng").
-define(SERVICE_STOPPED, 0).
-define(SERVICE_RUNNING, 1).
%%
-record(service, {
service_id :: binary(),
@ -15,5 +18,7 @@
%% ,
meta_data = #{} :: map(),
%% 0: , 1:
status = 0
status = 0,
create_ts = 0 :: integer(),
update_ts = 0 :: integer()
}).

View File

@ -42,7 +42,7 @@ create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerNa
Volumes = [<<ConfigFile/binary, ":/usr/local/etc/service.conf">>|Volumes0],
NewConfig = Config#{<<"volumes">> => Volumes},
Options = build_options(NewConfig),
Options = build_options(ContainerName, NewConfig),
lists:foreach(fun({K, V}) -> lager:debug("~p => ~p", [K, V]) end, maps:to_list(Options)),
Body = iolist_to_binary(jiffy:encode(Options, [force_utf8])),
@ -201,12 +201,15 @@ inspect_container(ContainerId) when is_binary(ContainerId) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% JSON Map
build_options(Config) when is_map(Config) ->
build_options(ContainerName, Config) when is_binary(ContainerName), is_map(Config) ->
%%
Envs0 = maps:get(<<"envs">>, Config, []),
Envs = [<<"CONTAINER_NAME=", ContainerName>>|Envs0],
#{
<<"Image">> => maps:get(<<"image">>, Config, <<>>),
<<"Cmd">> => maps:get(<<"command">>, Config, []),
<<"Entrypoint">> => maps:get(<<"entrypoint">>, Config, []),
<<"Env">> => maps:get(<<"envs">>, Config, []),
<<"Env">> => Envs,
<<"Labels">> => maps:get(<<"labels">>, Config, #{}),
<<"Volumes">> => build_volumes(Config),
<<"User">> => maps:get(<<"user">>, Config, <<>>),

View File

@ -83,8 +83,18 @@ init([]) ->
{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),
handle_call({insert, Service = #service{service_id = ServiceId}}, _From, State = #state{}) ->
case dets:lookup(?TAB, ServiceId) of
[] ->
ok = dets:insert(?TAB, Service);
[OldService] ->
NewService = OldService#service{
meta_data = Service#service.meta_data,
container_name = Service#service.container_name,
update_ts = Service#service.update_ts
},
ok = dets:insert(?TAB, NewService)
end,
{reply, ok, State};
handle_call({change_status, ServiceId, NewStatus}, _From, State = #state{}) ->

View File

@ -96,7 +96,15 @@ handle_request(#{<<"id">> := Id, <<"method">> := <<"register">>, <<"params">> :=
%%
MetaData = maps:get(<<"meta_data">>, Params, #{}),
ok = service_model:insert(#service{service_id = ServiceId, status = 1, meta_data = MetaData}),
ContainerName = maps:get(<<"container_name">>, Params, <<>>),
ok = service_model:insert(#service{
service_id = ServiceId,
container_name = ContainerName,
status = ?SERVICE_RUNNING,
meta_data = MetaData,
create_ts = efka_util:timestamp(),
update_ts = efka_util:timestamp()
}),
{reply, {text, Reply}, State#state{service_id = ServiceId, service_pid = ServicePid, is_registered = true}};
{error, Error} ->