diff --git a/apps/efka/src/efka_inetd_task.erl b/apps/efka/src/efka_inetd_task.erl index 5ee8438..613df7e 100644 --- a/apps/efka/src/efka_inetd_task.erl +++ b/apps/efka/src/efka_inetd_task.erl @@ -34,8 +34,7 @@ -spec deploy(Pid :: pid()) -> no_return(). deploy(Pid) when is_pid(Pid) -> - ReceiverPid = self(), - gen_server:cast(Pid, {deploy, ReceiverPid}). + gen_server:cast(Pid, deploy). %% @doc Spawns the server and registers the local name (unique) -spec(start_link(TaskId :: integer(), RootDir :: string(), ServiceId :: binary(), TarUrl :: binary()) -> @@ -74,13 +73,12 @@ handle_call(_Request, _From, State = #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_cast({deploy, ReceiverPid}, State = #state{task_id = TaskId, root_dir = RootDir, service_id = ServiceId, tar_url = TarUrl}) -> +handle_cast(deploy, State = #state{task_id = TaskId, root_dir = RootDir, service_id = ServiceId, tar_url = TarUrl}) -> %% 创建目录 {ok, ServiceRootDir} = ensure_dirs(RootDir, ServiceId), case check_lock(ServiceRootDir, TarUrl) of true -> - - {reply, ok, State}; + {stop, normal, State}; false -> case check_download_url(TarUrl) of ok -> @@ -88,6 +86,10 @@ handle_cast({deploy, ReceiverPid}, State = #state{task_id = TaskId, root_dir = R {ok, TarFile} -> efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 1), {ok, WorkDir} = make_work_dir(ServiceRootDir), + %% 清理目录下的文件 + Result = delete_directory(WorkDir), + lager:debug("delete_directory result is: ~p", [Result]), + case tar_extract(TarFile, WorkDir) of ok -> %% 创建lock文件 @@ -108,25 +110,22 @@ handle_cast({deploy, ReceiverPid}, State = #state{task_id = TaskId, root_dir = R case boot_service(ServiceId) of {ok, Pid} when is_pid(Pid) -> efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"boot">>, 1), - ok; + {stop, normal, State}; {error, Reason} -> - lager:debug("boot service get error: ~p", [Reason]), - efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"boot">>, 0) + efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"boot">>, 0), + {stop, {error, Reason}, State} end; {error, Reason} -> - ok + {stop, {error, Reason}, State} end; {error, Reason} -> - lager:debug("xx"), - ok + {stop, {error, Reason}, State} end; {error, Reason} -> lager:debug("[efka_inetd] check_download_url: ~p, get error: ~p", [TarUrl, Reason]), - {reply, {error, <<"download url error">>}, State} + {stop, {error, <<"download url error">>}, State} end - end, - - {noreply, State}. + end. %% @private %% @doc Handling all non call/cast messages @@ -174,6 +173,29 @@ make_work_dir(ServiceRootDir) when is_list(ServiceRootDir) -> ok = filelib:ensure_dir(WorkDir), {ok, WorkDir}. +%% 递归删除目录下的问题 +-spec delete_directory(Dir :: string()) -> ok | {error, Reason :: any()}. +delete_directory(Dir) when is_list(Dir) -> + % 递归删除目录内容 + case file:list_dir(Dir) of + {ok, Files} -> + lists:foreach(fun(File) -> + FullPath = filename:join(Dir, File), + case filelib:is_dir(FullPath) of + true -> + delete_directory(FullPath); + false -> + file:delete(FullPath) + end + end, Files), + % 删除空目录 + file:del_dir(Dir); + {error, enoent} -> + ok; + {error, Reason} -> + {error, Reason} + end. + -spec check_lock(DirName :: string(), TarUrl :: binary()) -> boolean(). check_lock(DirName, TarUrl) when is_list(DirName), is_binary(TarUrl) -> FileName = DirName ++ ".efka.lock",