This commit is contained in:
anlicheng 2025-05-20 11:14:00 +08:00
parent d15efa1a96
commit 45e3e1ec78
4 changed files with 31 additions and 41 deletions

View File

@ -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}

View File

@ -119,28 +119,31 @@ do_deploy(TaskId, ServiceRootDir, ServiceId, TarUrl) when is_integer(TaskId), is
%%
WorkDir = ServiceRootDir ++ "/work_dir/",
ok = filelib:ensure_dir(WorkDir),
%%
Result = delete_directory(WorkDir),
lager:debug("[efka_inetd_task] delete_directory result is: ~p", [Result]),
case tar_extract(TarFile, WorkDir) of
case filelib:ensure_dir(WorkDir) of
ok ->
%%
ok = service_model:insert(#service{
service_id = ServiceId,
tar_url = TarUrl,
%%
root_dir = ServiceRootDir,
params = <<"">>,
metrics = <<"">>,
%% 0: , 1:
status = 0
}),
efka_inetd_task_log:stash(TaskId, <<"deploy success">>);
%%
catch delete_directory(WorkDir),
case tar_extract(TarFile, WorkDir) of
ok ->
%%
ok = service_model:insert(#service{
service_id = ServiceId,
tar_url = TarUrl,
%%
root_dir = ServiceRootDir,
params = <<"">>,
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} ->
TarLog = io_lib:format("tar decompression: ~p, error: ~p", [filename:basename(TarFile), Reason]),
efka_inetd_task_log:stash(TaskId, list_to_binary(TarLog))
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]),
@ -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()) ->

View File

@ -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.

View File

@ -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) ->