This commit is contained in:
anlicheng 2025-05-19 23:52:34 +08:00
parent c6e1056f44
commit 040198e8d5
6 changed files with 29 additions and 27 deletions

View File

@ -13,7 +13,7 @@
service_id :: binary(), service_id :: binary(),
tar_url :: binary(), tar_url :: binary(),
%% %%
root_dir :: binary(), root_dir :: string(),
params :: binary(), params :: binary(),
metrics :: binary(), metrics :: binary(),
%% 0: , 1: %% 0: , 1:

View File

@ -71,16 +71,23 @@ init([]) ->
handle_call({deploy, TaskId, ServiceId, TarUrl}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) -> handle_call({deploy, TaskId, ServiceId, TarUrl}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) ->
%% %%
{ok, ServiceRootDir} = ensure_dirs(RootDir, ServiceId), {ok, ServiceRootDir} = ensure_dirs(RootDir, ServiceId),
case check_download_url(TarUrl) of
ok ->
{ok, TaskPid} = efka_inetd_task:start_link(TaskId, ServiceRootDir, ServiceId, TarUrl),
efka_inetd_task:deploy(TaskPid),
lager:debug("[efka_inetd] start task_id: ~p, tar_url: ~p", [TaskId, TarUrl]),
{reply, ok, State#state{task_map = maps:put(TaskPid, {TaskId, ServiceId}, TaskMap)}}; ServicePid = efka_service:get_pid(ServiceId),
{error, Reason} -> case is_pid(ServicePid) andalso efka_service:is_running(ServicePid) of
lager:debug("[efka_inetd] check_download_url: ~p, get error: ~p", [TarUrl, Reason]), true ->
{reply, {error, <<"download url error">>}, State} {reply, {error, <<"the service is running, stop first">>}, State};
false ->
case check_download_url(TarUrl) of
ok ->
{ok, TaskPid} = efka_inetd_task:start_link(TaskId, ServiceRootDir, ServiceId, TarUrl),
efka_inetd_task:deploy(TaskPid),
lager:debug("[efka_inetd] start task_id: ~p, tar_url: ~p", [TaskId, TarUrl]),
{reply, ok, State#state{task_map = maps:put(TaskPid, {TaskId, ServiceId}, TaskMap)}};
{error, Reason} ->
lager:debug("[efka_inetd] check_download_url: ~p, get error: ~p", [TarUrl, Reason]),
{reply, {error, <<"download url error">>}, State}
end
end; end;
handle_call(_Request, _From, State = #state{}) -> handle_call(_Request, _From, State = #state{}) ->

View File

@ -24,9 +24,8 @@
%% API %% API
-export([new/1, startup/1]). -export([new/1, startup/1]).
-spec new(WorkDir0 :: binary()) -> {ok, #manifest{}} | {error, Reason :: binary()}. -spec new(WorkDir :: string()) -> {ok, #manifest{}} | {error, Reason :: binary()}.
new(WorkDir0) when is_binary(WorkDir0) -> new(WorkDir) when is_list(WorkDir) ->
WorkDir = binary_to_list(WorkDir0),
case file:read_file(WorkDir ++ "manifest.json") of case file:read_file(WorkDir ++ "manifest.json") of
{ok, ManifestInfo} -> {ok, ManifestInfo} ->
Settings = catch jiffy:decode(ManifestInfo, [return_maps]), Settings = catch jiffy:decode(ManifestInfo, [return_maps]),

View File

@ -20,7 +20,7 @@
%% API %% API
-export([start_link/2]). -export([start_link/2]).
-export([get_name/1, get_pid/1, start_service/1, stop_service/1, attach_channel/2]). -export([get_name/1, get_pid/1, start_service/1, stop_service/1, attach_channel/2]).
-export([push_config/3, request_config/1, invoke/3]). -export([push_config/3, request_config/1, invoke/3, is_running/1]).
-export([metric_data/3, send_event/3]). -export([metric_data/3, send_event/3]).
%% gen_server callbacks %% gen_server callbacks
@ -59,6 +59,10 @@ push_config(Pid, Ref, ConfigJson) when is_pid(Pid), is_binary(ConfigJson) ->
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}).
-spec is_running(Pid :: pid()) -> boolean().
is_running(Pid) when is_pid(Pid) ->
gen_server:call(Pid, is_running).
request_config(Pid) when is_pid(Pid) -> request_config(Pid) when is_pid(Pid) ->
gen_server:call(Pid, request_config). gen_server:call(Pid, request_config).
@ -148,6 +152,10 @@ handle_call({attach_channel, ChannelPid}, _From, State = #state{channel_pid = Ol
{reply, {error, <<"serivce stopped">>}, State} {reply, {error, <<"serivce stopped">>}, State}
end; end;
%%
handle_call(is_running, _From, State = #state{running_status = RunningStatus}) ->
{reply, RunningStatus, State};
%% done %% done
handle_call(request_config, _From, State = #state{service_id = ServiceId, running_status = ?STATUS_RUNNING}) -> handle_call(request_config, _From, State = #state{service_id = ServiceId, running_status = ?STATUS_RUNNING}) ->
Params = service_model:get_params(ServiceId), Params = service_model:get_params(ServiceId),

View File

@ -46,18 +46,6 @@ start_link() ->
| ignore | {error, Reason :: term()}). | ignore | {error, Reason :: term()}).
init([]) -> init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
service_model:insert(#service{
service_id = <<"test1234">>,
tar_url = <<"http://118.178.229.213:3000/anlicheng/ekfa/archive1/main.tar.gz">>,
%%
root_dir = <<"/usr/local/code/tmp/test/">>,
params = <<"">>,
metrics = <<"">>,
%% 0: , 1:
status = 1
}),
MicroServiceIds = service_model:get_all_service_ids(), MicroServiceIds = service_model:get_all_service_ids(),
Specs = lists:map(fun(ServiceId) -> child_spec(ServiceId) end, MicroServiceIds), Specs = lists:map(fun(ServiceId) -> child_spec(ServiceId) end, MicroServiceIds),

View File

@ -1,6 +1,6 @@
[ [
{efka, [ {efka, [
{root_dir, "/usr/local/code/efka/"}, {root_dir, "/usr/local/code/efka"},
{tcp_server, [ {tcp_server, [
{port, 18088} {port, 18088}