Skip to content

Commit

Permalink
Merge pull request #29 from Adamant-im/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
martiliones authored Jan 4, 2023
2 parents a15bf48 + 2daeabe commit cac266d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
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

0 comments on commit cac266d

Please sign in to comment.