Skip to content

Commit 9b9a151

Browse files
Introduce rabbit_nodes:await_running_count/2
It will wait until the cluster has N members, up to so many seconds. The function will return immediately for the value of 1. Part of rabbitmq/rabbitmq-cli#235. (cherry picked from commit 2df7b4c)
1 parent edc24f5 commit 9b9a151

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/rabbit_nodes.erl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
-export([names/1, diagnostics/1, make/1, parts/1, cookie_hash/0,
2121
is_running/2, is_process_running/2,
2222
cluster_name/0, set_cluster_name/2, ensure_epmd/0,
23-
all_running/0, name_type/0]).
23+
all_running/0, name_type/0, running_count/0,
24+
await_running_count/2]).
2425

2526
-include_lib("kernel/include/inet.hrl").
2627

28+
-define(SAMPLING_INTERVAL, 1000).
29+
2730
%%----------------------------------------------------------------------------
2831
%% Specs
2932
%%----------------------------------------------------------------------------
@@ -37,6 +40,9 @@
3740
-spec cluster_name() -> binary().
3841
-spec set_cluster_name(binary(), rabbit_types:username()) -> 'ok'.
3942
-spec all_running() -> [node()].
43+
-spec running_count() -> integer().
44+
45+
-spec await_running_count(integer(), integer()) -> 'ok,' | {'error', atom()}.
4046

4147
%%----------------------------------------------------------------------------
4248

@@ -85,3 +91,20 @@ ensure_epmd() ->
8591
rabbit_nodes_common:ensure_epmd().
8692

8793
all_running() -> rabbit_mnesia:cluster_nodes(running).
94+
95+
running_count() -> length(all_running()).
96+
97+
await_running_count(TargetCount, Timeout) ->
98+
Retries = floor(Timeout/?SAMPLING_INTERVAL),
99+
await_running_count_with_retries(TargetCount, Retries).
100+
101+
await_running_count_with_retries(1, _Retries) -> true;
102+
await_running_count_with_retries(_TargetCount, Retries) when Retries =:= 0 ->
103+
{error, timeout};
104+
await_running_count_with_retries(TargetCount, Retries) ->
105+
case running_count() >= TargetCount of
106+
true -> ok;
107+
false ->
108+
timer:sleep(?SAMPLING_INTERVAL),
109+
await_running_count_with_retries(TargetCount, Retries - 1)
110+
end.

0 commit comments

Comments
 (0)