This commit is contained in:
anlicheng 2025-05-07 15:21:11 +08:00
parent dbb571a882
commit a741eaa6f7
2 changed files with 39 additions and 17 deletions

View File

@ -12,7 +12,7 @@
-behaviour(gen_server).
%% API
-export([start_monitor/0, download/3]).
-export([start_monitor/0, download/4]).
-export([test/0]).
%% gen_server callbacks
@ -30,16 +30,19 @@ test() ->
{ok, {Pid, _}} = start_monitor(),
Url = "http://118.178.229.213:3000/anlicheng/ekfa/archive1/main.tar.gz",
TargetDir = "/tmp/",
download(Pid, Url, TargetDir),
Ref = make_ref(),
download(Pid, Ref, Url, TargetDir),
receive
Info ->
efka_logger:debug("info is: ~p", [Info])
end,
ok.
-spec download(Pid :: pid(), Url :: string(), TargetDir :: string()) -> reference().
download(Pid, Url, TargetDir) when is_pid(Pid), is_list(Url), is_list(TargetDir) ->
gen_server:cast(Pid, {download, Url, TargetDir}).
-spec download(Pid :: pid(), Ref :: reference(), Url :: string(), TargetDir :: string()) -> reference().
download(Pid, Ref, Url, TargetDir) when is_pid(Pid), is_reference(Ref), is_list(Url), is_list(TargetDir) ->
ReceiverPid = self(),
gen_server:cast(Pid, {download, Ref, ReceiverPid, Url, TargetDir}).
%% @doc Spawns the server and registers the local name (unique)
-spec(start_monitor() ->
@ -78,7 +81,7 @@ handle_call(_Request, _From, State = #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast({download, Url, TargetDir}, State = #state{}) ->
handle_cast({download, Ref, ReceiverPid, Url, TargetDir}, State = #state{}) ->
SslOpts = [
{ssl, [
%
@ -98,14 +101,20 @@ handle_cast({download, Url, TargetDir}, State = #state{}) ->
%%
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
lager:debug("[efka_downloader] download url: ~p, cost: ~p(ms)", [Url, CostMs]),
ReceiverPid ! {downloader_reply, Ref, ok},
{stop, normal, State};
{error, Reason} ->
%%
file:delete(FullFilename),
{stop, {error, Reason}, State}
ReceiverPid ! {downloader_reply, Ref, {error, Reason}},
{stop, normal, State}
end;
{error, Reason} ->
{stop, {error, Reason}, State}
ReceiverPid ! {downloader_reply, Ref, {error, Reason}},
{stop, normal, State}
end.
%% @private

View File

@ -77,8 +77,9 @@ handle_call({deploy, TaskId, ServerId, TarUrl}, _From, State = #state{root_dir =
false ->
case check_download_url(TarUrl) of
ok ->
{ok, {TaskPid, MRef}} = efka_downloader:start_monitor(),
efka_downloader:download(TaskPid, binary_to_list(TarUrl), WorkDir),
{ok, {TaskPid, Ref}} = efka_downloader:start_monitor(),
efka_downloader:download(TaskPid, Ref, binary_to_list(TarUrl), WorkDir),
%%
Service = #micro_service{
service_id = ServerId,
@ -90,7 +91,7 @@ handle_call({deploy, TaskId, ServerId, TarUrl}, _From, State = #state{root_dir =
%% 0: , 1:
status = 0
},
{reply, ok, State#state{task_map = maps:put(MRef, {TaskId, Service}, TaskMap)}};
{reply, ok, State#state{task_map = maps:put(Ref, {TaskId, Service}, TaskMap)}};
{error, Reason} ->
lager:debug("[efka_inetd] check_download_url: ~p, get error: ~p", [TarUrl, Reason]),
{reply, {error, <<"download url error">>}, State}
@ -115,11 +116,11 @@ handle_cast(_Request, State = #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_info({'DOWN', Ref, process, _Pid, normal}, State = #state{task_map = TaskMap}) ->
%%
handle_info({downloader_reply, Ref, ok}, State = #state{task_map = TaskMap}) ->
{{TaskId, Service}, NTaskMap} = maps:take(Ref, TaskMap),
lager:debug("[efka_inetd] task_id: ~p", [TaskId]),
%% taskId的执行进度
%%
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 1),
%%
case start_service(Service) of
@ -129,18 +130,30 @@ handle_info({'DOWN', Ref, process, _Pid, normal}, State = #state{task_map = Task
micro_service_model:insert(Service#micro_service{status = 1});
{error, Reason} ->
lager:debug("[efka_inetd] boot service get error: ~p", [Reason]),
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"boot">>, 0, <<"boot failed">>),
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"boot">>, 0),
micro_service_model:insert(Service#micro_service{status = 0})
end,
{noreply, State#state{task_map = NTaskMap}};
handle_info({'DOWN', Ref, process, _Pid, {error, Reason}}, State = #state{task_map = TaskMap}) ->
%%
handle_info({downloader_reply, Ref, {error, Reason}}, State = #state{task_map = TaskMap}) ->
{{TaskId, _Service}, NTaskMap} = maps:take(Ref, TaskMap),
lager:debug("[efka_inetd] service: ~p, download get error: ~p", [Reason]),
%%
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 0, <<"下载失败">>),
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 0),
{noreply, State#state{task_map = NTaskMap}};
%% 退
handle_info({'DOWN', _Ref, process, _Pid, normal}, State) ->
{noreply, State};
%% 退
handle_info({'DOWN', Ref, process, _Pid, {error, Reason}}, State = #state{task_map = TaskMap}) ->
{{TaskId, _Service}, NTaskMap} = maps:take(Ref, TaskMap),
lager:debug("[efka_inetd] service: ~p, download get error: ~p", [Reason]),
%%
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 0),
{noreply, State#state{task_map = NTaskMap}};