Skip to content

Commit

Permalink
adjust globalAppsLocations calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Cabecinha84 committed Oct 25, 2023
1 parent 7354cc1 commit 993d48f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
42 changes: 12 additions & 30 deletions ZelBack/src/services/appsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8118,12 +8118,10 @@ async function getAppHashes(req, res) {
async function appLocation(appname) {
const dbopen = dbHelper.databaseConnection();
const database = dbopen.db(config.database.appsglobal.database);
const query = [];
let query = { name: new RegExp(`^${appname}$`, 'i'), removedBroadcastedAt: null };
if (appname) {
query.push({ name: new RegExp(`^${appname}$`, 'i') }); // case insensitive
query = { removedBroadcastedAt: null };
}
query.push({ removedBroadcastedAt: null });
const querySearch = { $and: [query] };
const projection = {
projection: {
_id: 0,
Expand All @@ -8134,25 +8132,6 @@ async function appLocation(appname) {
expireAt: 1,
},
};
const results = await dbHelper.findInDatabase(database, globalAppsLocations, querySearch, projection);
return results;
}

/**
* To get known apps running on a node ip
* @param {string} nodeIp Node IP.
*/
async function appsRunningOnNodeIp(nodeIp) {
const dbopen = dbHelper.databaseConnection();
const database = dbopen.db(config.database.appsglobal.database);
const query = { ip: nodeIp };
const projection = {
projection: {
_id: 0,
name: 1,
hash: 1,
},
};
const results = await dbHelper.findInDatabase(database, globalAppsLocations, query, projection);
return results;
}
Expand Down Expand Up @@ -8235,9 +8214,13 @@ async function getAllGlobalApplications(proj = []) {
* @returns {object[]} Array of running apps.
*/
async function getRunningAppIpList(ip) { // returns all apps running on this ip
let adjustedIp = ip;
if (adjustedIp.endsWith(':16127')) {
adjustedIp = adjustedIp.split(':')[0];
}
const dbopen = dbHelper.databaseConnection();
const database = dbopen.db(config.database.appsglobal.database);
const query = { ip: new RegExp(`^${ip}`) };
const query = { ip: new RegExp(`^${adjustedIp}`), removedBroadcastedAt: null };
const projection = {
projection: {
_id: 0,
Expand All @@ -8260,7 +8243,7 @@ async function getRunningAppIpList(ip) { // returns all apps running on this ip
async function getRunningAppList(appName) {
const dbopen = dbHelper.databaseConnection();
const database = dbopen.db(config.database.appsglobal.database);
const query = { name: appName };
const query = { name: appName, removedBroadcastedAt: null };
const projection = {
projection: {
_id: 0,
Expand Down Expand Up @@ -10837,13 +10820,13 @@ async function updateAppsRunningOnNodeIP(ip, appsRunning) {
* @param {object} ip Node ip and port of the running app.
*/
async function removeAppsRunningOnNodeIP(ip) {
let fixedIp = ip;
if (fixedIp.endsWith(':16127')) {
fixedIp = fixedIp.split(':')[0];
let adjustedIp = ip;
if (adjustedIp.endsWith(':16127')) {
adjustedIp = adjustedIp.split(':')[0];
}
const db = dbHelper.databaseConnection();
const database = db.db(config.database.appsglobal.database);
const queryUpdate = { ip: fixedIp };
const queryUpdate = { ip: adjustedIp };
const update = { $set: { removedBroadcastedAt: new Date() } };
// eslint-disable-next-line no-await-in-loop
await dbHelper.updateInDatabase(database, globalAppsLocations, queryUpdate, update);
Expand Down Expand Up @@ -10989,5 +10972,4 @@ module.exports = {
checkForNonAllowedAppsOnLocalNetwork,
triggerAppHashesCheckAPI,
installedAppsNames,
appsRunningOnNodeIp,
};
4 changes: 2 additions & 2 deletions ZelBack/src/services/fluxService.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ async function sentinelDoubleCheck(urlToConnect) {
timeout,
};
log.info(`sentinelDoubleCheck - checking ${urlToConnect} apps running`);
const appsRunningOnTheSelectedNode = await appsService.appsRunningOnNodeIp(urlToConnect);
const appsRunningOnTheSelectedNode = await appsService.getRunningAppIpList(urlToConnect);
const resMyAppAvailability = await axios.get(`http://${urlToConnect}/apps/installedappsnames`, axiosConfig).catch(async (error) => {
log.error(`sentinelDoubleCheck - ${urlToConnect} for app installedappsnames is not reachable`);
log.error(error);
Expand Down Expand Up @@ -1232,7 +1232,7 @@ async function sentinel() {
timeout,
};
log.info(`sentinel - checking ${urlToConnect} apps running`);
const appsRunningOnTheSelectedNode = await appsService.appsRunningOnNodeIp(urlToConnect);
const appsRunningOnTheSelectedNode = await appsService.getRunningAppIpList(urlToConnect);
const resMyAppAvailability = await axios.get(`http://${urlToConnect}/apps/installedappsnames`, axiosConfig).catch(async (error) => {
log.error(`sentinel - ${urlToConnect} for app installedappsnames is not reachable`);
log.error(error);
Expand Down

0 comments on commit 993d48f

Please sign in to comment.