This commit is contained in:
anlicheng 2025-09-17 10:48:15 +08:00
parent 4eea5eb880
commit aff440648a
4 changed files with 27 additions and 36 deletions

View File

@ -119,7 +119,7 @@ check_container_exist(ContainerName) when is_binary(ContainerName) ->
false
end.
-spec start_container(ContainerName :: binary()) -> boolean().
-spec start_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
start_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1",
@ -127,15 +127,15 @@ start_container(ContainerName) when is_binary(ContainerName) ->
Port when is_port(Port) ->
case gather_output(Port) of
{0, _} ->
true;
ok;
{_ExitCode, _Error} ->
false
{error, <<"启动失败"/utf8>>}
end;
_Error ->
false
{error, <<"启动失败"/utf8>>}
end.
-spec stop_container(ContainerName :: binary()) -> boolean().
-spec stop_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
stop_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker stop " ++ binary_to_list(ContainerName),
@ -143,12 +143,12 @@ stop_container(ContainerName) when is_binary(ContainerName) ->
Port when is_port(Port) ->
case gather_output(Port) of
{0, _} ->
true;
ok;
{_ExitCode, _Error} ->
false
{error, <<>>}
end;
_Error ->
false
{error, <<>>}
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -179,11 +179,11 @@ do_deploy(TaskId, RootDir, Config) when is_integer(TaskId), is_list(RootDir), is
%% envs参数
%maybe_create_env_file(ContainerDir, maps:get(<<"envs">>, Config, [])),
%% , : "/etc/容器名称/"
case efka_docker_command:check_container_exist(ContainerName) of
case docker_commands:check_container_exist(ContainerName) of
true ->
{error, <<"container exist">>};
false ->
case efka_docker_command:create_container(ContainerName, ContainerDir, Config) of
case docker_commands:create_container(ContainerName, ContainerDir, Config) of
{ok, ContainerId} ->
{ok, ContainerId};
{error, Reason} ->
@ -206,7 +206,6 @@ try_pull_image(Image) when is_binary(Image) ->
maybe_create_env_file(_ContainerDir, []) ->
ok;
maybe_create_env_file(ContainerDir, Envs) when is_list(Envs)->
lager:debug("envs: ~p", [Envs]),
TargetFile = ContainerDir ++ "env",
{ok, IoDevice} = file:open(TargetFile, [write, binary]),
lists:foreach(fun(Env) -> file:write(IoDevice, <<Env/binary, $\n>>) end, Envs),

View File

@ -84,30 +84,20 @@ handle_call({deploy, TaskId, Config}, _From, State = #state{root_dir = RootDir,
%% :
handle_call({start_container, ContainerId}, _From, State) ->
case efka_container:get_pid(ContainerId) of
undefined ->
case efka_container_sup:start_container(ContainerId) of
{ok, _} ->
%% , efka重启的时候
ok = service_model:change_status(ContainerId, 1),
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
ContainerPid when is_pid(ContainerPid) ->
{reply, {error, <<"service is running">>}, State}
case docker_commands:start_container(ContainerId) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
%% , status字段
handle_call({stop_container, ContainerId}, _From, State = #state{}) ->
case efka_container:get_pid(ContainerId) of
undefined ->
{reply, {error, <<"service not running">>}, State};
ContainerPid when is_pid(ContainerPid) ->
efka_container_sup:stop_container(ContainerPid),
%% , efka重启的时候
ok = service_model:change_status(ContainerId, 0),
{reply, ok, State}
case docker_commands:stop_container(ContainerId) of
ok ->
{reply, ok, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call(_Request, _From, State = #state{}) ->
@ -136,11 +126,13 @@ handle_info({'EXIT', TaskPid, Reason}, State = #state{task_map = TaskMap}) ->
case Reason of
normal ->
lager:debug("[efka_inetd] task_pid: ~p, exit normal", [TaskPid]),
efka_inetd_task_log:flush(TaskId);
%efka_inetd_task_log:flush(TaskId);
ok;
Error ->
lager:notice("[efka_inetd] task_pid: ~p, exit with error: ~p", [TaskPid, Error]),
efka_inetd_task_log:stash(TaskId, <<"task aborted">>),
efka_inetd_task_log:flush(TaskId)
%efka_inetd_task_log:stash(TaskId, <<"task aborted">>),
%efka_inetd_task_log:flush(TaskId)
ok
end,
{noreply, State#state{task_map = NTaskMap}}
end;

View File

@ -2,11 +2,11 @@
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% TODO
%%% @end
%%% Created : 09. 5 2025 16:45
%%%-------------------------------------------------------------------
-module(docker_deploy_log).
-module(docker_task_log).
-author("anlicheng").
-behaviour(gen_server).