This commit is contained in:
anlicheng 2025-05-07 17:07:00 +08:00
parent 4e754cba78
commit 7bb5997063

View File

@ -34,8 +34,7 @@
-spec deploy(Pid :: pid()) -> no_return(). -spec deploy(Pid :: pid()) -> no_return().
deploy(Pid) when is_pid(Pid) -> deploy(Pid) when is_pid(Pid) ->
ReceiverPid = self(), gen_server:cast(Pid, deploy).
gen_server:cast(Pid, {deploy, ReceiverPid}).
%% @doc Spawns the server and registers the local name (unique) %% @doc Spawns the server and registers the local name (unique)
-spec(start_link(TaskId :: integer(), RootDir :: string(), ServiceId :: binary(), TarUrl :: binary()) -> -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{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}). {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), {ok, ServiceRootDir} = ensure_dirs(RootDir, ServiceId),
case check_lock(ServiceRootDir, TarUrl) of case check_lock(ServiceRootDir, TarUrl) of
true -> true ->
{stop, normal, State};
{reply, ok, State};
false -> false ->
case check_download_url(TarUrl) of case check_download_url(TarUrl) of
ok -> ok ->
@ -88,6 +86,10 @@ handle_cast({deploy, ReceiverPid}, State = #state{task_id = TaskId, root_dir = R
{ok, TarFile} -> {ok, TarFile} ->
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 1), efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 1),
{ok, WorkDir} = make_work_dir(ServiceRootDir), {ok, WorkDir} = make_work_dir(ServiceRootDir),
%%
Result = delete_directory(WorkDir),
lager:debug("delete_directory result is: ~p", [Result]),
case tar_extract(TarFile, WorkDir) of case tar_extract(TarFile, WorkDir) of
ok -> ok ->
%% lock文件 %% lock文件
@ -108,25 +110,22 @@ handle_cast({deploy, ReceiverPid}, State = #state{task_id = TaskId, root_dir = R
case boot_service(ServiceId) of case boot_service(ServiceId) of
{ok, Pid} when is_pid(Pid) -> {ok, Pid} when is_pid(Pid) ->
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"boot">>, 1), efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"boot">>, 1),
ok; {stop, normal, State};
{error, Reason} -> {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; end;
{error, Reason} -> {error, Reason} ->
ok {stop, {error, Reason}, State}
end; end;
{error, Reason} -> {error, Reason} ->
lager:debug("xx"), {stop, {error, Reason}, State}
ok
end; end;
{error, Reason} -> {error, Reason} ->
lager:debug("[efka_inetd] check_download_url: ~p, get error: ~p", [TarUrl, 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
end, end.
{noreply, State}.
%% @private %% @private
%% @doc Handling all non call/cast messages %% @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 = filelib:ensure_dir(WorkDir),
{ok, 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(). -spec check_lock(DirName :: string(), TarUrl :: binary()) -> boolean().
check_lock(DirName, TarUrl) when is_list(DirName), is_binary(TarUrl) -> check_lock(DirName, TarUrl) when is_list(DirName), is_binary(TarUrl) ->
FileName = DirName ++ ".efka.lock", FileName = DirName ++ ".efka.lock",