diff --git a/apps/efka/src/docker/docker_commands.erl b/apps/efka/src/docker/docker_commands.erl index 98348e4..3760c7c 100644 --- a/apps/efka/src/docker/docker_commands.erl +++ b/apps/efka/src/docker/docker_commands.erl @@ -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. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/apps/efka/src/docker/docker_deployer.erl b/apps/efka/src/docker/docker_deployer.erl index e6440ce..d42e3d8 100644 --- a/apps/efka/src/docker/docker_deployer.erl +++ b/apps/efka/src/docker/docker_deployer.erl @@ -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, <>) end, Envs), diff --git a/apps/efka/src/docker/docker_manager.erl b/apps/efka/src/docker/docker_manager.erl index 6ea39e8..a8d2251 100644 --- a/apps/efka/src/docker/docker_manager.erl +++ b/apps/efka/src/docker/docker_manager.erl @@ -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; diff --git a/apps/efka/src/docker/docker_deploy_log.erl b/apps/efka/src/docker/docker_task_log.erl similarity index 98% rename from apps/efka/src/docker/docker_deploy_log.erl rename to apps/efka/src/docker/docker_task_log.erl index d584877..ff16cee 100644 --- a/apps/efka/src/docker/docker_deploy_log.erl +++ b/apps/efka/src/docker/docker_task_log.erl @@ -2,11 +2,11 @@ %%% @author anlicheng %%% @copyright (C) 2025, %%% @doc -%%% +%%% TODO 需要完善日志的逻辑 %%% @end %%% Created : 09. 5月 2025 16:45 %%%------------------------------------------------------------------- --module(docker_deploy_log). +-module(docker_task_log). -author("anlicheng"). -behaviour(gen_server).