fix service

This commit is contained in:
anlicheng 2025-05-20 00:11:21 +08:00
parent cefc473309
commit 3c33be6ab5
2 changed files with 13 additions and 13 deletions

View File

@ -26,7 +26,7 @@
-spec new(ServiceRootDir :: string()) -> {ok, #manifest{}} | {error, Reason :: binary()}. -spec new(ServiceRootDir :: string()) -> {ok, #manifest{}} | {error, Reason :: binary()}.
new(ServiceRootDir) when is_list(ServiceRootDir) -> new(ServiceRootDir) when is_list(ServiceRootDir) ->
WorkDir = ServiceRootDir + "/work_dir/", WorkDir = ServiceRootDir ++ "/work_dir/",
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

@ -126,7 +126,7 @@ init0(#service{service_id = ServiceId, status = 1}, Manifest) ->
{ok, #state{service_id = ServiceId, manifest = Manifest, running_status = ?STATUS_STOPPED, port = undefined, os_pid = undefined}} {ok, #state{service_id = ServiceId, manifest = Manifest, running_status = ?STATUS_STOPPED, port = undefined, os_pid = undefined}}
end; end;
init0(#service{service_id = ServiceId, status = 0}, Manifest) -> init0(#service{service_id = ServiceId, status = 0}, Manifest) ->
lager:debug("[efka_service] service: ~p current status is 0, not boot"), lager:debug("[efka_service] service: ~p current status is 0, not boot", [ServiceId]),
{ok, #state{service_id = ServiceId, manifest = Manifest, running_status = ?STATUS_STOPPED, port = undefined, os_pid = undefined}}. {ok, #state{service_id = ServiceId, manifest = Manifest, running_status = ?STATUS_STOPPED, port = undefined, os_pid = undefined}}.
%% @private %% @private
@ -169,7 +169,7 @@ handle_call(start_service, _From, State = #state{running_status = ?STATUS_STOPPE
case efka_manifest:startup(Manifest) of case efka_manifest:startup(Manifest) of
{ok, Port} -> {ok, Port} ->
{os_pid, OSPid} = erlang:port_info(Port, os_pid), {os_pid, OSPid} = erlang:port_info(Port, os_pid),
lager:debug("start_service port: ~p, os_pid: ~p", [Port, OSPid]), lager:debug("[efka_service] service_id: ~p, start_service port: ~p, os_pid: ~p", [ServiceId, Port, OSPid]),
%% %%
ok = service_model:change_status(ServiceId, 1), ok = service_model:change_status(ServiceId, 1),
{reply, ok, State#state{running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid}}; {reply, ok, State#state{running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid}};
@ -179,8 +179,8 @@ handle_call(start_service, _From, State = #state{running_status = ?STATUS_STOPPE
end; end;
%% , status字段 %% , status字段
handle_call(stop_service, _From, State = #state{running_status = ?STATUS_STOPPED, port = Port, os_pid = OSPid}) -> handle_call(stop_service, _From, State = #state{running_status = ?STATUS_STOPPED, service_id = ServiceId, port = Port, os_pid = OSPid}) ->
lager:debug("stop service port: ~p, os_pid: ~p", [Port, OSPid]), lager:debug("[efka_service] service_id: ~p, stop service port: ~p, os_pid: ~p", [ServiceId, Port, OSPid]),
{reply, {error, <<"service not running">>}, State}; {reply, {error, <<"service not running">>}, State};
handle_call(stop_service, _From, State = #state{running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid, service_id = ServiceId}) when is_port(Port) -> handle_call(stop_service, _From, State = #state{running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid, service_id = ServiceId}) when is_port(Port) ->
@ -188,7 +188,7 @@ handle_call(stop_service, _From, State = #state{running_status = ?STATUS_RUNNING
kill_os_pid(OSPid), kill_os_pid(OSPid),
erlang:is_port(Port) andalso erlang:port_close(Port), erlang:is_port(Port) andalso erlang:port_close(Port),
lager:debug("port: ~p, os_pid: ~p, will closed", [Port, OSPid]), lager:debug("[efka_service] service_id: ~p, port: ~p, os_pid: ~p, will closed", [ServiceId, Port, OSPid]),
ok = service_model:change_status(ServiceId, 0), ok = service_model:change_status(ServiceId, 0),
{reply, ok, State#state{port = undefined, os_pid = undefined, running_status = ?STATUS_STOPPED}}; {reply, ok, State#state{port = undefined, os_pid = undefined, running_status = ?STATUS_STOPPED}};
@ -251,10 +251,10 @@ handle_info({timeout, _, reboot_service}, State = #state{service_id = ServiceId,
case efka_manifest:startup(Manifest) of case efka_manifest:startup(Manifest) of
{ok, Port} -> {ok, Port} ->
{os_pid, OSPid} = erlang:port_info(Port, os_pid), {os_pid, OSPid} = erlang:port_info(Port, os_pid),
lager:debug("[efka_service] reboot service success: ~p, port: ~p, os_pid: ~p", [ServiceId, Port, OSPid]), lager:debug("[efka_service] service_id: ~p, reboot success: ~p, port: ~p, os_pid: ~p", [ServiceId, Port, OSPid]),
{noreply, State#state{running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid}}; {noreply, State#state{running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid}};
{error, Reason} -> {error, Reason} ->
lager:debug("[efka_service] service: ~p, boot_service get error: ~p", [ServiceId, Reason]), lager:debug("[efka_service] service_id: ~p, boot_service get error: ~p", [ServiceId, Reason]),
{noreply, State#state{running_status = ?STATUS_STOPPED}} {noreply, State#state{running_status = ?STATUS_STOPPED}}
end end
end; end;
@ -281,7 +281,7 @@ handle_info({Port, {exit_status, Code}}, State = #state{service_id = ServiceId})
%% channel进程的退出 %% channel进程的退出
handle_info({'DOWN', _Ref, process, ChannelPid, Reason}, State = #state{channel_pid = ChannelPid, service_id = ServiceId}) -> handle_info({'DOWN', _Ref, process, ChannelPid, Reason}, State = #state{channel_pid = ChannelPid, service_id = ServiceId}) ->
lager:debug("[efka_service] service: ~p, channel exited: ~p", [ServiceId, Reason]), lager:debug("[efka_service] service_id: ~p, channel exited: ~p", [ServiceId, Reason]),
{noreply, State#state{channel_pid = undefined, inflight = #{}}}. {noreply, State#state{channel_pid = undefined, inflight = #{}}}.
%% @private %% @private
@ -291,12 +291,12 @@ handle_info({'DOWN', _Ref, process, ChannelPid, Reason}, State = #state{channel_
%% with Reason. The return value is ignored. %% with Reason. The return value is ignored.
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), -spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #state{}) -> term()). State :: #state{}) -> term()).
terminate(Reason, _State = #state{os_pid = OSPid}) -> terminate(Reason, _State = #state{service_id = ServiceId, os_pid = OSPid}) ->
lager:debug("[efka_service] terminate with reason: ~p", [Reason]), lager:debug("[efka_service] service_id: ~p, terminate with reason: ~p", [ServiceId, Reason]),
kill_os_pid(OSPid), kill_os_pid(OSPid),
ok; ok;
terminate(Reason, _State) -> terminate(Reason, #state{service_id = ServiceId}) ->
lager:debug("[efka_service] terminate with reason: ~p", [Reason]), lager:debug("[efka_service] service_id: ~p, terminate with reason: ~p", [ServiceId, Reason]),
ok. ok.
%% @private %% @private