fix
This commit is contained in:
parent
d15efa1a96
commit
45e3e1ec78
@ -81,9 +81,7 @@ handle_call({deploy, TaskId, ServiceId, TarUrl}, _From, State = #state{root_dir
|
|||||||
{ok, ServiceRootDir} = ensure_dirs(RootDir, ServiceId),
|
{ok, ServiceRootDir} = ensure_dirs(RootDir, ServiceId),
|
||||||
|
|
||||||
ServicePid = efka_service:get_pid(ServiceId),
|
ServicePid = efka_service:get_pid(ServiceId),
|
||||||
lager:debug("service pid is: ~p", [ServicePid]),
|
case is_pid(ServicePid) of
|
||||||
|
|
||||||
case is_pid(ServicePid) andalso efka_service:is_running(ServicePid) of
|
|
||||||
true ->
|
true ->
|
||||||
{reply, {error, <<"the service is running, stop first">>}, State};
|
{reply, {error, <<"the service is running, stop first">>}, State};
|
||||||
false ->
|
false ->
|
||||||
@ -121,8 +119,8 @@ handle_call({stop_service, ServiceId}, _From, State = #state{}) ->
|
|||||||
case efka_service:get_pid(ServiceId) of
|
case efka_service:get_pid(ServiceId) of
|
||||||
undefined ->
|
undefined ->
|
||||||
{reply, {error, <<"service not running">>}, State};
|
{reply, {error, <<"service not running">>}, State};
|
||||||
ServicePid ->
|
ServicePid when is_pid(ServicePid) ->
|
||||||
efka_service_sup:delete_service(ServicePid),
|
efka_service_sup:delete_service(ServiceId),
|
||||||
%% 主动停止的服务,需要更新数据库状态, 状态是为了保证下次efka重启的时候,不自动启动服务
|
%% 主动停止的服务,需要更新数据库状态, 状态是为了保证下次efka重启的时候,不自动启动服务
|
||||||
ok = service_model:change_status(ServiceId, 0),
|
ok = service_model:change_status(ServiceId, 0),
|
||||||
{reply, ok, State}
|
{reply, ok, State}
|
||||||
|
|||||||
@ -119,28 +119,31 @@ do_deploy(TaskId, ServiceRootDir, ServiceId, TarUrl) when is_integer(TaskId), is
|
|||||||
|
|
||||||
%% 创建工作目录
|
%% 创建工作目录
|
||||||
WorkDir = ServiceRootDir ++ "/work_dir/",
|
WorkDir = ServiceRootDir ++ "/work_dir/",
|
||||||
ok = filelib:ensure_dir(WorkDir),
|
case filelib:ensure_dir(WorkDir) of
|
||||||
|
|
||||||
%% 清理目录下的文件
|
|
||||||
Result = delete_directory(WorkDir),
|
|
||||||
lager:debug("[efka_inetd_task] delete_directory result is: ~p", [Result]),
|
|
||||||
case tar_extract(TarFile, WorkDir) of
|
|
||||||
ok ->
|
ok ->
|
||||||
%% 更新数据
|
%% 清理目录下的文件
|
||||||
ok = service_model:insert(#service{
|
catch delete_directory(WorkDir),
|
||||||
service_id = ServiceId,
|
case tar_extract(TarFile, WorkDir) of
|
||||||
tar_url = TarUrl,
|
ok ->
|
||||||
%% 工作目录
|
%% 更新数据
|
||||||
root_dir = ServiceRootDir,
|
ok = service_model:insert(#service{
|
||||||
params = <<"">>,
|
service_id = ServiceId,
|
||||||
metrics = <<"">>,
|
tar_url = TarUrl,
|
||||||
%% 状态: 0: 停止, 1: 运行中
|
%% 工作目录
|
||||||
status = 0
|
root_dir = ServiceRootDir,
|
||||||
}),
|
params = <<"">>,
|
||||||
efka_inetd_task_log:stash(TaskId, <<"deploy success">>);
|
metrics = <<"">>,
|
||||||
|
%% 状态: 0: 停止, 1: 运行中
|
||||||
|
status = 0
|
||||||
|
}),
|
||||||
|
efka_inetd_task_log:stash(TaskId, <<"deploy success">>);
|
||||||
|
{error, Reason} ->
|
||||||
|
TarLog = io_lib:format("tar decompression: ~p, error: ~p", [filename:basename(TarFile), Reason]),
|
||||||
|
efka_inetd_task_log:stash(TaskId, list_to_binary(TarLog))
|
||||||
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
TarLog = io_lib:format("tar decompression: ~p, error: ~p", [filename:basename(TarFile), Reason]),
|
DownloadLog = io_lib:format("make work_dir error: ~p", [Reason]),
|
||||||
efka_inetd_task_log:stash(TaskId, list_to_binary(TarLog))
|
efka_inetd_task_log:stash(TaskId, list_to_binary(DownloadLog))
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
DownloadLog = io_lib:format("download: ~p, error: ~p", [binary_to_list(TarUrl), Reason]),
|
DownloadLog = io_lib:format("download: ~p, error: ~p", [binary_to_list(TarUrl), Reason]),
|
||||||
@ -174,13 +177,7 @@ delete_directory(Dir) when is_list(Dir) ->
|
|||||||
-spec tar_extract(string(), string()) -> ok | {error, term()}.
|
-spec tar_extract(string(), string()) -> ok | {error, term()}.
|
||||||
tar_extract(TarFile, TargetDir) when is_list(TarFile), is_list(TargetDir) ->
|
tar_extract(TarFile, TargetDir) when is_list(TarFile), is_list(TargetDir) ->
|
||||||
%% 判断文件的后缀名来判断
|
%% 判断文件的后缀名来判断
|
||||||
Ext = filename:extension(TarFile),
|
erl_tar:extract(TarFile, [compressed, {cwd, TargetDir}, verbose]).
|
||||||
case Ext of
|
|
||||||
".tar" ->
|
|
||||||
erl_tar:extract(TarFile, [{cwd, TargetDir}, verbose]);
|
|
||||||
".gz" ->
|
|
||||||
erl_tar:extract(TarFile, [compressed, {cwd, TargetDir}, verbose])
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% 下载文件
|
%% 下载文件
|
||||||
-spec download(Url :: string(), TargetDir :: string()) ->
|
-spec download(Url :: string(), TargetDir :: string()) ->
|
||||||
|
|||||||
@ -52,7 +52,7 @@ get_pid(ServiceId) when is_binary(ServiceId) ->
|
|||||||
push_config(Pid, Ref, ConfigJson) when is_pid(Pid), is_binary(ConfigJson) ->
|
push_config(Pid, Ref, ConfigJson) when is_pid(Pid), is_binary(ConfigJson) ->
|
||||||
gen_server:cast(Pid, {push_config, Ref, self(), ConfigJson}).
|
gen_server:cast(Pid, {push_config, Ref, self(), ConfigJson}).
|
||||||
|
|
||||||
-spec push_config(Pid :: pid(), Ref :: reference(), Payload :: binary()) -> no_return().
|
-spec invoke(Pid :: pid(), Ref :: reference(), Payload :: binary()) -> no_return().
|
||||||
invoke(Pid, Ref, Payload) when is_pid(Pid), is_reference(Ref), is_binary(Payload) ->
|
invoke(Pid, Ref, Payload) when is_pid(Pid), is_reference(Ref), is_binary(Payload) ->
|
||||||
gen_server:cast(Pid, {invoke, Ref, self(), Payload}).
|
gen_server:cast(Pid, {invoke, Ref, self(), Payload}).
|
||||||
|
|
||||||
@ -231,7 +231,7 @@ handle_info({'DOWN', _Ref, process, ChannelPid, Reason}, State = #state{channel_
|
|||||||
State :: #state{}) -> term()).
|
State :: #state{}) -> term()).
|
||||||
terminate(Reason, _State = #state{service_id = ServiceId, port = Port, os_pid = OSPid}) ->
|
terminate(Reason, _State = #state{service_id = ServiceId, port = Port, os_pid = OSPid}) ->
|
||||||
erlang:is_port(Port) andalso erlang:port_close(Port),
|
erlang:is_port(Port) andalso erlang:port_close(Port),
|
||||||
kill_os_pid(OSPid),
|
catch kill_os_pid(OSPid),
|
||||||
lager:debug("[efka_service] service_id: ~p, terminate with reason: ~p", [ServiceId, Reason]),
|
lager:debug("[efka_service] service_id: ~p, terminate with reason: ~p", [ServiceId, Reason]),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|||||||
@ -39,15 +39,10 @@ start_link() ->
|
|||||||
%% this function is called by the new process to find out about
|
%% this function is called by the new process to find out about
|
||||||
%% restart strategy, maximum restart frequency and child
|
%% restart strategy, maximum restart frequency and child
|
||||||
%% specifications.
|
%% specifications.
|
||||||
-spec(init(Args :: term()) ->
|
|
||||||
{ok, {SupFlags :: {RestartStrategy :: supervisor:strategy(),
|
|
||||||
MaxR :: non_neg_integer(), MaxT :: non_neg_integer()},
|
|
||||||
[ChildSpec :: supervisor:child_spec()]}}
|
|
||||||
| ignore | {error, Reason :: term()}).
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
|
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
|
||||||
%% 简化逻辑,只启动需要运行的微服务
|
%% 简化逻辑,只启动需要运行的微服务
|
||||||
Services = service_model:get_running_services(),
|
{ok, Services} = service_model:get_running_services(),
|
||||||
Specs = lists:map(fun(ServiceId) -> child_spec(ServiceId) end, Services),
|
Specs = lists:map(fun(ServiceId) -> child_spec(ServiceId) end, Services),
|
||||||
|
|
||||||
{ok, {SupFlags, Specs}}.
|
{ok, {SupFlags, Specs}}.
|
||||||
@ -70,7 +65,7 @@ start_service(ServiceId) when is_binary(ServiceId) ->
|
|||||||
-spec delete_service(ServiceId :: binary()) -> ok.
|
-spec delete_service(ServiceId :: binary()) -> ok.
|
||||||
delete_service(ServiceId) when is_binary(ServiceId) ->
|
delete_service(ServiceId) when is_binary(ServiceId) ->
|
||||||
ChildId = efka_service:get_name(ServiceId),
|
ChildId = efka_service:get_name(ServiceId),
|
||||||
ok = supervisor:terminate_child(?MODULE, ChildId),
|
supervisor:terminate_child(?MODULE, ChildId),
|
||||||
supervisor:delete_child(?MODULE, ChildId).
|
supervisor:delete_child(?MODULE, ChildId).
|
||||||
|
|
||||||
child_spec(#service{service_id = ServiceId}) when is_binary(ServiceId) ->
|
child_spec(#service{service_id = ServiceId}) when is_binary(ServiceId) ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user