This commit is contained in:
anlicheng 2025-04-30 15:40:19 +08:00
parent 32868d3444
commit 0bb521f6a0

View File

@ -42,6 +42,7 @@
test() -> test() ->
Pid = get_pid(<<"test1234">>), Pid = get_pid(<<"test1234">>),
stop_service(Pid),
start_service(Pid). start_service(Pid).
-spec get_name(ServiceId :: binary()) -> atom(). -spec get_name(ServiceId :: binary()) -> atom().
@ -133,15 +134,13 @@ handle_call(start_service, _From, State = #state{running_status = ?STATUS_STOPPE
end; end;
%% , status字段 %% , status字段
handle_call(stop_service, _From, State = #state{running_status = RunningStatus, port = Port, service = Service = #micro_service{service_id = ServiceId}}) -> handle_call(stop_service, _From, State = #state{running_status = ?STATUS_STOPPED}) ->
case RunningStatus of
?STATUS_STOPPED ->
{reply, {error, <<"service not running">>}, State}; {reply, {error, <<"service not running">>}, State};
?STATUS_RUNNING ->
ok = micro_service_model:stop_service(ServiceId), handle_call(stop_service, _From, State = #state{running_status = ?STATUS_RUNNING, port = Port, service = Service = #micro_service{service_id = ServiceId}}) when is_port(Port) ->
micro_service_model:stop_service(ServiceId),
kill_os_pid(Port), kill_os_pid(Port),
{reply, ok, State#state{port = undefined, service = Service#micro_service{status = 0}}} {reply, ok, State#state{port = undefined, running_status = ?STATUS_STOPPED, service = Service#micro_service{status = 0}}};
end;
handle_call(_Request, _From, State = #state{}) -> handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}. {reply, ok, State}.
@ -180,7 +179,7 @@ handle_info({Port, {data, Data}}, State = #state{port = Port, service = Service}
%% port的消息, Port的被动关闭会触发Port和State.port的值是相等的 %% port的消息, Port的被动关闭会触发Port和State.port的值是相等的
handle_info({Port, {exit_status, Code}}, State = #state{port = Port, service = Service}) -> handle_info({Port, {exit_status, Code}}, State = #state{port = Port, service = Service}) ->
lager:debug("[efka_micro_service] service_id: ~p, port exit with code: ~p", [Service#micro_service.service_id, Code]), lager:debug("[efka_micro_service] service_id: ~p, port exit with code: ~p", [Service#micro_service.service_id, Code]),
erlang:start_timer(5000, self(), reboot_service), % erlang:start_timer(5000, self(), reboot_service),
{noreply, State#state{port = undefined}}; {noreply, State#state{port = undefined}};