Fix resolving job ids.

This commit is contained in:
John-David Dalton
2014-05-06 21:49:33 -07:00
parent 1020637d79
commit 086e1e1a74

View File

@@ -300,7 +300,7 @@ function optionToValue(name, string) {
*/ */
function onJobRemove(error, res, body) { function onJobRemove(error, res, body) {
console.log('removed job id ' + this.id + ': ' + this.url); console.log('removed job id ' + this.id + ': ' + this.url);
this.id = this.url = null; this.id = this.testId = this.url = null;
this.removing = false; this.removing = false;
this.emit('remove'); this.emit('remove');
} }
@@ -314,7 +314,7 @@ function onJobRemove(error, res, body) {
* @param {Object} body The response body JSON object. * @param {Object} body The response body JSON object.
*/ */
function onJobStart(error, res, body) { function onJobStart(error, res, body) {
var id = _.result(body, 'js tests', [])[0], var testId = _.first(_.result(body, 'js tests')),
statusCode = _.result(res, 'statusCode'), statusCode = _.result(res, 'statusCode'),
tunnel = this.tunnel; tunnel = this.tunnel;
@@ -322,7 +322,7 @@ function onJobStart(error, res, body) {
if (this.stopping) { if (this.stopping) {
return; return;
} }
if (error || !id || statusCode != 200) { if (error || !testId || statusCode != 200) {
if (this.attempts < this.retries) { if (this.attempts < this.retries) {
this.restart(); this.restart();
return; return;
@@ -336,8 +336,8 @@ function onJobStart(error, res, body) {
this.emit('complete'); this.emit('complete');
return; return;
} }
this.id = id;
this.running = true; this.running = true;
this.testId = testId;
this.timestamp = _.now(); this.timestamp = _.now();
this.emit('start'); this.emit('start');
this.status(); this.status();
@@ -352,34 +352,34 @@ function onJobStart(error, res, body) {
* @param {Object} body The response body JSON object. * @param {Object} body The response body JSON object.
*/ */
function onJobStatus(error, res, body) { function onJobStatus(error, res, body) {
var data = _.result(body, 'js tests', [{}])[0], var completed = _.result(body, 'completed'),
data = _.result(body, 'js tests', [{}])[0],
jobResult = data.result,
jobStatus = data.status, jobStatus = data.status,
jobUrl = data.url,
options = this.options, options = this.options,
platform = options.platforms[0], platform = options.platforms[0],
result = data.result,
completed = _.result(body, 'completed'),
description = browserName(platform[1]) + ' ' + platform[2] + ' on ' + capitalizeWords(platform[0]), description = browserName(platform[1]) + ' ' + platform[2] + ' on ' + capitalizeWords(platform[0]),
elapsed = (_.now() - this.timestamp) / 1000, elapsed = (_.now() - this.timestamp) / 1000,
expired = (jobStatus != 'test session in progress' && elapsed >= queueTimeout), expired = (jobStatus != 'test session in progress' && elapsed >= queueTimeout),
failures = _.result(result, 'failed'), failures = _.result(jobResult, 'failed'),
label = options.name + ':', label = options.name + ':',
tunnel = this.tunnel, tunnel = this.tunnel;
url = data.url;
this.checking = false; this.checking = false;
this.id = _.last(url.parse(jobUrl).pathname.split('/'));
this.url = jobUrl;
if (!this.running || this.stopping) { if (!this.running || this.stopping) {
return; return;
} }
this.emit('status', jobStatus); this.emit('status', jobStatus);
if (!completed && !expired) { if (!completed && !expired) {
this.statusId = setTimeout(_.bind(this.status, this), this.statusInterval); this._timerId = setTimeout(_.bind(this.status, this), this.statusInterval);
return; return;
} }
this.result = result; this.result = jobResult;
this.url = url; if (!jobResult || failures || reError.test(jobResult.message)) {
if (!result || failures || reError.test(result.message)) {
if (this.attempts < this.retries) { if (this.attempts < this.retries) {
this.restart(); this.restart();
return; return;
@@ -396,7 +396,7 @@ function onJobStatus(error, res, body) {
return; return;
} }
else { else {
var message = _.result(result, 'message', 'no results available. ' + details); var message = _.result(jobResult, 'message', 'no results available. ' + details);
console.error(label, description, chalk.red('failed') + ';', message); console.error(label, description, chalk.red('failed') + ';', message);
} }
} else { } else {
@@ -475,7 +475,7 @@ function Job(properties) {
this.attempts = 0; this.attempts = 0;
this.checking = this.failed = this.removing = this.running = this.starting = this.stopping = false; this.checking = this.failed = this.removing = this.running = this.starting = this.stopping = false;
this.id = this.result = this.url = null; this._timerId = this.id = this.result = this.testId = this.url = null;
} }
util.inherits(Job, EventEmitter); util.inherits(Job, EventEmitter);
@@ -522,7 +522,7 @@ Job.prototype.reset = function(callback) {
} }
this.attempts = 0; this.attempts = 0;
this.failed = false; this.failed = false;
this.id = this.result = this.url = null; this._timerId = this.id = this.result = this.testId = this.url = null;
this.once('reset', _.callback(callback)); this.once('reset', _.callback(callback));
_.defer(_.bind(this.emit, this, 'reset')); _.defer(_.bind(this.emit, this, 'reset'));
@@ -588,7 +588,7 @@ Job.prototype.status = function(callback) {
this.checking = true; this.checking = true;
request.post(_.template('https://saucelabs.com/rest/v1/${user}/js-tests/status', this), { request.post(_.template('https://saucelabs.com/rest/v1/${user}/js-tests/status', this), {
'auth': { 'user': this.user, 'pass': this.pass }, 'auth': { 'user': this.user, 'pass': this.pass },
'json': { 'js tests': [this.id] } 'json': { 'js tests': [this.testId] }
}, _.bind(onJobStatus, this)); }, _.bind(onJobStatus, this));
return this; return this;
@@ -607,9 +607,10 @@ Job.prototype.stop = function(callback) {
return this; return this;
} }
this.stopping = true; this.stopping = true;
if (this.statusId) { if (this._timerId) {
clearTimeout(this._timerId);
this._timerId = null;
this.checking = false; this.checking = false;
this.statusId = clearTimeout(this.statusId);
} }
var onStop = _.bind(onJobStop, this); var onStop = _.bind(onJobStop, this);
if (!this.running) { if (!this.running) {