@@ -23,12 +23,29 @@ def construct_service_record():
23
23
max_tries = 10
24
24
)
25
25
def discover_peers (service_record ):
26
- print ('Resolving SRV record' , service_record )
27
- answers = dns .resolver .query (service_record , 'SRV' )
28
- # Erlang requires that we drop the trailing period from the absolute DNS
29
- # name to form the hostname used for the Erlang node. This feels hacky
30
- # but not sure of a more official answer
31
- return [rdata .target .to_text ()[:- 1 ] for rdata in answers ]
26
+ expected_peers_count = os .getenv ('COUCHDB_CLUSTER_SIZE' )
27
+ if expected_peers_count :
28
+ print ('Expecting' , expected_peers_count , 'peers...' )
29
+ else :
30
+ print ('Looks like COUCHDB_CLUSTER_SIZE is not set, will not wait for DNS...' )
31
+ peers_count = 0
32
+ while str (peers_count ) != expected_peers_count :
33
+ print ('Resolving SRV record:' , service_record )
34
+ # Erlang requires that we drop the trailing period from the absolute DNS
35
+ # name to form the hostname used for the Erlang node. This feels hacky
36
+ # but not sure of a more official answer
37
+ answers = dns .resolver .query (service_record , 'SRV' )
38
+ peers = [rdata .target .to_text ()[:- 1 ] for rdata in answers ]
39
+ peers_count = len (peers )
40
+ if expected_peers_count :
41
+ print ('Discovered' , peers_count , 'of' , expected_peers_count , 'peers:' , peers )
42
+ if str (peers_count ) != expected_peers_count :
43
+ print ('Waiting for cluster DNS to fully propagate...' )
44
+ time .sleep (5 )
45
+ else :
46
+ print ('Discovered' , peers_count , 'peers:' , peers )
47
+ expected_peers_count = str (peers_count )
48
+ return peers
32
49
33
50
@backoff .on_exception (
34
51
backoff .expo ,
@@ -45,7 +62,7 @@ def connect_the_dots(names):
45
62
else :
46
63
resp = requests .put (uri , data = json .dumps (doc ))
47
64
while resp .status_code == 404 :
48
- print ('Waiting for _nodes DB to be created ...' )
65
+ print ('Waiting for _nodes DB to be created...' )
49
66
time .sleep (5 )
50
67
resp = requests .put (uri , data = json .dumps (doc ))
51
68
print ('Adding cluster member' , name , resp .status_code )
0 commit comments