Add running flag to Job and Tunnel.

This commit is contained in:
John-David Dalton
2014-05-02 08:48:32 -07:00
parent 7e375370fa
commit cbe3a0fde7

View File

@@ -324,6 +324,7 @@ function onJobStart(error, res, body) {
return; return;
} }
this.id = id; this.id = id;
this.running = true;
this.timestamp = _.now(); this.timestamp = _.now();
this.emit('start'); this.emit('start');
this.status(); this.status();
@@ -388,6 +389,7 @@ function onJobStatus(error, res, body) {
} else { } else {
console.log(label, description, chalk.green('passed')); console.log(label, description, chalk.green('passed'));
} }
this.running = false;
this.emit('complete'); this.emit('complete');
} }
@@ -397,7 +399,7 @@ function onJobStatus(error, res, body) {
* @private * @private
*/ */
function onJobStop() { function onJobStop() {
this.stopping = false; this.running = this.stopping = false;
if (!this.tunnel.starting) { if (!this.tunnel.starting) {
this.emit('stop'); this.emit('stop');
} }
@@ -424,6 +426,7 @@ function Job(properties) {
this.attempts = 0; this.attempts = 0;
this.checking = false; this.checking = false;
this.failed = false; this.failed = false;
this.running = false;
this.starting = false; this.starting = false;
this.stopping = false; this.stopping = false;
} }
@@ -461,7 +464,7 @@ Job.prototype.start = function(callback) {
var tunnel = this.tunnel; var tunnel = this.tunnel;
this.once('start', _.callback(callback, this)); this.once('start', _.callback(callback, this));
if (this.starting || tunnel.starting || tunnel.stopping) { if (this.starting || this.running || tunnel.starting || tunnel.stopping) {
return this; return this;
} }
this.starting = true; this.starting = true;
@@ -540,7 +543,9 @@ function Tunnel(properties) {
this.connection = new SauceTunnel(this.user, this.pass, this.id, this.tunneled, this.timeout); this.connection = new SauceTunnel(this.user, this.pass, this.id, this.tunneled, this.timeout);
this.jobs = _.map(this.platforms, function(platform) { this.jobs = { 'active': [], 'queue': [] };
this.jobs.all = _.map(this.platforms, function(platform) {
return new Job(_.merge({ return new Job(_.merge({
'user': this.user, 'user': this.user,
'pass': this.pass, 'pass': this.pass,
@@ -550,8 +555,7 @@ function Tunnel(properties) {
}, this); }, this);
this.attempts = 0; this.attempts = 0;
this.queue = []; this.running = false;
this.running = [];
this.starting = false; this.starting = false;
this.stopping = false; this.stopping = false;
} }
@@ -581,7 +585,7 @@ Tunnel.prototype.restart = function(callback) {
*/ */
Tunnel.prototype.start = function(callback) { Tunnel.prototype.start = function(callback) {
this.once('start', _.callback(callback, this)); this.once('start', _.callback(callback, this));
if (this.starting) { if (this.starting || this.running) {
return this; return this;
} }
console.log('Opening Sauce Connect tunnel...'); console.log('Opening Sauce Connect tunnel...');
@@ -590,7 +594,9 @@ Tunnel.prototype.start = function(callback) {
this.starting = true; this.starting = true;
this.connection.start(function(success) { this.connection.start(function(success) {
tunnel.running = true;
tunnel.starting = false; tunnel.starting = false;
if (!success) { if (!success) {
if (tunnel.attempts < tunnel.retries) { if (tunnel.attempts < tunnel.retries) {
tunnel.restart(); tunnel.restart();
@@ -601,21 +607,24 @@ Tunnel.prototype.start = function(callback) {
} }
console.log('Sauce Connect tunnel opened'); console.log('Sauce Connect tunnel opened');
var jobs = tunnel.jobs,
queue = jobs.queue,
active = jobs.active;
push.apply(queue, jobs.all);
var completed = 0, var completed = 0,
queue = tunnel.queue, total = queue.length;
running = tunnel.running;
push.apply(queue, tunnel.jobs);
var total = queue.length;
tunnel.emit('start'); tunnel.emit('start');
_.invoke(queue, 'on', 'complete', function() { _.invoke(queue, 'on', 'complete', function() {
_.pull(running, this); _.pull(active, this);
if (success) { if (success) {
success = !this.failed; success = !this.failed;
} }
if (++completed == total) { if (++completed == total) {
tunnel.running = false;
tunnel.emit('complete', success); tunnel.emit('complete', success);
return; return;
} }
@@ -636,8 +645,13 @@ Tunnel.prototype.start = function(callback) {
* @param {Object} Returns the tunnel instance. * @param {Object} Returns the tunnel instance.
*/ */
Tunnel.prototype.dequeue = function() { Tunnel.prototype.dequeue = function() {
while (this.queue.length && (this.running.length < this.throttled)) { var jobs = this.jobs,
this.running.push(this.queue.shift().start()); active = jobs.active,
queue = jobs.queue,
throttled = this.throttled;
while (queue.length && (active.length < throttled)) {
active.push(queue.shift().start());
} }
return this; return this;
}; };
@@ -656,25 +670,28 @@ Tunnel.prototype.stop = function(callback) {
} }
console.log('Shutting down Sauce Connect tunnel...'); console.log('Shutting down Sauce Connect tunnel...');
var running = this.running, var jobs = this.jobs,
stopped = 0, active = jobs.active,
total = running.length, queue = jobs.queue;
var stopped = 0,
total = active.length,
tunnel = this; tunnel = this;
var onTunnelStop = function() { var onTunnelStop = function() {
tunnel.stopping = false; tunnel.running = tunnel.stopping = false;
tunnel.emit('stop'); tunnel.emit('stop');
}; };
this.stopping = true; this.stopping = true;
this.queue.length = 0; queue.length = 0;
if (!total) { if (!total) {
_.defer(onTunnelStop); _.defer(onTunnelStop);
return this; return this;
} }
_.invoke(running, 'stop', function() { _.invoke(active, 'stop', function() {
_.pull(running, this); _.pull(active, this);
if (++stopped == total) { if (++stopped == total) {
tunnel.connection.stop(onTunnelStop); tunnel.connection.stop(onTunnelStop);
} }