-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cf9990
commit 511867c
Showing
13 changed files
with
331 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint import/no-dynamic-require: 0 */ | ||
const https = require('https'); | ||
const { Logger } = require('../util/logger.js'); | ||
const { check } = require('../util/environmentCheck.js'); | ||
/* The environment variable LBCONFIG will contain the path to the config file. | ||
The actual path for the config is "../deploy/configs/load_balancer/nodes_data_conf.json" | ||
For the docker containers, the path is /etc/load_balancer/nodes_data_conf.json */ | ||
check('LBCONFIG'); | ||
|
||
const lbConfig = require(process.env.LBCONFIG); | ||
|
||
const loggerConfig = lbConfig.load_balancer; | ||
loggerConfig.cmd = 'log'; | ||
|
||
const logger = new Logger(loggerConfig); | ||
|
||
const getComponentResponse = function getComponentResponse(node, resolve) { | ||
const httpsCheckConn = Object.assign({}, node); | ||
httpsCheckConn.path = '/connectionCheck'; | ||
httpsCheckConn.rejectUnauthorized = false; | ||
|
||
// send a get request and capture the response | ||
const checkConnRequest = https.request(httpsCheckConn, (res) => { | ||
// Buffer the body entirely for processing as a whole. | ||
const bodyChunks = []; | ||
res.on('data', (chunk) => { | ||
bodyChunks.push(chunk); | ||
}).on('end', () => { | ||
const body = Buffer.concat(bodyChunks); | ||
if (body.toString() === 'true') { | ||
logger.debug(`Node at ${node.hostname}:${node.port} is up and running.`); | ||
resolve('up'); | ||
} | ||
}); | ||
}); | ||
|
||
checkConnRequest.on('error', (err) => { | ||
logger.error(`Error connecting to ${node.hostname}:${node.port}`); | ||
logger.error(err); | ||
resolve('down'); | ||
}); | ||
|
||
checkConnRequest.end(); | ||
}; | ||
|
||
const getComponentStatus = function getComponentStatus(node) { | ||
return new Promise((resolve) => { | ||
if (node.hostname === undefined || node.port === undefined || node.role === undefined) { | ||
resolve(undefined); | ||
} else { | ||
const resultJson = Object.assign({}, node); | ||
getComponentResponse(node, (status) => { | ||
resultJson.status = status; | ||
resolve(resultJson); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
class Status { | ||
constructor(nodes) { | ||
this.nodes = nodes; | ||
this.promises = []; | ||
for (let i = 0; i < this.nodes.length; i += 1) { | ||
this.promises.push(getComponentStatus(this.nodes[i])); | ||
} | ||
} | ||
|
||
selectActiveNodes(response) { | ||
const nodeQueue = []; | ||
// Check connection of all nodes | ||
Promise.all(this.promises).then((data) => { | ||
for (let i = 0; i < data.length; i += 1) { | ||
if (data[i].status === 'up') { | ||
nodeQueue.push({ | ||
hostname: data[i].hostname, | ||
port: data[i].port, | ||
}); | ||
logger.debug(`Added node ${data[i].hostname}:${data[i].port} to the node queue.`); | ||
} | ||
} | ||
response(nodeQueue); | ||
}).catch((error) => { | ||
logger.error(error); | ||
}); | ||
} | ||
|
||
checkStatus(response) { | ||
const result = {}; | ||
result.components = []; | ||
Promise.all(this.promises).then((data) => { | ||
result.components = data; | ||
response(result); | ||
}).catch((error) => { | ||
logger.error(error); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
Status, | ||
}; |
Oops, something went wrong.