@@ -13,35 +13,42 @@ const HEIGHT_EPSILON = 5; // Used to group nodes by height and choose synced
13
13
module . exports = ( nodes , checkHealthAtStartup = true , checkHealthAtStartupCallback ) => {
14
14
const nodesList = nodes ;
15
15
let isCheckingNodes = false ;
16
+ let startupCallback = checkHealthAtStartupCallback ;
16
17
17
18
// Note: it may be not synced; and before first health check a node can reply with obsolete data
18
19
let [ activeNode ] = nodesList ;
19
20
20
21
/**
21
22
* Updates active nodes. If nodes are already updating, returns Promise of previous call
22
23
* @param {boolean } isPlannedUpdate
24
+ * @param {boolean } isFirstUpdate
23
25
* @return {Promise } Call changeNodes().then to do something when update complete
24
26
*/
25
- async function changeNodes ( isPlannedUpdate = false ) {
27
+ async function changeNodes ( isPlannedUpdate = false , isFirstUpdate = false ) {
26
28
if ( ! isCheckingNodes ) {
29
+ isCheckingNodes = true ;
30
+
27
31
if ( ! isPlannedUpdate ) {
28
32
logger . warn ( '[ADAMANT js-api] Health check: Forcing to update active nodes…' ) ;
29
33
}
30
34
31
35
await checkNodes ( ! isPlannedUpdate ) ;
32
36
37
+ if ( isFirstUpdate ) {
38
+ startupCallback ?. ( ) ;
39
+ }
40
+
41
+ isCheckingNodes = false ;
42
+
33
43
return true ;
34
44
}
35
45
}
36
46
37
47
/**
38
48
* Requests every ADAMANT node for its status, makes a list of live nodes, and chooses one active
39
49
* @param {boolean } forceChangeActiveNode
40
- * @param {function? } checkNodesCallback
41
50
*/
42
- async function checkNodes ( forceChangeActiveNode , checkNodesCallback ) {
43
- isCheckingNodes = true ;
44
-
51
+ async function checkNodes ( forceChangeActiveNode ) {
45
52
const liveNodes = [ ] ;
46
53
47
54
try {
@@ -162,27 +169,33 @@ module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallba
162
169
} catch ( e ) {
163
170
logger . warn ( '[ADAMANT js-api] Health check: Error in checkNodes(), ' + e ) ;
164
171
}
172
+ }
165
173
166
- isCheckingNodes = false ;
167
- checkNodesCallback ?. ( ) ;
174
+ function setStartupCallback ( callback ) {
175
+ if ( ! isCheckingNodes ) {
176
+ callback ( ) ;
177
+ } else {
178
+ startupCallback = callback ;
179
+ }
168
180
}
169
181
170
182
if ( checkHealthAtStartup ) {
171
- changeNodes ( true , checkHealthAtStartupCallback ) ;
183
+ changeNodes ( true , true ) ;
172
184
173
185
setInterval (
174
186
( ) => changeNodes ( true ) ,
175
187
CHECK_NODES_INTERVAL ,
176
188
) ;
177
189
} else {
178
- checkHealthAtStartupCallback ?. ( ) ;
190
+ startupCallback ?. ( ) ;
179
191
}
180
192
181
193
return {
182
194
/**
183
195
* @return {string } Current active node, f. e. http://88.198.156.44:36666
184
196
*/
185
197
node : ( ) => activeNode ,
198
+ setStartupCallback,
186
199
changeNodes,
187
200
} ;
188
201
} ;
0 commit comments