fix micro service

This commit is contained in:
anlicheng 2025-05-05 22:37:53 +08:00
parent c1ef1accec
commit b148b60561

View File

@ -89,16 +89,25 @@ start_link(Name, Service = #micro_service{}) when is_atom(Name) ->
-spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0, status = Status}]) ->
init([Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0}]) ->
WorkDir = binary_to_list(WorkDir0),
case file:read_file(WorkDir ++ "manifest.json") of
{ok, ManifestInfo} ->
Manifest = jiffy:decode(ManifestInfo, [return_maps]),
Manifest = catch jiffy:decode(ManifestInfo, [return_maps]),
case check_manifest(Manifest) of
ok ->
init0(Service, Manifest);
{error, Reason} ->
lager:notice("[efka_micro_service] service: ~p, check manifest.json get error: ~p", [ServiceId, Reason]),
ignore
end;
{error, Reason} ->
lager:notice("[efka_micro_service] service: ~p, read manifest.json get error: ~p", [ServiceId, Reason]),
ignore
end.
init0(Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0, status = 1}, Manifest) ->
%% 2
case Status == 1 of
true ->
case boot_service(WorkDir0, Manifest) of
{ok, Port} ->
{os_pid, OSPid} = erlang:port_info(Port, os_pid),
@ -108,18 +117,9 @@ init([Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0, stat
lager:debug("[efka_micro_service] service: ~p, boot_service get error: ~p", [ServiceId, Reason]),
{ok, #state{service = Service, manifest = Manifest, running_status = ?STATUS_STOPPED, port = undefined, os_pid = undefined}}
end;
false ->
init0(Service, Manifest) ->
lager:debug("[efka_micro_service] service: ~p current status is 0, not boot"),
{ok, #state{service = Service, manifest = Manifest, running_status = ?STATUS_STOPPED, port = undefined, os_pid = undefined}}
end;
{error, Reason} ->
lager:notice("[efka_micro_service] service: ~p, check manifest.json get error: ~p", [ServiceId, Reason]),
ignore
end;
{error, Reason} ->
lager:notice("[efka_micro_service] service: ~p, read manifest.json get error: ~p", [ServiceId, Reason]),
ignore
end.
{ok, #state{service = Service, manifest = Manifest, running_status = ?STATUS_STOPPED, port = undefined, os_pid = undefined}}.
%% @private
%% @doc Handling call messages
@ -251,7 +251,9 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
-spec check_manifest(Manifest :: map()) -> ok | {error, Reason :: binary()}.
check_manifest(Manifest) when is_map(Manifest) ->
RequiredKeys = [<<"serivce_id">>, <<"exec">>, <<"args">>, <<"health_check">>],
check_manifest0(RequiredKeys, Manifest).
check_manifest0(RequiredKeys, Manifest);
check_manifest(_Manifest) ->
{error, <<"invalid manifest json">>}.
check_manifest0([], _Manifest) ->
ok;