From 3c3e81a892d6c65b75012abe87812bfb846699d8 Mon Sep 17 00:00:00 2001 From: Page Date: Fri, 14 Mar 2014 16:52:05 +0000 Subject: [PATCH] Install apps in parallel to deleting them - also fixes SQL errors when there are no apps to install. --- src/application.coffee | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/application.coffee b/src/application.coffee index 1125f8ec..227bd261 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -109,13 +109,16 @@ exports.update = -> toBeInstalled = _.difference(remoteApps, localApps) console.log(toBeInstalled) - Promise.all(toBeRemoved.map(kill)).then(-> - Promise.all(toBeInstalled.map(start)) - ).then(-> - knex('app').whereIn('imageId', toBeRemoved).delete().then(-> - knex('app').insert(({imageId: app} for app in toBeInstalled)) - ) + # Install the apps and add each to the db as they succeed + promises = toBeInstalled.map (app) -> + start(app).then -> + knex('app').insert({imageId: app}) + # And delete all the ones to remove in one go + promises.push( + Promise.all(toBeRemoved.map(kill)).then -> + knex('app').whereIn('imageId', toBeRemoved).delete() ) + Promise.all(promises) ) )