Ensure add/remove modules are handled sequentially

This commit is contained in:
Nick O'Leary 2018-07-30 10:08:39 +01:00
parent f1d5bbb036
commit 5155770213
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 133 additions and 118 deletions

View File

@ -15,7 +15,6 @@
**/ **/
var when = require("when");
var path = require("path"); var path = require("path");
var fs = require("fs"); var fs = require("fs");
@ -36,6 +35,8 @@ function init(_settings) {
settings = _settings; settings = _settings;
} }
var activePromise = Promise.resolve();
function checkModulePath(folder) { function checkModulePath(folder) {
var moduleName; var moduleName;
var err; var err;
@ -71,8 +72,9 @@ function checkExistingModule(module,version) {
return false; return false;
} }
function installModule(module,version) { function installModule(module,version) {
activePromise = activePromise.then(() => {
//TODO: ensure module is 'safe' //TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) { return new Promise((resolve,reject) => {
var installName = module; var installName = module;
var isUpgrade = false; var isUpgrade = false;
try { try {
@ -144,6 +146,12 @@ function installModule(module,version) {
} }
}); });
}); });
}).catch(err => {
// In case of error, reset activePromise to be resolvable
activePromise = Promise.resolve();
throw err;
});
return activePromise;
} }
@ -176,7 +184,8 @@ function reportRemovedModules(removedNodes) {
} }
function uninstallModule(module) { function uninstallModule(module) {
return when.promise(function(resolve,reject) { activePromise = activePromise.then(() => {
return new Promise((resolve,reject) => {
if (/[\s;]/.test(module)) { if (/[\s;]/.test(module)) {
reject(new Error(log._("server.install.invalid"))); reject(new Error(log._("server.install.invalid")));
return; return;
@ -217,6 +226,12 @@ function uninstallModule(module) {
} }
); );
}); });
}).catch(err => {
// In case of error, reset activePromise to be resolvable
activePromise = Promise.resolve();
throw err;
});
return activePromise;
} }
function checkPrereq() { function checkPrereq() {
@ -227,9 +242,9 @@ function checkPrereq() {
) { ) {
log.info(log._("server.palette-editor.disabled")); log.info(log._("server.palette-editor.disabled"));
paletteEditorEnabled = false; paletteEditorEnabled = false;
return when.resolve(); return Promise.resolve();
} else { } else {
return when.promise(function(resolve) { return new Promise(resolve => {
child_process.execFile(npmCommand,['-v'],function(err) { child_process.execFile(npmCommand,['-v'],function(err) {
if (err) { if (err) {
log.info(log._("server.palette-editor.npm-not-found")); log.info(log._("server.palette-editor.npm-not-found"));

View File

@ -73,7 +73,7 @@ describe('nodes/registry/installer', function() {
return ee; return ee;
}); });
installer.installModule("this_wont_exist").otherwise(function(err) { installer.installModule("this_wont_exist").catch(function(err) {
err.code.should.be.eql(404); err.code.should.be.eql(404);
done(); done();
}); });
@ -95,7 +95,7 @@ describe('nodes/registry/installer', function() {
} }
}); });
installer.installModule("this_wont_exist","0.1.2").otherwise(function(err) { installer.installModule("this_wont_exist","0.1.2").catch(function(err) {
err.code.should.be.eql(404); err.code.should.be.eql(404);
done(); done();
}); });
@ -106,7 +106,7 @@ describe('nodes/registry/installer', function() {
version: "0.1.1" version: "0.1.1"
} }
}); });
installer.installModule("this_wont_exist","0.1.1").otherwise(function(err) { installer.installModule("this_wont_exist","0.1.1").catch(function(err) {
err.code.should.be.eql('module_already_loaded'); err.code.should.be.eql('module_already_loaded');
done(); done();
}); });
@ -125,7 +125,7 @@ describe('nodes/registry/installer', function() {
installer.installModule("this_wont_exist").then(function() { installer.installModule("this_wont_exist").then(function() {
done(new Error("Unexpected success")); done(new Error("Unexpected success"));
}).otherwise(function(err) { }).catch(function(err) {
done(); done();
}); });
}); });
@ -150,7 +150,7 @@ describe('nodes/registry/installer', function() {
// commsMessages[0].topic.should.equal("node/added"); // commsMessages[0].topic.should.equal("node/added");
// commsMessages[0].msg.should.eql(nodeInfo.nodes); // commsMessages[0].msg.should.eql(nodeInfo.nodes);
done(); done();
}).otherwise(function(err) { }).catch(function(err) {
done(err); done(err);
}); });
}); });
@ -159,7 +159,7 @@ describe('nodes/registry/installer', function() {
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local","TestNodeModule","node_modules","NonExistant")); var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local","TestNodeModule","node_modules","NonExistant"));
installer.installModule(resourcesDir).then(function() { installer.installModule(resourcesDir).then(function() {
done(new Error("Unexpected success")); done(new Error("Unexpected success"));
}).otherwise(function(err) { }).catch(function(err) {
if (err.hasOwnProperty("code")) { if (err.hasOwnProperty("code")) {
err.code.should.eql(404); err.code.should.eql(404);
done(); done();
@ -189,7 +189,7 @@ describe('nodes/registry/installer', function() {
installer.installModule(resourcesDir).then(function(info) { installer.installModule(resourcesDir).then(function(info) {
info.should.eql(nodeInfo); info.should.eql(nodeInfo);
done(); done();
}).otherwise(function(err) { }).catch(function(err) {
done(err); done(err);
}); });
}); });
@ -218,7 +218,7 @@ describe('nodes/registry/installer', function() {
installer.uninstallModule("this_wont_exist").then(function() { installer.uninstallModule("this_wont_exist").then(function() {
done(new Error("Unexpected success")); done(new Error("Unexpected success"));
}).otherwise(function(err) { }).catch(function(err) {
done(); done();
}); });
}); });
@ -242,7 +242,7 @@ describe('nodes/registry/installer', function() {
// commsMessages[0].topic.should.equal("node/removed"); // commsMessages[0].topic.should.equal("node/removed");
// commsMessages[0].msg.should.eql(nodeInfo); // commsMessages[0].msg.should.eql(nodeInfo);
done(); done();
}).otherwise(function(err) { }).catch(function(err) {
done(err); done(err);
}); });
}); });