mecano
Version:
Common functions for system deployment.
83 lines (80 loc) • 2.11 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var each;
module.exports = function(options, callback) {
var modified, quorum_current;
modified = false;
if (typeof (options.argument != null)) {
if (options.cmd == null) {
options.cmd = options.argument;
}
}
if (options.cmd == null) {
return callback(new Error("Missing cmd: " + options.cmd));
}
if (!Array.isArray(options.cmd)) {
options.cmd = [options.cmd];
}
options.quorum = options.quorum;
if (options.quorum && options.quorum === true) {
options.quorum = Math.ceil(options.cmd.length / 2);
} else if (options.quorum == null) {
options.quorum = options.cmd.length;
}
if (options.interval == null) {
options.interval = 2000;
}
if (options.code_skipped == null) {
options.code_skipped = 1;
}
options.log({
message: "Entering wait for execution",
level: 'DEBUG',
module: 'mecano/wait/execute'
});
quorum_current = 0;
modified = false;
return each(options.cmd).call((function(_this) {
return function(cmd, next) {
var count, run;
count = 0;
if (quorum_current >= options.quorum) {
return next();
}
run = function() {
count++;
options.log({
message: "Attempt #" + count,
level: 'INFO',
module: 'mecano/wait/execute'
});
return _this.execute({
cmd: cmd,
code: options.code || 0,
code_skipped: options.code_skipped
}, function(err, ready) {
if (!err && !ready) {
setTimeout(run, options.interval);
return;
}
if (err) {
return next(err);
}
options.log({
message: "Finish wait for execution",
level: 'INFO',
module: 'mecano/wait/execute'
});
quorum_current++;
if (count > 1) {
modified = true;
}
return next();
});
};
return run();
};
})(this)).then(function(err) {
return callback(err, modified);
});
};
each = require('each');