fix micro service
This commit is contained in:
parent
c1ef1accec
commit
b148b60561
@ -89,29 +89,14 @@ start_link(Name, Service = #micro_service{}) when is_atom(Name) ->
|
|||||||
-spec(init(Args :: term()) ->
|
-spec(init(Args :: term()) ->
|
||||||
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term()} | ignore).
|
{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),
|
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} ->
|
||||||
Manifest = jiffy:decode(ManifestInfo, [return_maps]),
|
Manifest = catch jiffy:decode(ManifestInfo, [return_maps]),
|
||||||
case check_manifest(Manifest) of
|
case check_manifest(Manifest) of
|
||||||
ok ->
|
ok ->
|
||||||
%% 数据的状态和运行状态是2回事
|
init0(Service, Manifest);
|
||||||
case Status == 1 of
|
|
||||||
true ->
|
|
||||||
case boot_service(WorkDir0, Manifest) of
|
|
||||||
{ok, Port} ->
|
|
||||||
{os_pid, OSPid} = erlang:port_info(Port, os_pid),
|
|
||||||
lager:debug("[efka_micro_service] service: ~p, port: ~p, boot_service success os_pid: ~p", [ServiceId, Port, OSPid]),
|
|
||||||
{ok, #state{service = Service, manifest = Manifest, running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid}};
|
|
||||||
{error, Reason} ->
|
|
||||||
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 ->
|
|
||||||
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} ->
|
{error, Reason} ->
|
||||||
lager:notice("[efka_micro_service] service: ~p, check manifest.json get error: ~p", [ServiceId, Reason]),
|
lager:notice("[efka_micro_service] service: ~p, check manifest.json get error: ~p", [ServiceId, Reason]),
|
||||||
ignore
|
ignore
|
||||||
@ -121,6 +106,21 @@ init([Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0, stat
|
|||||||
ignore
|
ignore
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
init0(Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0, status = 1}, Manifest) ->
|
||||||
|
%% 数据的状态和运行状态是2回事
|
||||||
|
case boot_service(WorkDir0, Manifest) of
|
||||||
|
{ok, Port} ->
|
||||||
|
{os_pid, OSPid} = erlang:port_info(Port, os_pid),
|
||||||
|
lager:debug("[efka_micro_service] service: ~p, port: ~p, boot_service success os_pid: ~p", [ServiceId, Port, OSPid]),
|
||||||
|
{ok, #state{service = Service, manifest = Manifest, running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid}};
|
||||||
|
{error, Reason} ->
|
||||||
|
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;
|
||||||
|
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}}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Handling call messages
|
%% @doc Handling call messages
|
||||||
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
||||||
@ -251,7 +251,9 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
|||||||
-spec check_manifest(Manifest :: map()) -> ok | {error, Reason :: binary()}.
|
-spec check_manifest(Manifest :: map()) -> ok | {error, Reason :: binary()}.
|
||||||
check_manifest(Manifest) when is_map(Manifest) ->
|
check_manifest(Manifest) when is_map(Manifest) ->
|
||||||
RequiredKeys = [<<"serivce_id">>, <<"exec">>, <<"args">>, <<"health_check">>],
|
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) ->
|
check_manifest0([], _Manifest) ->
|
||||||
ok;
|
ok;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user