From 45e3e1ec78b1995fab6559e50baaddb0322ae1d6 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 20 May 2025 11:14:00 +0800 Subject: [PATCH] fix --- apps/efka/src/efka_inetd.erl | 8 ++--- apps/efka/src/efka_inetd_task.erl | 51 ++++++++++++++---------------- apps/efka/src/efka_service.erl | 4 +-- apps/efka/src/efka_service_sup.erl | 9 ++---- 4 files changed, 31 insertions(+), 41 deletions(-) diff --git a/apps/efka/src/efka_inetd.erl b/apps/efka/src/efka_inetd.erl index ba08c9c..c5ec919 100644 --- a/apps/efka/src/efka_inetd.erl +++ b/apps/efka/src/efka_inetd.erl @@ -81,9 +81,7 @@ handle_call({deploy, TaskId, ServiceId, TarUrl}, _From, State = #state{root_dir {ok, ServiceRootDir} = ensure_dirs(RootDir, ServiceId), ServicePid = efka_service:get_pid(ServiceId), - lager:debug("service pid is: ~p", [ServicePid]), - - case is_pid(ServicePid) andalso efka_service:is_running(ServicePid) of + case is_pid(ServicePid) of true -> {reply, {error, <<"the service is running, stop first">>}, State}; false -> @@ -121,8 +119,8 @@ handle_call({stop_service, ServiceId}, _From, State = #state{}) -> case efka_service:get_pid(ServiceId) of undefined -> {reply, {error, <<"service not running">>}, State}; - ServicePid -> - efka_service_sup:delete_service(ServicePid), + ServicePid when is_pid(ServicePid) -> + efka_service_sup:delete_service(ServiceId), %% 主动停止的服务,需要更新数据库状态, 状态是为了保证下次efka重启的时候,不自动启动服务 ok = service_model:change_status(ServiceId, 0), {reply, ok, State} diff --git a/apps/efka/src/efka_inetd_task.erl b/apps/efka/src/efka_inetd_task.erl index 0fcda29..12010e8 100644 --- a/apps/efka/src/efka_inetd_task.erl +++ b/apps/efka/src/efka_inetd_task.erl @@ -119,28 +119,31 @@ do_deploy(TaskId, ServiceRootDir, ServiceId, TarUrl) when is_integer(TaskId), is %% 创建工作目录 WorkDir = ServiceRootDir ++ "/work_dir/", - ok = filelib:ensure_dir(WorkDir), - - %% 清理目录下的文件 - Result = delete_directory(WorkDir), - lager:debug("[efka_inetd_task] delete_directory result is: ~p", [Result]), - case tar_extract(TarFile, WorkDir) of + case filelib:ensure_dir(WorkDir) of ok -> - %% 更新数据 - ok = service_model:insert(#service{ - service_id = ServiceId, - tar_url = TarUrl, - %% 工作目录 - root_dir = ServiceRootDir, - params = <<"">>, - metrics = <<"">>, - %% 状态: 0: 停止, 1: 运行中 - status = 0 - }), - efka_inetd_task_log:stash(TaskId, <<"deploy success">>); + %% 清理目录下的文件 + catch delete_directory(WorkDir), + case tar_extract(TarFile, WorkDir) of + ok -> + %% 更新数据 + ok = service_model:insert(#service{ + service_id = ServiceId, + tar_url = TarUrl, + %% 工作目录 + root_dir = ServiceRootDir, + params = <<"">>, + metrics = <<"">>, + %% 状态: 0: 停止, 1: 运行中 + status = 0 + }), + efka_inetd_task_log:stash(TaskId, <<"deploy success">>); + {error, Reason} -> + TarLog = io_lib:format("tar decompression: ~p, error: ~p", [filename:basename(TarFile), Reason]), + efka_inetd_task_log:stash(TaskId, list_to_binary(TarLog)) + end; {error, Reason} -> - TarLog = io_lib:format("tar decompression: ~p, error: ~p", [filename:basename(TarFile), Reason]), - efka_inetd_task_log:stash(TaskId, list_to_binary(TarLog)) + DownloadLog = io_lib:format("make work_dir error: ~p", [Reason]), + efka_inetd_task_log:stash(TaskId, list_to_binary(DownloadLog)) end; {error, Reason} -> DownloadLog = io_lib:format("download: ~p, error: ~p", [binary_to_list(TarUrl), Reason]), @@ -174,13 +177,7 @@ delete_directory(Dir) when is_list(Dir) -> -spec tar_extract(string(), string()) -> ok | {error, term()}. tar_extract(TarFile, TargetDir) when is_list(TarFile), is_list(TargetDir) -> %% 判断文件的后缀名来判断 - Ext = filename:extension(TarFile), - case Ext of - ".tar" -> - erl_tar:extract(TarFile, [{cwd, TargetDir}, verbose]); - ".gz" -> - erl_tar:extract(TarFile, [compressed, {cwd, TargetDir}, verbose]) - end. + erl_tar:extract(TarFile, [compressed, {cwd, TargetDir}, verbose]). %% 下载文件 -spec download(Url :: string(), TargetDir :: string()) -> diff --git a/apps/efka/src/efka_service.erl b/apps/efka/src/efka_service.erl index c4758a2..65cc549 100644 --- a/apps/efka/src/efka_service.erl +++ b/apps/efka/src/efka_service.erl @@ -52,7 +52,7 @@ get_pid(ServiceId) when is_binary(ServiceId) -> push_config(Pid, Ref, ConfigJson) when is_pid(Pid), is_binary(ConfigJson) -> gen_server:cast(Pid, {push_config, Ref, self(), ConfigJson}). --spec push_config(Pid :: pid(), Ref :: reference(), Payload :: binary()) -> no_return(). +-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}). @@ -231,7 +231,7 @@ handle_info({'DOWN', _Ref, process, ChannelPid, Reason}, State = #state{channel_ State :: #state{}) -> term()). terminate(Reason, _State = #state{service_id = ServiceId, port = Port, os_pid = OSPid}) -> erlang:is_port(Port) andalso erlang:port_close(Port), - kill_os_pid(OSPid), + catch kill_os_pid(OSPid), lager:debug("[efka_service] service_id: ~p, terminate with reason: ~p", [ServiceId, Reason]), ok. diff --git a/apps/efka/src/efka_service_sup.erl b/apps/efka/src/efka_service_sup.erl index ec9b665..e83ad95 100644 --- a/apps/efka/src/efka_service_sup.erl +++ b/apps/efka/src/efka_service_sup.erl @@ -39,15 +39,10 @@ start_link() -> %% this function is called by the new process to find out about %% restart strategy, maximum restart frequency and child %% specifications. --spec(init(Args :: term()) -> - {ok, {SupFlags :: {RestartStrategy :: supervisor:strategy(), - MaxR :: non_neg_integer(), MaxT :: non_neg_integer()}, - [ChildSpec :: supervisor:child_spec()]}} - | ignore | {error, Reason :: term()}). init([]) -> SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, %% 简化逻辑,只启动需要运行的微服务 - Services = service_model:get_running_services(), + {ok, Services} = service_model:get_running_services(), Specs = lists:map(fun(ServiceId) -> child_spec(ServiceId) end, Services), {ok, {SupFlags, Specs}}. @@ -70,7 +65,7 @@ start_service(ServiceId) when is_binary(ServiceId) -> -spec delete_service(ServiceId :: binary()) -> ok. delete_service(ServiceId) when is_binary(ServiceId) -> ChildId = efka_service:get_name(ServiceId), - ok = supervisor:terminate_child(?MODULE, ChildId), + supervisor:terminate_child(?MODULE, ChildId), supervisor:delete_child(?MODULE, ChildId). child_spec(#service{service_id = ServiceId}) when is_binary(ServiceId) ->