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),
|
||||
|
||||
ServicePid = efka_service:get_pid(ServiceId),
|
||||
lager:debug("service pid is: ~p", [ServicePid]),
|
||||
|
||||
case is_pid(ServicePid) andalso efka_service:is_running(ServicePid) of
|
||||
case is_pid(ServicePid) of
|
||||
true ->
|
||||
{reply, {error, <<"the service is running, stop first">>}, State};
|
||||
false ->
|
||||
@ -121,8 +119,8 @@ handle_call({stop_service, ServiceId}, _From, State = #state{}) ->
|
||||
case efka_service:get_pid(ServiceId) of
|
||||
undefined ->
|
||||
{reply, {error, <<"service not running">>}, State};
|
||||
ServicePid ->
|
||||
efka_service_sup:delete_service(ServicePid),
|
||||
ServicePid when is_pid(ServicePid) ->
|
||||
efka_service_sup:delete_service(ServiceId),
|
||||
%% 主动停止的服务,需要更新数据库状态, 状态是为了保证下次efka重启的时候,不自动启动服务
|
||||
ok = service_model:change_status(ServiceId, 0),
|
||||
{reply, ok, State}
|
||||
|
||||
@ -119,11 +119,10 @@ do_deploy(TaskId, ServiceRootDir, ServiceId, TarUrl) when is_integer(TaskId), is
|
||||
|
||||
%% 创建工作目录
|
||||
WorkDir = ServiceRootDir ++ "/work_dir/",
|
||||
ok = filelib:ensure_dir(WorkDir),
|
||||
|
||||
case filelib:ensure_dir(WorkDir) of
|
||||
ok ->
|
||||
%% 清理目录下的文件
|
||||
Result = delete_directory(WorkDir),
|
||||
lager:debug("[efka_inetd_task] delete_directory result is: ~p", [Result]),
|
||||
catch delete_directory(WorkDir),
|
||||
case tar_extract(TarFile, WorkDir) of
|
||||
ok ->
|
||||
%% 更新数据
|
||||
@ -142,6 +141,10 @@ do_deploy(TaskId, ServiceRootDir, ServiceId, TarUrl) when is_integer(TaskId), is
|
||||
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} ->
|
||||
DownloadLog = io_lib:format("make work_dir error: ~p", [Reason]),
|
||||
efka_inetd_task_log:stash(TaskId, list_to_binary(DownloadLog))
|
||||
end;
|
||||
{error, Reason} ->
|
||||
DownloadLog = io_lib:format("download: ~p, error: ~p", [binary_to_list(TarUrl), Reason]),
|
||||
efka_inetd_task_log:stash(TaskId, list_to_binary(DownloadLog))
|
||||
@ -174,13 +177,7 @@ delete_directory(Dir) when is_list(Dir) ->
|
||||
-spec tar_extract(string(), string()) -> ok | {error, term()}.
|
||||
tar_extract(TarFile, TargetDir) when is_list(TarFile), is_list(TargetDir) ->
|
||||
%% 判断文件的后缀名来判断
|
||||
Ext = filename:extension(TarFile),
|
||||
case Ext of
|
||||
".tar" ->
|
||||
erl_tar:extract(TarFile, [{cwd, TargetDir}, verbose]);
|
||||
".gz" ->
|
||||
erl_tar:extract(TarFile, [compressed, {cwd, TargetDir}, verbose])
|
||||
end.
|
||||
erl_tar:extract(TarFile, [compressed, {cwd, TargetDir}, verbose]).
|
||||
|
||||
%% 下载文件
|
||||
-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) ->
|
||||
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) ->
|
||||
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()).
|
||||
terminate(Reason, _State = #state{service_id = ServiceId, port = Port, os_pid = OSPid}) ->
|
||||
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]),
|
||||
ok.
|
||||
|
||||
|
||||
@ -39,15 +39,10 @@ start_link() ->
|
||||
%% this function is called by the new process to find out about
|
||||
%% restart strategy, maximum restart frequency and child
|
||||
%% 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([]) ->
|
||||
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),
|
||||
|
||||
{ok, {SupFlags, Specs}}.
|
||||
@ -70,7 +65,7 @@ start_service(ServiceId) when is_binary(ServiceId) ->
|
||||
-spec delete_service(ServiceId :: binary()) -> ok.
|
||||
delete_service(ServiceId) when is_binary(ServiceId) ->
|
||||
ChildId = efka_service:get_name(ServiceId),
|
||||
ok = supervisor:terminate_child(?MODULE, ChildId),
|
||||
supervisor:terminate_child(?MODULE, ChildId),
|
||||
supervisor:delete_child(?MODULE, ChildId).
|
||||
|
||||
child_spec(#service{service_id = ServiceId}) when is_binary(ServiceId) ->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user