summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandru Scvortov <alexandru@rabbitmq.com>2011-10-25 16:51:58 +0100
committerAlexandru Scvortov <alexandru@rabbitmq.com>2011-10-25 16:51:58 +0100
commitd76e07f23e1daadc178c699f4abe7a50a7eea9d3 (patch)
treec05c55aa6a4058523c8c7c9305721a68c3082cfd
parent95a7ba33b5177763a52d74380fe80b807ee03191 (diff)
downloadrabbitmq-server-d76e07f23e1daadc178c699f4abe7a50a7eea9d3.tar.gz
refactor + guard against garbage
-rw-r--r--src/rabbit_control.erl38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl
index cef5fd67..95fd218e 100644
--- a/src/rabbit_control.erl
+++ b/src/rabbit_control.erl
@@ -20,6 +20,7 @@
-export([start/0, stop/0, action/5, diagnostics/1]).
-define(RPC_TIMEOUT, infinity).
+-define(FILE_CHECK_INTERVAL, 1000).
-define(QUIET_OPT, "-q").
-define(NODE_OPT, "-n").
@@ -163,7 +164,7 @@ usage() ->
action(stop, Node, [PidFile], _Opts, Inform) ->
action(stop, Node, [], _Opts, Inform),
- wait_for_process_death(PidFile);
+ wait_for_process_death(wait_and_read_pid_file(PidFile, false));
action(stop, Node, [], _Opts, Inform) ->
Inform("Stopping and halting node ~p", [Node]),
@@ -366,7 +367,7 @@ action(report, Node, _Args, _Opts, Inform) ->
%%----------------------------------------------------------------------------
wait_for_application(Node, PidFile, Inform) ->
- Pid = wait_and_read_pid_file(PidFile),
+ Pid = wait_and_read_pid_file(PidFile, true),
Inform("pid is ~s", [Pid]),
wait_for_application(Node, Pid).
@@ -374,31 +375,34 @@ wait_for_application(Node, Pid) ->
case process_up(Pid) of
true -> case rabbit:is_running(Node) of
true -> ok;
- false -> timer:sleep(1000),
+ false -> timer:sleep(?FILE_CHECK_INTERVAL),
wait_for_application(Node, Pid)
end;
false -> {error, process_not_running}
end.
-wait_for_process_death(PidFile) ->
- Pid = case file:read_file(PidFile) of
- {ok, Bin} -> string:strip(binary_to_list(Bin), right, $\n);
- {error, _} = E -> exit({error, {could_not_read_pid, E}})
- end,
- wait_for_process_death1(Pid).
-
-wait_for_process_death1(Pid) ->
+wait_for_process_death(Pid) ->
case process_up(Pid) of
- true -> timer:sleep(1000),
- wait_for_process_death1(Pid);
+ true -> timer:sleep(?FILE_CHECK_INTERVAL),
+ wait_for_process_death(Pid);
false -> ok
end.
-wait_and_read_pid_file(PidFile) ->
+wait_and_read_pid_file(PidFile, Wait) ->
case file:read_file(PidFile) of
- {ok, Bin} -> string:strip(binary_to_list(Bin), right, $\n);
- {error, enoent} -> timer:sleep(500),
- wait_and_read_pid_file(PidFile);
+ {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, S}})
+ end,
+ S;
+ {error, enoent} -> case Wait of
+ true -> timer:sleep(500),
+ wait_and_read_pid_file(PidFile, Wait);
+ false -> exit({error, enoent})
+ end;
{error, _} = E -> exit({error, {could_not_read_pid, E}})
end.