fix
This commit is contained in:
parent
ba391f4b6e
commit
f9059e4934
@ -11,7 +11,6 @@
|
|||||||
%% 用来保存微服务
|
%% 用来保存微服务
|
||||||
-record(micro_service, {
|
-record(micro_service, {
|
||||||
service_id :: binary(),
|
service_id :: binary(),
|
||||||
service_name :: binary(),
|
|
||||||
from :: binary(),
|
from :: binary(),
|
||||||
%% 工作目录
|
%% 工作目录
|
||||||
work_dir :: binary(),
|
work_dir :: binary(),
|
||||||
|
|||||||
@ -27,12 +27,12 @@
|
|||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
test() ->
|
test() ->
|
||||||
{ok, Pid} = start_monitor(),
|
{ok, {Pid, _}} = start_monitor(),
|
||||||
Url = "http://118.178.229.213:3000/anlicheng/ekfa/archive/main.tar.gz",
|
Url = "http://118.178.229.213:3000/anlicheng/ekfa/archive1/main.tar.gz",
|
||||||
TargetDir = "/tmp/",
|
TargetDir = "/tmp/",
|
||||||
Ref = download(Pid, Url, TargetDir),
|
download(Pid, Url, TargetDir),
|
||||||
receive
|
receive
|
||||||
{download_response, Ref, Info} ->
|
Info ->
|
||||||
efka_logger:debug("info is: ~p", [Info])
|
efka_logger:debug("info is: ~p", [Info])
|
||||||
end,
|
end,
|
||||||
ok.
|
ok.
|
||||||
@ -97,7 +97,8 @@ handle_cast({download, Url, TargetDir}, State = #state{}) ->
|
|||||||
EndTs = os:timestamp(),
|
EndTs = os:timestamp(),
|
||||||
%% 计算操作的时间,单位为毫秒
|
%% 计算操作的时间,单位为毫秒
|
||||||
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
|
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
|
||||||
{stop, {ok, CostMs}, State};
|
lager:debug("[efka_downloader] download url: ~p, cost: ~p(ms)", [Url, CostMs]),
|
||||||
|
{stop, normal, State};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
%% 出错需要删除掉文件
|
%% 出错需要删除掉文件
|
||||||
file:delete(FullFilename),
|
file:delete(FullFilename),
|
||||||
@ -143,7 +144,10 @@ receive_data(RequestId, FullFilename) ->
|
|||||||
receive
|
receive
|
||||||
{http, {RequestId, stream_start, _Headers}} ->
|
{http, {RequestId, stream_start, _Headers}} ->
|
||||||
{ok, File} = file:open(FullFilename, [write, binary]),
|
{ok, File} = file:open(FullFilename, [write, binary]),
|
||||||
receive_data1(RequestId, File)
|
receive_data1(RequestId, File);
|
||||||
|
{http, {RequestId, {{_, 404, Status}, _Headers, Body}}} ->
|
||||||
|
lager:debug("[efka_downloader] http_status: ~p, body: ~p", [Status, Body]),
|
||||||
|
{error, Status}
|
||||||
end.
|
end.
|
||||||
%% 接受文件数据
|
%% 接受文件数据
|
||||||
receive_data1(RequestId, File) ->
|
receive_data1(RequestId, File) ->
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(efka_inetd).
|
-module(efka_inetd).
|
||||||
-author("anlicheng").
|
-author("anlicheng").
|
||||||
|
-include("efka_tables.hrl").
|
||||||
|
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
@ -68,8 +69,8 @@ init([]) ->
|
|||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
handle_call({deploy, TaskId, ServerId, From}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) ->
|
handle_call({deploy, TaskId, ServerId, From}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) ->
|
||||||
%% 创建目录
|
%% 创建目录
|
||||||
{ok, ServerRootDir} = ensure_dirs(RootDir, ServerId, From),
|
{ok, WorkDir} = ensure_dirs(RootDir, ServerId, From),
|
||||||
case check_lock(ServerRootDir) of
|
case check_lock(WorkDir) of
|
||||||
true ->
|
true ->
|
||||||
{reply, ok, State};
|
{reply, ok, State};
|
||||||
false ->
|
false ->
|
||||||
@ -77,9 +78,19 @@ handle_call({deploy, TaskId, ServerId, From}, _From, State = #state{root_dir = R
|
|||||||
case check_download_url(DownloadUrl) of
|
case check_download_url(DownloadUrl) of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, {TaskPid, MRef}} = efka_downloader:start_monitor(),
|
{ok, {TaskPid, MRef}} = efka_downloader:start_monitor(),
|
||||||
efka_downloader:download(TaskPid, DownloadUrl, ServerRootDir),
|
efka_downloader:download(TaskPid, DownloadUrl, WorkDir),
|
||||||
{reply, ok, State#state{task_map = maps:put(MRef, TaskId, TaskMap)}};
|
%% 保存服务的上下文
|
||||||
|
Service = #micro_service{
|
||||||
|
service_id = ServerId,
|
||||||
|
from = From,
|
||||||
|
%% 工作目录
|
||||||
|
work_dir = list_to_binary(WorkDir),
|
||||||
|
params = <<"">>,
|
||||||
|
metrics = <<"">>,
|
||||||
|
%% 状态: 0: 停止, 1: 运行中
|
||||||
|
status = 0
|
||||||
|
},
|
||||||
|
{reply, ok, State#state{task_map = maps:put(MRef, {TaskId, Service}, TaskMap)}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:debug("[efka_inetd] check_download_url: ~p, get error: ~p", [DownloadUrl, Reason]),
|
lager:debug("[efka_inetd] check_download_url: ~p, get error: ~p", [DownloadUrl, Reason]),
|
||||||
{reply, {error, <<"download url error">>}, State}
|
{reply, {error, <<"download url error">>}, State}
|
||||||
@ -104,6 +115,28 @@ 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, Reason}, State = #state{task_map = TaskMap}) ->
|
||||||
|
case maps:take(Ref, TaskMap) of
|
||||||
|
error ->
|
||||||
|
{noreply, State};
|
||||||
|
{{TaskId, Service}, NTaskMap} ->
|
||||||
|
lager:debug("[efka_inetd] task_id: ~p", [TaskId]),
|
||||||
|
%% 汇报taskId的执行进度
|
||||||
|
case Reason of
|
||||||
|
normal ->
|
||||||
|
%% 正常启动服务
|
||||||
|
case start_service(Service) of
|
||||||
|
{ok, Pid} ->
|
||||||
|
micro_service_model:insert(Service#micro_service{status = 1});
|
||||||
|
{error, Reason} ->
|
||||||
|
micro_service_model:insert(Service#micro_service{status = 0})
|
||||||
|
end;
|
||||||
|
{error, Reason} ->
|
||||||
|
lager:debug("[efka_inetd] service: ~p, download get error: ~p", [Reason]),
|
||||||
|
{noreply, State#state{task_map = NTaskMap}}
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
|
||||||
handle_info(_Info, State = #state{}) ->
|
handle_info(_Info, State = #state{}) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
@ -176,3 +209,13 @@ make_download_url(From) when is_binary(From) ->
|
|||||||
From1 = binary_to_list(From),
|
From1 = binary_to_list(From),
|
||||||
Basename = lists:flatten(string:replace(From1, ":", "-")) ++ ".tar.gz",
|
Basename = lists:flatten(string:replace(From1, ":", "-")) ++ ".tar.gz",
|
||||||
BaseUrl ++ Basename.
|
BaseUrl ++ Basename.
|
||||||
|
|
||||||
|
-spec start_service(Service :: #micro_service{}) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
|
||||||
|
start_service(S = #micro_service{service_id = ServiceId}) ->
|
||||||
|
%% 正常启动服务
|
||||||
|
case efka_micro_service:get_pid(ServiceId) of
|
||||||
|
undefined ->
|
||||||
|
efka_micro_service_sup:start_service(S);
|
||||||
|
Pid ->
|
||||||
|
{ok, Pid}
|
||||||
|
end.
|
||||||
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/0]).
|
-export([start_link/0]).
|
||||||
-export([register_service/1, delete_service/1]).
|
-export([start_service/1, delete_service/1]).
|
||||||
|
|
||||||
%% Supervisor callbacks
|
%% Supervisor callbacks
|
||||||
-export([init/1]).
|
-export([init/1]).
|
||||||
@ -68,8 +68,8 @@ init([]) ->
|
|||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
-spec register_service(Service :: #micro_service{}) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
|
-spec start_service(Service :: #micro_service{}) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
|
||||||
register_service(Service) ->
|
start_service(Service) ->
|
||||||
case supervisor:start_child(?MODULE, child_spec(Service)) of
|
case supervisor:start_child(?MODULE, child_spec(Service)) of
|
||||||
{ok, Pid} when is_pid(Pid) ->
|
{ok, Pid} when is_pid(Pid) ->
|
||||||
{ok, Pid};
|
{ok, Pid};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user