diff --git a/ZelBack/src/services/appsService.js b/ZelBack/src/services/appsService.js index a09ab6027..267294380 100644 --- a/ZelBack/src/services/appsService.js +++ b/ZelBack/src/services/appsService.js @@ -3383,27 +3383,6 @@ async function registerAppLocally(appSpecs, componentSpecs, res) { } else { await installApplicationHard(specificationsToInstall, appName, isComponent, res, appSpecifications); } - - const broadcastedAt = new Date().getTime(); - const newAppRunningMessage = { - type: 'fluxapprunning', - version: 1, - name: appSpecifications.name, - hash: appSpecifications.hash, // hash of application specifics that are running - ip: myIP, - broadcastedAt, - runningSince: broadcastedAt, - }; - - // store it in local database first - // eslint-disable-next-line no-await-in-loop, no-use-before-define - await storeAppRunningMessage(newAppRunningMessage); - // broadcast messages about running apps to all peers - await fluxCommunicationMessagesSender.broadcastMessageToOutgoing(newAppRunningMessage); - await serviceHelper.delay(500); - await fluxCommunicationMessagesSender.broadcastMessageToIncoming(newAppRunningMessage); - // broadcast messages about running apps to all peers - // all done message const successStatus = messageHelper.createSuccessMessage(`Flux App ${appName} successfully installed and launched`); log.info(successStatus); @@ -8896,11 +8875,44 @@ async function trySpawningGlobalApplication() { trySpawningGlobalApplication(); return; } + + let broadcastedAt = new Date().getTime(); + const newAppRunningMessage = { + type: 'fluxapprunning', + version: 1, + name: appSpecifications.name, + hash: appSpecifications.hash, // hash of application specifics that are running + ip: myIP, + broadcastedAt, + runningSince: broadcastedAt, + }; + + // store it in local database first + // eslint-disable-next-line no-await-in-loop, no-use-before-define + await storeAppRunningMessage(newAppRunningMessage); + // broadcast messages about running apps to all peers + await fluxCommunicationMessagesSender.broadcastMessageToOutgoing(newAppRunningMessage); + await serviceHelper.delay(500); + await fluxCommunicationMessagesSender.broadcastMessageToIncoming(newAppRunningMessage); + // an application was selected and checked that it can run on this node. try to install and run it locally // install the app const registerOk = await registerAppLocally(appSpecifications); // can throw if (!registerOk) { log.info('Error on registerAppLocally'); + broadcastedAt = new Date().getTime(); + const appRemovedMessage = { + type: 'fluxappremoved', + version: 1, + appName: appSpecifications.name, + ip: myIP, + broadcastedAt, + }; + log.info('Broadcasting appremoved message to the network'); + // broadcast messages about app removed to all peers + await fluxCommunicationMessagesSender.broadcastMessageToOutgoing(appRemovedMessage); + await serviceHelper.delay(500); + await fluxCommunicationMessagesSender.broadcastMessageToIncoming(appRemovedMessage); await serviceHelper.delay(adjustedDelay); trySpawningGlobalApplication(); return;