Skip to content

Commit 79c22e2

Browse files
author
Francesco Mazzoli
committed
Move node waiting to rabbit_control.
That functionality doesn't have much to do with rabbit itself (we should leave it undocumented).
1 parent f222e1b commit 79c22e2

File tree

6 files changed

+87
-116
lines changed

6 files changed

+87
-116
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ start-background-node: all
207207
-rm -f $(RABBITMQ_MNESIA_DIR).pid
208208
mkdir -p $(RABBITMQ_MNESIA_DIR)
209209
setsid sh -c "$(MAKE) run-background-node > $(RABBITMQ_MNESIA_DIR)/startup_log 2> $(RABBITMQ_MNESIA_DIR)/startup_err" &
210-
./wait_node $(RABBITMQ_NODENAME) $(RABBITMQ_MNESIA_DIR).pid
210+
./scripts/rabbitmqctl -n $(RABBITMQ_NODENAME) wait $(RABBITMQ_MNESIA_DIR).pid kernel
211211

212212
start-rabbit-on-node: all
213213
echo "rabbit:start()." | $(ERL_CALL)

src/rabbit.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ status() ->
347347
is_running() -> is_running(node()).
348348

349349
is_running(Node) ->
350-
rabbit_misc:is_running(Node, rabbit).
350+
rabbit_nodes:is_running(Node, rabbit).
351351

352352
environment() ->
353353
lists:keysort(

src/rabbit_control.erl

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
-export([start/0, stop/0, action/5]).
2121

2222
-define(RPC_TIMEOUT, infinity).
23+
-define(EXTERNAL_CHECK_INTERVAL, 1000).
2324

2425
-define(QUIET_OPT, "-q").
2526
-define(NODE_OPT, "-n").
@@ -156,8 +157,8 @@ action(stop, Node, Args, _Opts, Inform) ->
156157
Inform("Stopping and halting node ~p", [Node]),
157158
Res = call(Node, {rabbit, stop_and_halt, []}),
158159
case {Res, Args} of
159-
{ok, [PidFile]} -> rabbit_misc:wait_for_process_death(
160-
rabbit_misc:read_pid_file(PidFile, false));
160+
{ok, [PidFile]} -> wait_for_process_death(
161+
read_pid_file(PidFile, false));
161162
{ok, [_, _| _]} -> exit({badarg, Args});
162163
_ -> ok
163164
end,
@@ -191,9 +192,16 @@ action(force_cluster, Node, ClusterNodeSs, _Opts, Inform) ->
191192
[Node, ClusterNodes]),
192193
rpc_call(Node, rabbit_mnesia, force_cluster, [ClusterNodes]);
193194

194-
action(wait, Node, [PidFile], _Opts, Inform) ->
195+
action(wait, Node, Args, _Opts, Inform) ->
196+
{PidFile, Application} =
197+
case Args of
198+
[PidFile0] -> {PidFile0, rabbit};
199+
[PidFile0, AppString] -> {PidFile0, list_to_atom(AppString)}
200+
end,
195201
Inform("Waiting for ~p", [Node]),
196-
wait_for_application(Node, PidFile, Inform);
202+
Pid = read_pid_file(PidFile, true),
203+
Inform("pid is ~s", [Pid]),
204+
wait_for_application(Node, Pid, Application);
197205

198206
action(status, Node, [], _Opts, Inform) ->
199207
Inform("Status of node ~p", [Node]),
@@ -378,10 +386,71 @@ action(eval, Node, [Expr], _Opts, _Inform) ->
378386

379387
%%----------------------------------------------------------------------------
380388

381-
wait_for_application(Node, PidFile, Inform) ->
382-
Pid = rabbit_misc:read_pid_file(PidFile, true),
383-
Inform("pid is ~s", [Pid]),
384-
rabbit_misc:wait_for_application(Node, Pid, rabbit).
389+
wait_for_application(Node, Pid, Application) ->
390+
case process_up(Pid) of
391+
true -> case rabbit_nodes:is_running(Node, Application) of
392+
true -> ok;
393+
false -> timer:sleep(?EXTERNAL_CHECK_INTERVAL),
394+
wait_for_application(Node, Pid, Application)
395+
end;
396+
false -> {error, process_not_running}
397+
end.
398+
399+
wait_for_process_death(Pid) ->
400+
case process_up(Pid) of
401+
true -> timer:sleep(?EXTERNAL_CHECK_INTERVAL),
402+
wait_for_process_death(Pid);
403+
false -> ok
404+
end.
405+
406+
read_pid_file(PidFile, Wait) ->
407+
case {file:read_file(PidFile), Wait} of
408+
{{ok, Bin}, _} ->
409+
S = string:strip(binary_to_list(Bin), right, $\n),
410+
try list_to_integer(S)
411+
catch error:badarg ->
412+
exit({error, {garbage_in_pid_file, PidFile}})
413+
end,
414+
S;
415+
{{error, enoent}, true} ->
416+
timer:sleep(?EXTERNAL_CHECK_INTERVAL),
417+
read_pid_file(PidFile, Wait);
418+
{{error, _} = E, _} ->
419+
exit({error, {could_not_read_pid, E}})
420+
end.
421+
422+
% Test using some OS clunkiness since we shouldn't trust
423+
% rpc:call(os, getpid, []) at this point
424+
process_up(Pid) ->
425+
with_os([{unix, fun () ->
426+
system("ps -p " ++ Pid
427+
++ " >/dev/null 2>&1") =:= 0
428+
end},
429+
{win32, fun () ->
430+
Res = os:cmd("tasklist /nh /fi \"pid eq " ++
431+
Pid ++ "\" 2>&1"),
432+
case re:run(Res, "erl\\.exe", [{capture, none}]) of
433+
match -> true;
434+
_ -> false
435+
end
436+
end}]).
437+
438+
with_os(Handlers) ->
439+
{OsFamily, _} = os:type(),
440+
case proplists:get_value(OsFamily, Handlers) of
441+
undefined -> throw({unsupported_os, OsFamily});
442+
Handler -> Handler()
443+
end.
444+
445+
% Like system(3)
446+
system(Cmd) ->
447+
ShCmd = "sh -c '" ++ escape_quotes(Cmd) ++ "'",
448+
Port = erlang:open_port({spawn, ShCmd}, [exit_status,nouse_stdio]),
449+
receive {Port, {exit_status, Status}} -> Status end.
450+
451+
% Escape the quotes in a shell command so that it can be used in "sh -c 'cmd'"
452+
escape_quotes(Cmd) ->
453+
lists:flatten(lists:map(fun ($') -> "'\\''"; (Ch) -> Ch end, Cmd)).
385454

386455
format_parse_error({_Line, Mod, Err}) ->
387456
lists:flatten(Mod:format_error(Err)).

src/rabbit_misc.erl

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@
6161
-export([quit/1]).
6262
-export([os_cmd/1]).
6363
-export([gb_sets_difference/2]).
64-
-export([is_running/2, wait_for_application/3, wait_for_process_death/1,
65-
read_pid_file/2]).
66-
67-
-define(EXTERNAL_CHECK_INTERVAL, 1000).
6864

6965
%%----------------------------------------------------------------------------
7066

@@ -210,11 +206,6 @@
210206
-spec(quit/1 :: (integer() | string()) -> no_return()).
211207
-spec(os_cmd/1 :: (string()) -> string()).
212208
-spec(gb_sets_difference/2 :: (gb_set(), gb_set()) -> gb_set()).
213-
-spec(is_running/2 :: (node(), atom()) -> boolean()).
214-
-spec(wait_for_application/3 :: (node(), string(), atom())
215-
-> ok | {error, process_not_running}).
216-
-spec(wait_for_process_death/1 :: (string()) -> ok).
217-
-spec(read_pid_file/2 :: (file:name(), boolean()) -> string() | no_return()).
218209

219210
-endif.
220211

@@ -926,77 +917,3 @@ os_cmd(Command) ->
926917

927918
gb_sets_difference(S1, S2) ->
928919
gb_sets:fold(fun gb_sets:delete_any/2, S1, S2).
929-
930-
%%----------------------------------------------------------------------------
931-
932-
is_running(Node, Application) ->
933-
case rpc:call(Node, application, which_applications, [infinity]) of
934-
{badrpc, _} -> false;
935-
Apps -> proplists:is_defined(Application, Apps)
936-
end.
937-
938-
wait_for_application(Node, Pid, Application) ->
939-
case process_up(Pid) of
940-
true -> case is_running(Node, Application) of
941-
true -> ok;
942-
false -> timer:sleep(?EXTERNAL_CHECK_INTERVAL),
943-
wait_for_application(Node, Pid, Application)
944-
end;
945-
false -> {error, process_not_running}
946-
end.
947-
948-
wait_for_process_death(Pid) ->
949-
case process_up(Pid) of
950-
true -> timer:sleep(?EXTERNAL_CHECK_INTERVAL),
951-
wait_for_process_death(Pid);
952-
false -> ok
953-
end.
954-
955-
read_pid_file(PidFile, Wait) ->
956-
case {file:read_file(PidFile), Wait} of
957-
{{ok, Bin}, _} ->
958-
S = string:strip(binary_to_list(Bin), right, $\n),
959-
try list_to_integer(S)
960-
catch error:badarg ->
961-
exit({error, {garbage_in_pid_file, PidFile}})
962-
end,
963-
S;
964-
{{error, enoent}, true} ->
965-
timer:sleep(?EXTERNAL_CHECK_INTERVAL),
966-
read_pid_file(PidFile, Wait);
967-
{{error, _} = E, _} ->
968-
exit({error, {could_not_read_pid, E}})
969-
end.
970-
971-
% Test using some OS clunkiness since we shouldn't trust
972-
% rpc:call(os, getpid, []) at this point
973-
process_up(Pid) ->
974-
with_os([{unix, fun () ->
975-
system("ps -p " ++ Pid
976-
++ " >/dev/null 2>&1") =:= 0
977-
end},
978-
{win32, fun () ->
979-
Res = os:cmd("tasklist /nh /fi \"pid eq " ++
980-
Pid ++ "\" 2>&1"),
981-
case re:run(Res, "erl\\.exe", [{capture, none}]) of
982-
match -> true;
983-
_ -> false
984-
end
985-
end}]).
986-
987-
with_os(Handlers) ->
988-
{OsFamily, _} = os:type(),
989-
case proplists:get_value(OsFamily, Handlers) of
990-
undefined -> throw({unsupported_os, OsFamily});
991-
Handler -> Handler()
992-
end.
993-
994-
% Like system(3)
995-
system(Cmd) ->
996-
ShCmd = "sh -c '" ++ escape_quotes(Cmd) ++ "'",
997-
Port = erlang:open_port({spawn, ShCmd}, [exit_status,nouse_stdio]),
998-
receive {Port, {exit_status, Status}} -> Status end.
999-
1000-
% Escape the quotes in a shell command so that it can be used in "sh -c 'cmd'"
1001-
escape_quotes(Cmd) ->
1002-
lists:flatten(lists:map(fun ($') -> "'\\''"; (Ch) -> Ch end, Cmd)).

src/rabbit_nodes.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
-module(rabbit_nodes).
1818

19-
-export([names/1, diagnostics/1, make/1, parts/1, cookie_hash/0]).
19+
-export([names/1, diagnostics/1, make/1, parts/1, cookie_hash/0, is_running/2]).
2020

2121
-define(EPMD_TIMEOUT, 30000).
2222

@@ -32,6 +32,7 @@
3232
-spec(make/1 :: ({string(), string()} | string()) -> node()).
3333
-spec(parts/1 :: (node() | string()) -> {string(), string()}).
3434
-spec(cookie_hash/0 :: () -> string()).
35+
-spec(is_running/2 :: (node(), atom()) -> boolean()).
3536

3637
-endif.
3738

@@ -92,3 +93,9 @@ parts(NodeStr) ->
9293

9394
cookie_hash() ->
9495
base64:encode_to_string(erlang:md5(atom_to_list(erlang:get_cookie()))).
96+
97+
is_running(Node, Application) ->
98+
case rpc:call(Node, application, which_applications, [infinity]) of
99+
{badrpc, _} -> false;
100+
Apps -> proplists:is_defined(Application, Apps)
101+
end.

wait_node

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)