fix micro service
This commit is contained in:
parent
8d869f5bc0
commit
c1ef1accec
@ -94,14 +94,12 @@ init([Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0, stat
|
|||||||
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 = jiffy:decode(ManifestInfo, [return_maps]),
|
||||||
lager:debug("main: ~p", [Manifest]),
|
|
||||||
case check_manifest(Manifest) of
|
case check_manifest(Manifest) of
|
||||||
ok ->
|
ok ->
|
||||||
%% 数据的状态和运行状态是2回事
|
%% 数据的状态和运行状态是2回事
|
||||||
case Status == 1 of
|
case Status == 1 of
|
||||||
true ->
|
true ->
|
||||||
StartCmd = maps:get(<<"start">>, Manifest),
|
case boot_service(WorkDir0, Manifest) of
|
||||||
case boot_service(WorkDir0, StartCmd) 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_micro_service] service: ~p, port: ~p, boot_service success os_pid: ~p", [ServiceId, Port, OSPid]),
|
lager:debug("[efka_micro_service] service: ~p, port: ~p, boot_service success os_pid: ~p", [ServiceId, Port, OSPid]),
|
||||||
@ -150,8 +148,7 @@ handle_call(start_service, _From, State = #state{running_status = ?STATUS_RUNNIN
|
|||||||
{reply, {error, <<"service is running">>}, State};
|
{reply, {error, <<"service is running">>}, State};
|
||||||
handle_call(start_service, _From, State = #state{running_status = ?STATUS_STOPPED, manifest = Manifest, service = Service = #micro_service{work_dir = WorkDir0, service_id = ServiceId}}) ->
|
handle_call(start_service, _From, State = #state{running_status = ?STATUS_STOPPED, manifest = Manifest, service = Service = #micro_service{work_dir = WorkDir0, service_id = ServiceId}}) ->
|
||||||
%% 异步启动服务
|
%% 异步启动服务
|
||||||
StartCmd = maps:get(<<"start">>, Manifest),
|
case boot_service(WorkDir0, Manifest) of
|
||||||
case boot_service(WorkDir0, StartCmd) of
|
|
||||||
{ok, Port} ->
|
{ok, Port} ->
|
||||||
%% 更新数据库状态
|
%% 更新数据库状态
|
||||||
micro_service_model:start_service(ServiceId),
|
micro_service_model:start_service(ServiceId),
|
||||||
@ -168,20 +165,12 @@ handle_call(stop_service, _From, State = #state{running_status = ?STATUS_STOPPED
|
|||||||
lager:debug("stop service port: ~p, os_pid: ~p", [Port, OSPid]),
|
lager:debug("stop service port: ~p, os_pid: ~p", [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, manifest = Manifest,
|
handle_call(stop_service, _From, State = #state{running_status = ?STATUS_RUNNING, port = Port, os_pid = OSPid, service = Service = #micro_service{service_id = ServiceId}}) when is_port(Port) ->
|
||||||
service = Service = #micro_service{service_id = ServiceId, work_dir = WorkDir0}}) when is_port(Port) ->
|
|
||||||
|
|
||||||
%% 优先使用微服务提供的stop指令, 没有提供的情况下,使用kill指令
|
%% 优先使用微服务提供的stop指令, 没有提供的情况下,使用kill指令
|
||||||
case maps:find(<<"stop">>, Manifest) of
|
kill_os_pid(OSPid),
|
||||||
error ->
|
|
||||||
kill_os_pid(OSPid);
|
|
||||||
{ok, StopCmd} ->
|
|
||||||
kill_service(WorkDir0, StopCmd, OSPid)
|
|
||||||
end,
|
|
||||||
|
|
||||||
micro_service_model:stop_service(ServiceId),
|
micro_service_model:stop_service(ServiceId),
|
||||||
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("port: ~p, os_pid: ~p, will closed", [Port, OSPid]),
|
||||||
|
|
||||||
{reply, ok, State#state{port = undefined, os_pid = undefined, running_status = ?STATUS_STOPPED, service = Service#micro_service{status = 0}}};
|
{reply, ok, State#state{port = undefined, os_pid = undefined, running_status = ?STATUS_STOPPED, service = Service#micro_service{status = 0}}};
|
||||||
@ -209,8 +198,7 @@ handle_info({timeout, _, reboot_service}, State = #state{service = #micro_servic
|
|||||||
lager:debug("[efka_micro_service] service_id: ~p, is stopped, ignore boot_service", [ServiceId]),
|
lager:debug("[efka_micro_service] service_id: ~p, is stopped, ignore boot_service", [ServiceId]),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
handle_info({timeout, _, reboot_service}, State = #state{manifest = Manifest, service = #micro_service{work_dir = WorkDir0, service_id = ServiceId, status = 1}}) ->
|
handle_info({timeout, _, reboot_service}, State = #state{manifest = Manifest, service = #micro_service{work_dir = WorkDir0, service_id = ServiceId, status = 1}}) ->
|
||||||
StartCmd = maps:get(<<"start">>, Manifest),
|
case boot_service(WorkDir0, Manifest) of
|
||||||
case boot_service(WorkDir0, StartCmd) 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_micro_service] reboot service success: ~p, port: ~p, os_pid: ~p", [ServiceId, Port, OSPid]),
|
lager:debug("[efka_micro_service] reboot service success: ~p, port: ~p, os_pid: ~p", [ServiceId, Port, OSPid]),
|
||||||
@ -262,7 +250,7 @@ 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">>, <<"start">>, <<"health_check">>, <<"stop">>],
|
RequiredKeys = [<<"serivce_id">>, <<"exec">>, <<"args">>, <<"health_check">>],
|
||||||
check_manifest0(RequiredKeys, Manifest).
|
check_manifest0(RequiredKeys, Manifest).
|
||||||
|
|
||||||
check_manifest0([], _Manifest) ->
|
check_manifest0([], _Manifest) ->
|
||||||
@ -290,8 +278,8 @@ check_manifest0([<<"health_check">>|T], Manifest) ->
|
|||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
{error, <<"health_check is not string">>}
|
{error, <<"health_check is not string">>}
|
||||||
end;
|
end;
|
||||||
check_manifest0([<<"start">>|T], Manifest) ->
|
check_manifest0([<<"exec">>|T], Manifest) ->
|
||||||
case maps:find(<<"start">>, Manifest) of
|
case maps:find(<<"exec">>, Manifest) of
|
||||||
error ->
|
error ->
|
||||||
{error, <<"miss start">>};
|
{error, <<"miss start">>};
|
||||||
{ok, Cmd} when is_binary(Cmd) ->
|
{ok, Cmd} when is_binary(Cmd) ->
|
||||||
@ -301,42 +289,17 @@ check_manifest0([<<"start">>|T], Manifest) ->
|
|||||||
check_manifest0(T, Manifest);
|
check_manifest0(T, Manifest);
|
||||||
_ ->
|
_ ->
|
||||||
{error, <<"start cmd cannot contain args">>}
|
{error, <<"start cmd cannot contain args">>}
|
||||||
end;
|
end
|
||||||
%% 对参数项目不进行检查
|
|
||||||
{ok, [Cmd|_Args]} when is_binary(Cmd) ->
|
|
||||||
%% 不能包含空格
|
|
||||||
case binary:match(Cmd, <<" ">>) of
|
|
||||||
nomatch ->
|
|
||||||
check_manifest0(T, Manifest);
|
|
||||||
_ ->
|
|
||||||
{error, <<"start cmd cannot contain args">>}
|
|
||||||
end;
|
|
||||||
{ok, _} ->
|
|
||||||
{error, <<"start is not string">>}
|
|
||||||
end;
|
end;
|
||||||
check_manifest0([<<"stop">>|T], Manifest) ->
|
check_manifest0([<<"args">>|T], Manifest) ->
|
||||||
case maps:find(<<"stop">>, Manifest) of
|
case maps:find(<<"args">>, Manifest) of
|
||||||
error ->
|
error ->
|
||||||
check_manifest0(T, Manifest);
|
check_manifest0(T, Manifest);
|
||||||
{ok, Cmd} when is_binary(Cmd) ->
|
|
||||||
%% 不能包含空格
|
|
||||||
case binary:match(Cmd, <<" ">>) of
|
|
||||||
nomatch ->
|
|
||||||
check_manifest0(T, Manifest);
|
|
||||||
_ ->
|
|
||||||
{error, <<"stop cmd cannot contain args">>}
|
|
||||||
end;
|
|
||||||
%% 对参数项目不进行检查
|
%% 对参数项目不进行检查
|
||||||
{ok, [Cmd|_Args]} when is_binary(Cmd) ->
|
{ok, Args} when is_list(Args) ->
|
||||||
%% 不能包含空格
|
check_manifest0(T, Manifest);
|
||||||
case binary:match(Cmd, <<" ">>) of
|
|
||||||
nomatch ->
|
|
||||||
check_manifest0(T, Manifest);
|
|
||||||
_ ->
|
|
||||||
{error, <<"stop cmd cannot contain args">>}
|
|
||||||
end;
|
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
{error, <<"stop is not string">>}
|
{error, <<"args must be list">>}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% 关闭系统进程
|
%% 关闭系统进程
|
||||||
@ -348,24 +311,21 @@ kill_os_pid(OSPid) when is_integer(OSPid) ->
|
|||||||
lager:debug("kill cmd is: ~p", [Cmd]),
|
lager:debug("kill cmd is: ~p", [Cmd]),
|
||||||
os:cmd(Cmd).
|
os:cmd(Cmd).
|
||||||
|
|
||||||
%% 执行命令
|
|
||||||
-spec kill_service(WorkDir0 :: binary(), ExecCmd0 :: binary() | [binary()], OSPid :: integer()) -> no_return().
|
|
||||||
kill_service(WorkDir0, ExecCmd0, OSPid) when is_binary(WorkDir0) ->
|
|
||||||
{RealExecCmd, PortSettings} = make_cmd(WorkDir0, ExecCmd0),
|
|
||||||
Port = erlang:open_port({spawn_executable, RealExecCmd}, PortSettings),
|
|
||||||
receive
|
|
||||||
{Port, {exit_status, Code}} ->
|
|
||||||
lager:debug("[service] exit with code: ~p", [Code])
|
|
||||||
after 5000 ->
|
|
||||||
erlang:port_close(Port),
|
|
||||||
kill_os_pid(OSPid)
|
|
||||||
end,
|
|
||||||
is_port(Port) andalso erlang:port_close(Port).
|
|
||||||
|
|
||||||
%% 启动微服务
|
%% 启动微服务
|
||||||
-spec boot_service(WorkDir :: binary(), ExecCmd :: binary() | [binary()]) -> {ok, Port :: port()} | {error, Reason :: binary()}.
|
-spec boot_service(WorkDir :: binary(), Manifest :: map()) -> {ok, Port :: port()} | {error, Reason :: binary()}.
|
||||||
boot_service(WorkDir0, ExecCmd0) when is_binary(WorkDir0), is_binary(ExecCmd0); is_list(ExecCmd0) ->
|
boot_service(WorkDir0, Manifest) when is_binary(WorkDir0), is_map(Manifest) ->
|
||||||
{RealExecCmd, PortSettings} = make_cmd(WorkDir0, ExecCmd0),
|
ExecCmd0 = maps:get(<<"exec">>, Manifest),
|
||||||
|
Args0 = maps:get(<<"args">>, Manifest, []),
|
||||||
|
|
||||||
|
WorkDir = binary_to_list(WorkDir0),
|
||||||
|
PortSettings = [
|
||||||
|
{cd, WorkDir},
|
||||||
|
{args, [binary_to_list(A) || A <- Args0]},
|
||||||
|
exit_status
|
||||||
|
],
|
||||||
|
ExecCmd = binary_to_list(ExecCmd0),
|
||||||
|
RealExecCmd = filename:absname_join(WorkDir, ExecCmd),
|
||||||
|
|
||||||
Port = erlang:open_port({spawn_executable, RealExecCmd}, PortSettings),
|
Port = erlang:open_port({spawn_executable, RealExecCmd}, PortSettings),
|
||||||
{ok, Port}.
|
{ok, Port}.
|
||||||
|
|
||||||
@ -377,24 +337,3 @@ is_url(Input) when is_binary(Input) ->
|
|||||||
catch
|
catch
|
||||||
_:_ -> false
|
_:_ -> false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec make_cmd(WorkDir0 :: binary(), Cmd0 :: binary() | [binary()]) -> {RealCmd :: string(), PortSettings :: list()}.
|
|
||||||
make_cmd(WorkDir0, Cmd0) when is_binary(Cmd0) ->
|
|
||||||
WorkDir = binary_to_list(WorkDir0),
|
|
||||||
PortSettings = [
|
|
||||||
{cd, WorkDir},
|
|
||||||
exit_status
|
|
||||||
],
|
|
||||||
ExecCmd = binary_to_list(Cmd0),
|
|
||||||
RealExecCmd = filename:absname_join(WorkDir, ExecCmd),
|
|
||||||
{RealExecCmd, PortSettings};
|
|
||||||
make_cmd(WorkDir0, [Cmd0|Args]) when is_binary(Cmd0) ->
|
|
||||||
WorkDir = binary_to_list(WorkDir0),
|
|
||||||
PortSettings = [
|
|
||||||
{cd, WorkDir},
|
|
||||||
{args, [binary_to_list(A) || A <- Args]},
|
|
||||||
exit_status
|
|
||||||
],
|
|
||||||
ExecCmd = binary_to_list(Cmd0),
|
|
||||||
RealExecCmd = filename:absname_join(WorkDir, ExecCmd),
|
|
||||||
{RealExecCmd, PortSettings}.
|
|
||||||
Loading…
x
Reference in New Issue
Block a user