diff options
| author | Simon MacMullen <simon@rabbitmq.com> | 2015-01-30 16:53:02 +0000 |
|---|---|---|
| committer | Simon MacMullen <simon@rabbitmq.com> | 2015-01-30 16:53:02 +0000 |
| commit | c364864a29592215ca331ffd7db19b660ddeeccf (patch) | |
| tree | 4e41a5ca6da72378922180d23ac8e300d23ed2fa | |
| parent | dd20291501fbd476b88b8d8760553659956469c1 (diff) | |
| download | rabbitmq-server-git-c364864a29592215ca331ffd7db19b660ddeeccf.tar.gz | |
Introduce rabbit_misc:is_os_process_alive/1 from a private function in rabbit_control_main, as we will want it for testing.
| -rw-r--r-- | src/rabbit_control_main.erl | 38 | ||||
| -rw-r--r-- | src/rabbit_misc.erl | 34 |
2 files changed, 36 insertions, 36 deletions
diff --git a/src/rabbit_control_main.erl b/src/rabbit_control_main.erl index 751dbd298d..810f5f6f33 100644 --- a/src/rabbit_control_main.erl +++ b/src/rabbit_control_main.erl @@ -525,7 +525,7 @@ wait_for_startup(Node, Pid) -> Node, Pid, fun() -> rpc:call(Node, rabbit, await_startup, []) =:= ok end). while_process_is_alive(Node, Pid, Activity) -> - case process_up(Pid) of + case rabbit_misc:is_os_process_alive(Pid) of true -> case Activity() of true -> ok; false -> timer:sleep(?EXTERNAL_CHECK_INTERVAL), @@ -535,7 +535,7 @@ while_process_is_alive(Node, Pid, Activity) -> end. wait_for_process_death(Pid) -> - case process_up(Pid) of + case rabbit_misc:is_os_process_alive(Pid) of true -> timer:sleep(?EXTERNAL_CHECK_INTERVAL), wait_for_process_death(Pid); false -> ok @@ -559,40 +559,6 @@ read_pid_file(PidFile, Wait) -> 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 () -> - run_ps(Pid) =:= 0 - end}, - {win32, fun () -> - Cmd = "tasklist /nh /fi \"pid eq " ++ Pid ++ "\" ", - Res = rabbit_misc:os_cmd(Cmd ++ "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. - -run_ps(Pid) -> - Port = erlang:open_port({spawn, "ps -p " ++ Pid}, - [exit_status, {line, 16384}, - use_stdio, stderr_to_stdout]), - exit_loop(Port). - -exit_loop(Port) -> - receive - {Port, {exit_status, Rc}} -> Rc; - {Port, _} -> exit_loop(Port) - end. - become(BecomeNode) -> error_logger:tty(false), ok = net_kernel:stop(), diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl index 626341b5f5..5e9c7ceb40 100644 --- a/src/rabbit_misc.erl +++ b/src/rabbit_misc.erl @@ -59,6 +59,7 @@ -export([format_message_queue/2]). -export([append_rpc_all_nodes/4]). -export([os_cmd/1]). +-export([is_os_process_alive/1]). -export([gb_sets_difference/2]). -export([version/0, otp_release/0, which_applications/0]). -export([sequence_error/1]). @@ -232,6 +233,7 @@ -spec(format_message_queue/2 :: (any(), priority_queue:q()) -> term()). -spec(append_rpc_all_nodes/4 :: ([node()], atom(), atom(), [any()]) -> [any()]). -spec(os_cmd/1 :: (string()) -> string()). +-spec(is_os_process_alive/1 :: (non_neg_integer()) -> boolean()). -spec(gb_sets_difference/2 :: (gb_sets:set(), gb_sets:set()) -> gb_sets:set()). -spec(version/0 :: () -> string()). -spec(otp_release/0 :: () -> string()). @@ -932,6 +934,38 @@ os_cmd(Command) -> end end. +is_os_process_alive(Pid) -> + with_os([{unix, fun () -> + run_ps(Pid) =:= 0 + end}, + {win32, fun () -> + Cmd = "tasklist /nh /fi \"pid eq " ++ Pid ++ "\" ", + Res = os_cmd(Cmd ++ "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. + +run_ps(Pid) -> + Port = erlang:open_port({spawn, "ps -p " ++ Pid}, + [exit_status, {line, 16384}, + use_stdio, stderr_to_stdout]), + exit_loop(Port). + +exit_loop(Port) -> + receive + {Port, {exit_status, Rc}} -> Rc; + {Port, _} -> exit_loop(Port) + end. + gb_sets_difference(S1, S2) -> gb_sets:fold(fun gb_sets:delete_any/2, S1, S2). |
