From 38481f524d3e21a58961cf8f81c98af4eb1479c4 Mon Sep 17 00:00:00 2001 From: Page Date: Fri, 2 May 2014 17:22:00 +0100 Subject: [PATCH] Make sure all apps are removed before adding new/updating existing, this is to make sure any ports they use have been freed in case they're needed by the new/updated apps. Conflicts: src/application.coffee --- src/application.coffee | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/application.coffee b/src/application.coffee index 659bb758..5e4550a6 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -151,25 +151,26 @@ exports.update = -> return !_.isEqual(remoteApps[imageId], apps[imageId]) console.log(toBeUpdated) - # Install the apps and add each to the db as they succeed - promises = toBeInstalled.map (imageId) -> - app = remoteApps[imageId] - start(app) + # Delete all the ones to remove in one go + Promise.map toBeRemoved, (imageId) -> + kill(apps[imageId]) .then -> - knex('app').insert(app) - # And restart updated apps and update db as they succeed - promises = promises.concat toBeUpdated.map (imageId) -> - app = remoteApps[imageId] - restart(app) - .then -> - knex('app').update(app).where(imageId: app.imageId) - # And delete all the ones to remove in one go - promises.push( - Promise.map(toBeRemoved, (imageId) -> kill(apps[imageId])) - .then -> - knex('app').whereIn('imageId', toBeRemoved).delete() + knex('app').where('imageId', imageId).delete() ) - Promise.all(promises) + .then -> + # Then install the apps and add each to the db as they succeed + installingPromises = toBeInstalled.map (imageId) -> + app = remoteApps[imageId] + start(app) + .then -> + knex('app').insert(app) + # And restart updated apps and update db as they succeed + updatingPromises = toBeUpdated.map (imageId) -> + app = remoteApps[imageId] + restart(app) + .then -> + knex('app').update(app).where(imageId: app.imageId) + Promise.all(installingPromises.concat(updatingPromises)) .finally -> if currentlyUpdating is 2 # If an update is required then schedule it