Implement Node Monitor Service

- Check in with online nodes every n ms
- Update nodes that do not reply
- Added node object record helper
- Updated mysql wrapper for updating node info to accept bool or number
This commit is contained in:
Logan Cusano
2023-05-07 02:41:58 -04:00
parent 7b2215e9da
commit 9b2d0c4bbb
5 changed files with 141 additions and 51 deletions

View File

@@ -39,30 +39,22 @@ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callbac
res.on('data', (data) => {
const responseObject = {
"statusCode": res.statusCode,
"body": data
"body": (requestOptions.method === "POST") ? JSON.parse(data) : data.toString()
};
try {
responseObject.body = JSON.parse(responseObject.body)
}
catch (err) {
}
log.DEBUG("Response Object: ", responseObject);
return callback(responseObject);
callback(responseObject);
})
}).on('error', err => {
log.ERROR('Error: ', err.message)
if (err.code === "ECONNREFUSED"){
// Bot refused connection, assumed offline
log.WARN("Connection Refused");
}
else log.ERROR('Error: ', err.message, err);
callback(undefined);
// TODO need to handle if the server is down
})
if (requestOptions.timeout) {
req.setTimeout(requestOptions.timeout, () => {
return callback(false);
});
}
// Write the data to the request and send it
req.write(data)
req.end()
req.write(data);
req.end();
}

View File

@@ -68,6 +68,7 @@ exports.addNewNode = (nodeObject, callback) => {
* @param callback Callback function
*/
exports.updateNodeInfo = (nodeObject, callback) => {
if(!nodeObject.id) throw new Error("Attempted to updated node without providing ID", nodeObject);
const name = nodeObject.name,
ip = nodeObject.ip,
port = nodeObject.port,
@@ -84,8 +85,8 @@ exports.updateNodeInfo = (nodeObject, callback) => {
nearbySystems = utils.JsonToBuffer(nearbySystems)
queryParams.push(`nearbySystems = '${nearbySystems}'`);
}
if (typeof online === "boolean") {
if (online) queryParams.push(`online = 1`);
if (typeof online === "boolean" || typeof online === "number") {
if (online || online === 1) queryParams.push(`online = 1`);
else queryParams.push(`online = 0`);
}

View File

@@ -96,4 +96,31 @@ class BaseUserAccount {
}
}
exports.BaseUserAccount = BaseUserAccount;
exports.BaseUserAccount = BaseUserAccount;
/**
*
*/
class nodeObject {
/**
*
* @param {*} param0._id The ID of the node
* @param {*} param0._name The name of the node
* @param {*} param0._ip The IP that the master can contact the node at
* @param {*} param0._port The port that the client is listening on
* @param {*} param0._location The physical location of the node
* @param {*} param0._online An integer representation of the online status of the bot, ie 0=off, 1=on
* @param {*} param0._nearbySystems An object array of nearby systems
*/
constructor({ _id = null, _name = null, _ip = null, _port = null, _location = null, _nearbySystems = null, _online = null }) {
this.id = _id;
this.name = _name;
this.ip = _ip;
this.port = _port;
this.location = _location;
this.nearbySystems = _nearbySystems;
this.online = _online;
}
}
exports.nodeObject = nodeObject;