Skip to content

Commit

Permalink
Merge pull request #104 from RunOnFlux/speed
Browse files Browse the repository at this point in the history
Processing improvements
  • Loading branch information
TheTrunk authored Jul 30, 2024
2 parents 51d122b + 5d2f316 commit eef584a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 58 deletions.
32 changes: 24 additions & 8 deletions src/services/application/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ async function isSyncedOK(ip, port) {
}
}

async function isDaemonSyncedOK(ip, port) {
try {
const url = `http://${ip}:${port}/daemon/getblockchaininfo`;
const response = await serviceHelper.httpGetRequest(url, timeout);
if (response.data.data.blocks + 3 >= response.data.data.headers) {
return true;
}
return false;
} catch (error) {
return false;
}
}

async function hasManyApps(ip, port) {
try {
const url = `http://${ip}:${port}/apps/globalappsspecifications`;
Expand Down Expand Up @@ -185,14 +198,17 @@ async function checkMainFlux(ip, port = 16127) {
if (communicationOK) {
const isSynced = await isSyncedOK(ip, port);
if (isSynced) {
const hasApps = await hasManyApps(ip, port);
if (hasApps) {
const hasMessages = await hasManyMessages(ip, port);
if (hasMessages) {
// eslint-disable-next-line no-await-in-loop
const uiOK = await isHomeOK(ip, +port - 1);
if (uiOK) {
return true;
const isDaemonSynced = isDaemonSyncedOK(ip, port);
if (isDaemonSynced) {
const hasApps = await hasManyApps(ip, port);
if (hasApps) {
const hasMessages = await hasManyMessages(ip, port);
if (hasMessages) {
// eslint-disable-next-line no-await-in-loop
const uiOK = await isHomeOK(ip, +port - 1);
if (uiOK) {
return true;
}
}
}
}
Expand Down
113 changes: 63 additions & 50 deletions src/services/domainService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ let permanentMessages = null;
let globalAppSpecs = null;
const unifiedAppsDomains = [];
const mapOfNamesIps = {};
let recentlyConfiguredApps;
let recentlyConfiguredApps = null;
let recentlyConfiguredGApps = null;
let lastHaproxyAppsConfig = [];

async function getPermanentMessages() {
try {
Expand Down Expand Up @@ -217,9 +219,8 @@ async function generateAndReplaceMainApplicationHaproxyConfig(isGmode = false, t
try {
if (isGmode) {
log.info(`G Mode STARTED at${new Date()}`);
if (!recentlyConfiguredApps) {
throw new Error('G Mode is awaiting processing');
}
} else {
log.info(`Non G Mode STARTED at${new Date()}`);
}
// get permanent messages on the network
await getPermanentMessages();
Expand All @@ -232,29 +233,36 @@ async function generateAndReplaceMainApplicationHaproxyConfig(isGmode = false, t

// filter applications based on config
let applicationSpecifications = getApplicationsToProcess(globalAppSpecs);
if (isGmode) {
const gApps = [];
// in G mode we process only apps that do have g: in containerData
for (const app of applicationSpecifications) {
if (app.version <= 3) {
if (app.containerData.includes('g:')) {
gApps.push(app);
}

const gApps = [];
const nonGApps = [];
// in G mode we process only apps that do have g: in containerData
for (const app of applicationSpecifications) {
if (app.version <= 3) {
if (app.containerData.includes('g:')) {
gApps.push(app);
} else {
let isG = false;
for (const component of app.compose) {
if (component.containerData.includes('g:')) {
isG = true;
}
}
if (isG) {
gApps.push(app);
nonGApps.push(app);
}
} else {
let isG = false;
for (const component of app.compose) {
if (component.containerData.includes('g:')) {
isG = true;
}
}
if (isG) {
gApps.push(app);
} else {
nonGApps.push(app);
}
}
}
if (isGmode) {
applicationSpecifications = gApps;
} else {
applicationSpecifications = nonGApps;
}

// for every application do following
// get name, ports
// main application domain is name.app.domain, for every port we have name-port.app.domain
Expand Down Expand Up @@ -578,49 +586,54 @@ async function generateAndReplaceMainApplicationHaproxyConfig(isGmode = false, t
}

if (isGmode) {
const updatingConfig = JSON.parse(JSON.stringify(recentlyConfiguredApps));
// merge recentlyConfiguredApps with currently configuredApps
for (const app of configuredApps) {
log.info(`G Updating ${app.appName} of ${app.name} with IPs ${JSON.stringify(app.ips)}`);
const appExists = updatingConfig.find((a) => a.appName === app.appName);
log.info(`Exists ${JSON.stringify(appExists)}`);
if (!appExists) {
updatingConfig.push(app);
} else if (!app.ips.length) {
// replace this element with new
updatingConfig.splice(updatingConfig.indexOf(appExists), 1);
} else {
// replace this element with new
updatingConfig.splice(updatingConfig.indexOf(appExists), 1, app);
}
}
configuredApps = updatingConfig;
// remove from configuration apps without ips
configuredApps = configuredApps.filter((app) => app.ips.length > 0);
}

if (configuredApps.length < 10) {
if (!isGmode && configuredApps.length < 10) {
throw new Error('PANIC PLEASE DEV HELP ME');
}
if (JSON.stringify(configuredApps) === JSON.stringify(recentlyConfiguredApps)) {

if (isGmode && JSON.stringify(configuredApps) === JSON.stringify(recentlyConfiguredGApps)) {
log.info('No changes in Gmode configuration detected');
} else if (!isGmode && JSON.stringify(configuredApps) === JSON.stringify(recentlyConfiguredApps)) {
log.info('No changes in configuration detected');
} else if (isGmode) {
log.info('Changes in configuration detected in G mode');
} else {
log.info('Changes in configuration detected');
}
recentlyConfiguredApps = configuredApps;
const hc = await haproxyTemplate.createAppsHaproxyConfig(configuredApps);
console.log(hc);
const dataToWrite = hc;
// test haproxy config
const successRestart = await haproxyTemplate.restartProxy(dataToWrite);
if (!successRestart) {
throw new Error('Invalid HAPROXY Config File!');
let haproxyAppsConfig = [];
if (isGmode) {
recentlyConfiguredGApps = configuredApps;
if (recentlyConfiguredApps) {
haproxyAppsConfig = recentlyConfiguredApps.concat(configuredApps);
}
} else {
recentlyConfiguredApps = configuredApps;
if (recentlyConfiguredGApps) {
haproxyAppsConfig = configuredApps.concat(recentlyConfiguredGApps); // we need to put always in same order to avoid. non g first g at end
}
}

if (recentlyConfiguredGApps && recentlyConfiguredGApps && JSON.stringify(lastHaproxyAppsConfig) !== JSON.stringify(haproxyAppsConfig)) {
lastHaproxyAppsConfig = haproxyAppsConfig;
const hc = await haproxyTemplate.createAppsHaproxyConfig(haproxyAppsConfig);
console.log(hc);
const dataToWrite = hc;
// test haproxy config
const successRestart = await haproxyTemplate.restartProxy(dataToWrite);
if (!successRestart) {
throw new Error('Invalid HAPROXY Config File!');
}
}
} catch (error) {
log.error(error);
} finally {
if (isGmode) {
log.info(`G Mode ENDED at${new Date()}`);
} else {
log.info(`Non G Mode ENDED at${new Date()}`);
}
setTimeout(() => {
generateAndReplaceMainApplicationHaproxyConfig(isGmode, timeout);
Expand Down Expand Up @@ -687,14 +700,14 @@ function initializeServices() {
generateAndReplaceMainApplicationHaproxyConfig(false);
setTimeout(() => {
generateAndReplaceMainApplicationHaproxyConfig(true, 5);
}, 5 * 60 * 1000);
}, 2 * 60 * 1000);
log.info('Flux Main Application Domain Service initiated.');
} else if (config.mainDomain === config.pDNS.domain && config.pDNS.manageapp) {
// only runs on main FDM handles X.APP.runonflux.io
generateAndReplaceMainApplicationHaproxyConfig(false);
setTimeout(() => {
generateAndReplaceMainApplicationHaproxyConfig(true, 5);
}, 5 * 60 * 1000);
}, 2 * 60 * 1000);
log.info('Flux Main Application Domain Service initiated.');
} else {
log.info('CUSTOM DOMAIN SERVICE UNAVAILABLE');
Expand Down

0 comments on commit eef584a

Please sign in to comment.