fix
This commit is contained in:
parent
dbb571a882
commit
a741eaa6f7
@ -12,7 +12,7 @@
|
|||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_monitor/0, download/3]).
|
-export([start_monitor/0, download/4]).
|
||||||
-export([test/0]).
|
-export([test/0]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
@ -30,16 +30,19 @@ test() ->
|
|||||||
{ok, {Pid, _}} = start_monitor(),
|
{ok, {Pid, _}} = start_monitor(),
|
||||||
Url = "http://118.178.229.213:3000/anlicheng/ekfa/archive1/main.tar.gz",
|
Url = "http://118.178.229.213:3000/anlicheng/ekfa/archive1/main.tar.gz",
|
||||||
TargetDir = "/tmp/",
|
TargetDir = "/tmp/",
|
||||||
download(Pid, Url, TargetDir),
|
|
||||||
|
Ref = make_ref(),
|
||||||
|
download(Pid, Ref, Url, TargetDir),
|
||||||
receive
|
receive
|
||||||
Info ->
|
Info ->
|
||||||
efka_logger:debug("info is: ~p", [Info])
|
efka_logger:debug("info is: ~p", [Info])
|
||||||
end,
|
end,
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-spec download(Pid :: pid(), Url :: string(), TargetDir :: string()) -> reference().
|
-spec download(Pid :: pid(), Ref :: reference(), Url :: string(), TargetDir :: string()) -> reference().
|
||||||
download(Pid, Url, TargetDir) when is_pid(Pid), is_list(Url), is_list(TargetDir) ->
|
download(Pid, Ref, Url, TargetDir) when is_pid(Pid), is_reference(Ref), is_list(Url), is_list(TargetDir) ->
|
||||||
gen_server:cast(Pid, {download, Url, TargetDir}).
|
ReceiverPid = self(),
|
||||||
|
gen_server:cast(Pid, {download, Ref, ReceiverPid, Url, TargetDir}).
|
||||||
|
|
||||||
%% @doc Spawns the server and registers the local name (unique)
|
%% @doc Spawns the server and registers the local name (unique)
|
||||||
-spec(start_monitor() ->
|
-spec(start_monitor() ->
|
||||||
@ -78,7 +81,7 @@ 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({download, Url, TargetDir}, State = #state{}) ->
|
handle_cast({download, Ref, ReceiverPid, Url, TargetDir}, State = #state{}) ->
|
||||||
SslOpts = [
|
SslOpts = [
|
||||||
{ssl, [
|
{ssl, [
|
||||||
% 完全禁用证书验证
|
% 完全禁用证书验证
|
||||||
@ -98,14 +101,20 @@ handle_cast({download, Url, TargetDir}, State = #state{}) ->
|
|||||||
%% 计算操作的时间,单位为毫秒
|
%% 计算操作的时间,单位为毫秒
|
||||||
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
|
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
|
||||||
lager:debug("[efka_downloader] download url: ~p, cost: ~p(ms)", [Url, CostMs]),
|
lager:debug("[efka_downloader] download url: ~p, cost: ~p(ms)", [Url, CostMs]),
|
||||||
|
ReceiverPid ! {downloader_reply, Ref, ok},
|
||||||
|
|
||||||
{stop, normal, State};
|
{stop, normal, State};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
%% 出错需要删除掉文件
|
%% 出错需要删除掉文件
|
||||||
file:delete(FullFilename),
|
file:delete(FullFilename),
|
||||||
{stop, {error, Reason}, State}
|
ReceiverPid ! {downloader_reply, Ref, {error, Reason}},
|
||||||
|
|
||||||
|
{stop, normal, State}
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{stop, {error, Reason}, State}
|
ReceiverPid ! {downloader_reply, Ref, {error, Reason}},
|
||||||
|
|
||||||
|
{stop, normal, State}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
|
|||||||
@ -77,8 +77,9 @@ handle_call({deploy, TaskId, ServerId, TarUrl}, _From, State = #state{root_dir =
|
|||||||
false ->
|
false ->
|
||||||
case check_download_url(TarUrl) of
|
case check_download_url(TarUrl) of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, {TaskPid, MRef}} = efka_downloader:start_monitor(),
|
{ok, {TaskPid, Ref}} = efka_downloader:start_monitor(),
|
||||||
efka_downloader:download(TaskPid, binary_to_list(TarUrl), WorkDir),
|
efka_downloader:download(TaskPid, Ref, binary_to_list(TarUrl), WorkDir),
|
||||||
|
|
||||||
%% 保存服务的上下文
|
%% 保存服务的上下文
|
||||||
Service = #micro_service{
|
Service = #micro_service{
|
||||||
service_id = ServerId,
|
service_id = ServerId,
|
||||||
@ -90,7 +91,7 @@ handle_call({deploy, TaskId, ServerId, TarUrl}, _From, State = #state{root_dir =
|
|||||||
%% 状态: 0: 停止, 1: 运行中
|
%% 状态: 0: 停止, 1: 运行中
|
||||||
status = 0
|
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} ->
|
{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}
|
{reply, {error, <<"download url error">>}, State}
|
||||||
@ -115,11 +116,11 @@ handle_cast(_Request, 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_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),
|
{{TaskId, Service}, NTaskMap} = maps:take(Ref, TaskMap),
|
||||||
lager:debug("[efka_inetd] task_id: ~p", [TaskId]),
|
lager:debug("[efka_inetd] task_id: ~p", [TaskId]),
|
||||||
%% 汇报taskId的执行进度
|
%% 汇报taskId的执行进度
|
||||||
%% 下载完整
|
|
||||||
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 1),
|
efka_agent:feedback_phase(TaskId, efka_util:timestamp(), <<"download">>, 1),
|
||||||
%% 正常启动服务
|
%% 正常启动服务
|
||||||
case start_service(Service) of
|
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});
|
micro_service_model:insert(Service#micro_service{status = 1});
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:debug("[efka_inetd] boot service get error: ~p", [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})
|
micro_service_model:insert(Service#micro_service{status = 0})
|
||||||
end,
|
end,
|
||||||
|
|
||||||
{noreply, State#state{task_map = NTaskMap}};
|
{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),
|
{{TaskId, _Service}, NTaskMap} = maps:take(Ref, TaskMap),
|
||||||
|
|
||||||
lager:debug("[efka_inetd] service: ~p, download get error: ~p", [Reason]),
|
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}};
|
{noreply, State#state{task_map = NTaskMap}};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user