diff --git a/apps/efka/src/efka_micro_service.erl b/apps/efka/src/efka_micro_service.erl index e2d811a..04c4a13 100644 --- a/apps/efka/src/efka_micro_service.erl +++ b/apps/efka/src/efka_micro_service.erl @@ -89,29 +89,14 @@ 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 -> - %% 数据的状态和运行状态是2回事 - 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; + init0(Service, Manifest); {error, Reason} -> lager:notice("[efka_micro_service] service: ~p, check manifest.json get error: ~p", [ServiceId, Reason]), ignore @@ -121,6 +106,21 @@ init([Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0, stat ignore 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 %% @doc Handling call messages -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()}. 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;