Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/helpers/healthCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,42 @@ const HEIGHT_EPSILON = 5; // Used to group nodes by height and choose synced
module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallback) => {
const nodesList = nodes;
let isCheckingNodes = false;
let startupCallback = checkHealthAtStartupCallback;

// Note: it may be not synced; and before first health check a node can reply with obsolete data
let [activeNode] = nodesList;

/**
* Updates active nodes. If nodes are already updating, returns Promise of previous call
* @param {boolean} isPlannedUpdate
* @param {boolean} isFirstUpdate
* @return {Promise} Call changeNodes().then to do something when update complete
*/
async function changeNodes(isPlannedUpdate = false) {
async function changeNodes(isPlannedUpdate = false, isFirstUpdate = false) {
if (!isCheckingNodes) {
isCheckingNodes = true;

if (!isPlannedUpdate) {
logger.warn('[ADAMANT js-api] Health check: Forcing to update active nodes…');
}

await checkNodes(!isPlannedUpdate);

if (isFirstUpdate) {
startupCallback?.();
}

isCheckingNodes = false;

return true;
}
}

/**
* Requests every ADAMANT node for its status, makes a list of live nodes, and chooses one active
* @param {boolean} forceChangeActiveNode
* @param {function?} checkNodesCallback
*/
async function checkNodes(forceChangeActiveNode, checkNodesCallback) {
isCheckingNodes = true;

async function checkNodes(forceChangeActiveNode) {
const liveNodes = [];

try {
Expand Down Expand Up @@ -162,27 +169,33 @@ module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallba
} catch (e) {
logger.warn('[ADAMANT js-api] Health check: Error in checkNodes(), ' + e);
}
}

isCheckingNodes = false;
checkNodesCallback?.();
function setStartupCallback(callback) {
if (!isCheckingNodes) {
callback();
} else {
startupCallback = callback;
}
}

if (checkHealthAtStartup) {
changeNodes(true, checkHealthAtStartupCallback);
changeNodes(true, true);

setInterval(
() => changeNodes(true),
CHECK_NODES_INTERVAL,
);
} else {
checkHealthAtStartupCallback?.();
startupCallback?.();
}

return {
/**
* @return {string} Current active node, f. e. http://88.198.156.44:36666
*/
node: () => activeNode,
setStartupCallback,
changeNodes,
};
};
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = (params, customLogger, checkHealthAtStartupCallback) => {
sendMessage: sendMessage(nodeManager),
newDelegate: newDelegate(nodeManager),
voteForDelegate: voteForDelegate(nodeManager),
setStartupCallback: nodeManager.setStartupCallback,
decodeMsg,
eth,
dash,
Expand Down