diff options
| author | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-04-17 14:44:09 +0100 |
|---|---|---|
| committer | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-04-17 14:44:09 +0100 |
| commit | 5d56d9ff1082f7a3264e5d8535568dae7a544f1b (patch) | |
| tree | 6a4911883c290a62eb5f12518c36c377c83deb22 /src | |
| parent | 14573283eabd42f025ceb64b533a05b80829dbf0 (diff) | |
| download | rabbitmq-server-git-5d56d9ff1082f7a3264e5d8535568dae7a544f1b.tar.gz | |
Moved various functions that query the status of applications from rabbit_control to rabbit_misc.
Diffstat (limited to 'src')
| -rw-r--r-- | src/rabbit.erl | 5 | ||||
| -rw-r--r-- | src/rabbit_control.erl | 75 | ||||
| -rw-r--r-- | src/rabbit_misc.erl | 86 |
3 files changed, 91 insertions, 75 deletions
diff --git a/src/rabbit.erl b/src/rabbit.erl index eec7e34e8e..bee2634db8 100644 --- a/src/rabbit.erl +++ b/src/rabbit.erl @@ -347,10 +347,7 @@ status() -> is_running() -> is_running(node()). is_running(Node) -> - case rpc:call(Node, application, which_applications, [infinity]) of - {badrpc, _} -> false; - Apps -> proplists:is_defined(rabbit, Apps) - end. + rabbit_misc:is_running(Node, rabbit). environment() -> lists:keysort( diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl index 51f88c8f37..17a69725b5 100644 --- a/src/rabbit_control.erl +++ b/src/rabbit_control.erl @@ -20,7 +20,6 @@ -export([start/0, stop/0, action/5]). -define(RPC_TIMEOUT, infinity). --define(EXTERNAL_CHECK_INTERVAL, 1000). -define(QUIET_OPT, "-q"). -define(NODE_OPT, "-n"). @@ -157,8 +156,8 @@ action(stop, Node, Args, _Opts, Inform) -> Inform("Stopping and halting node ~p", [Node]), Res = call(Node, {rabbit, stop_and_halt, []}), case {Res, Args} of - {ok, [PidFile]} -> wait_for_process_death( - read_pid_file(PidFile, false)); + {ok, [PidFile]} -> rabbit_misc:wait_for_process_death( + rabbit_misc:read_pid_file(PidFile, false)); {ok, [_, _| _]} -> exit({badarg, Args}); _ -> ok end, @@ -380,75 +379,9 @@ action(eval, Node, [Expr], _Opts, _Inform) -> %%---------------------------------------------------------------------------- wait_for_application(Node, PidFile, Inform) -> - Pid = read_pid_file(PidFile, true), + Pid = rabbit_misc:read_pid_file(PidFile, true), Inform("pid is ~s", [Pid]), - wait_for_application(Node, Pid). - -wait_for_application(Node, Pid) -> - case process_up(Pid) of - true -> case rabbit:is_running(Node) of - true -> ok; - false -> timer:sleep(?EXTERNAL_CHECK_INTERVAL), - wait_for_application(Node, Pid) - end; - false -> {error, process_not_running} - end. - -wait_for_process_death(Pid) -> - case process_up(Pid) of - true -> timer:sleep(?EXTERNAL_CHECK_INTERVAL), - wait_for_process_death(Pid); - false -> ok - end. - -read_pid_file(PidFile, Wait) -> - case {file:read_file(PidFile), Wait} of - {{ok, Bin}, _} -> - S = string:strip(binary_to_list(Bin), right, $\n), - try list_to_integer(S) - catch error:badarg -> - exit({error, {garbage_in_pid_file, PidFile}}) - end, - S; - {{error, enoent}, true} -> - timer:sleep(?EXTERNAL_CHECK_INTERVAL), - read_pid_file(PidFile, Wait); - {{error, _} = E, _} -> - exit({error, {could_not_read_pid, E}}) - end. - -% Test using some OS clunkiness since we shouldn't trust -% rpc:call(os, getpid, []) at this point -process_up(Pid) -> - with_os([{unix, fun () -> - system("ps -p " ++ Pid - ++ " >/dev/null 2>&1") =:= 0 - end}, - {win32, fun () -> - Res = os:cmd("tasklist /nh /fi \"pid eq " ++ - Pid ++ "\" 2>&1"), - case re:run(Res, "erl\\.exe", [{capture, none}]) of - match -> true; - _ -> false - end - end}]). - -with_os(Handlers) -> - {OsFamily, _} = os:type(), - case proplists:get_value(OsFamily, Handlers) of - undefined -> throw({unsupported_os, OsFamily}); - Handler -> Handler() - end. - -% Like system(3) -system(Cmd) -> - ShCmd = "sh -c '" ++ escape_quotes(Cmd) ++ "'", - Port = erlang:open_port({spawn, ShCmd}, [exit_status,nouse_stdio]), - receive {Port, {exit_status, Status}} -> Status end. - -% Escape the quotes in a shell command so that it can be used in "sh -c 'cmd'" -escape_quotes(Cmd) -> - lists:flatten(lists:map(fun ($') -> "'\\''"; (Ch) -> Ch end, Cmd)). + rabbit_misc:wait_for_application(Node, Pid, rabbit). format_parse_error({_Line, Mod, Err}) -> lists:flatten(Mod:format_error(Err)). diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl index 0aacd654ce..e224bf7bae 100644 --- a/src/rabbit_misc.erl +++ b/src/rabbit_misc.erl @@ -61,6 +61,10 @@ -export([quit/1]). -export([os_cmd/1]). -export([gb_sets_difference/2]). +-export([is_running/2, wait_for_application/3, wait_for_process_death/1, + read_pid_file/2]). + +-define(EXTERNAL_CHECK_INTERVAL, 1000). %%---------------------------------------------------------------------------- @@ -206,6 +210,11 @@ -spec(quit/1 :: (integer() | string()) -> no_return()). -spec(os_cmd/1 :: (string()) -> string()). -spec(gb_sets_difference/2 :: (gb_set(), gb_set()) -> gb_set()). +-spec(is_running/2 :: (node(), atom()) -> boolean()). +-spec(wait_for_application/3 :: (node(), pid(), atom()) + -> ok | {error, process_not_running}). +-spec(wait_for_process_death/1 :: (pid()) -> ok). +-spec(read_pid_file/2 :: (file:name(), boolean()) -> pid() | no_return()). -endif. @@ -917,3 +926,80 @@ os_cmd(Command) -> gb_sets_difference(S1, S2) -> gb_sets:fold(fun gb_sets:delete_any/2, S1, S2). + +%%---------------------------------------------------------------------------- + +is_running(Node, Application) -> + case rpc:call(Node, application, which_applications, [infinity]) of + {badrpc, _} -> false; + Apps -> proplists:is_defined(Application, Apps) + end. + +wait_for_application(Node, Pid, Application) -> + case process_up(Pid) of + true -> case is_running(Node, Application) of + true -> ok; + false -> timer:sleep(?EXTERNAL_CHECK_INTERVAL), + wait_for_application(Node, Pid, Application) + end; + false -> {error, process_not_running} + end. + +wait_for_process_death(Pid) -> + case process_up(Pid) of + true -> timer:sleep(?EXTERNAL_CHECK_INTERVAL), + wait_for_process_death(Pid); + false -> ok + end. + +read_pid_file(PidFile, Wait) -> + case {file:read_file(PidFile), Wait} of + {{ok, Bin}, _} -> + S = string:strip(binary_to_list(Bin), right, $\n), + try list_to_integer(S) + catch error:badarg -> + exit({error, {garbage_in_pid_file, PidFile}}) + end, + S; + {{error, enoent}, true} -> + timer:sleep(?EXTERNAL_CHECK_INTERVAL), + read_pid_file(PidFile, Wait); + {{error, _} = E, _} -> + exit({error, {could_not_read_pid, E}}) + end. + +% Test using some OS clunkiness since we shouldn't trust +% rpc:call(os, getpid, []) at this point +process_up(Pid) -> + with_os([{unix, fun () -> + system("ps -p " ++ Pid + ++ " >/dev/null 2>&1") =:= 0 + end}, + {win32, fun () -> + Res = os:cmd("tasklist /nh /fi \"pid eq " ++ + Pid ++ "\" 2>&1"), + case re:run(Res, "erl\\.exe", [{capture, none}]) of + match -> true; + _ -> false + end + end}]). + +with_os(Handlers) -> + {OsFamily, _} = os:type(), + case proplists:get_value(OsFamily, Handlers) of + undefined -> throw({unsupported_os, OsFamily}); + Handler -> Handler() + end. + +% Like system(3) +system(Cmd) -> + ShCmd = "sh -c '" ++ escape_quotes(Cmd) ++ "'", + Port = erlang:open_port({spawn, ShCmd}, [exit_status,nouse_stdio]), + receive {Port, {exit_status, Status}} -> Status end. + +% Escape the quotes in a shell command so that it can be used in "sh -c 'cmd'" +escape_quotes(Cmd) -> + lists:flatten(lists:map(fun ($') -> "'\\''"; (Ch) -> Ch end, Cmd)). + +format_parse_error({_Line, Mod, Err}) -> + lists:flatten(Mod:format_error(Err)). |
