CartoDB-SQL-API/batch/models/job_fallback.js

299 lines
8.8 KiB
JavaScript
Raw Normal View History

2016-05-16 19:49:56 +08:00
'use strict';
var util = require('util');
var JobBase = require('./job_base');
2016-05-18 00:59:39 +08:00
var jobStatus = require('../job_status');
var breakStatus = [
jobStatus.CANCELLED,
jobStatus.FAILED,
jobStatus.UNKNOWN
];
function isBreakStatus(status) {
return breakStatus.indexOf(status) !== -1;
}
var finalStatus = [
jobStatus.CANCELLED,
jobStatus.DONE,
jobStatus.FAILED,
jobStatus.UNKNOWN
];
function isFinalStatus(status) {
return finalStatus.indexOf(status) !== -1;
}
2016-05-16 19:49:56 +08:00
2016-05-23 17:37:09 +08:00
function JobFallback(jobDefinition) {
JobBase.call(this, jobDefinition);
2016-05-16 19:49:56 +08:00
this.init();
}
util.inherits(JobFallback, JobBase);
module.exports = JobFallback;
// from user: {
// query: {
// query: [{
// query: 'select ...',
// onsuccess: 'select ..'
// }],
// onerror: 'select ...'
// }
// }
// from redis: {
// status: 'pending',
// fallback_status: 'pending'
2016-05-16 19:49:56 +08:00
// query: {
// query: [{
// query: 'select ...',
// onsuccess: 'select ..'
// status: 'pending',
// fallback_status: 'pending',
2016-05-16 19:49:56 +08:00
// }],
// onerror: 'select ...'
// }
// }
JobFallback.is = function (query) {
if (!query.query) {
return false;
}
if (!Array.isArray(query.query)) {
return false;
}
for (var i = 0; i < query.query.length; i++) {
if (!query.query[i].query) {
return false;
}
if (typeof query.query[i].query !== 'string') {
return false;
}
}
return true;
};
JobFallback.prototype.init = function () {
2016-05-23 17:20:42 +08:00
// jshint maxcomplexity: 10
2016-05-16 19:49:56 +08:00
for (var i = 0; i < this.data.query.query.length; i++) {
2016-05-17 07:00:27 +08:00
if ((this.data.query.query[i].onsuccess || this.data.query.query[i].onerror) &&
!this.data.query.query[i].status) {
this.data.query.query[i].status = jobStatus.PENDING;
this.data.query.query[i].fallback_status = jobStatus.PENDING;
2016-05-17 07:00:27 +08:00
} else if (!this.data.query.query[i].status){
2016-05-16 19:49:56 +08:00
this.data.query.query[i].status = jobStatus.PENDING;
}
}
2016-05-17 07:00:27 +08:00
if ((this.data.query.onsuccess || this.data.query.onerror) && !this.data.status) {
this.data.status = jobStatus.PENDING;
this.data.fallback_status = jobStatus.PENDING;
2016-05-17 07:00:27 +08:00
} else if (!this.data.status) {
2016-05-16 19:49:56 +08:00
this.data.status = jobStatus.PENDING;
}
};
JobFallback.prototype.getNextQuery = function () {
var query = this._getNextQueryFromQuery();
if (!query) {
query = this._getNextQueryFromJobFallback();
}
return query;
};
JobFallback.prototype._hasNextQueryFromQuery = function () {
return !!this._getNextQueryFromQuery();
};
JobFallback.prototype._getNextQueryFromQuery = function () {
2016-05-23 17:20:42 +08:00
// jshint maxcomplexity: 10
2016-05-17 07:00:27 +08:00
for (var i = 0; i < this.data.query.query.length; i++) {
if (this.data.query.query[i].fallback_status) {
if (this._isNextQuery(i)) {
2016-05-16 19:49:56 +08:00
return this.data.query.query[i].query;
} else if (this._isNextQueryOnSuccess(i)) {
2016-05-16 19:49:56 +08:00
return this.data.query.query[i].onsuccess;
} else if (this._isNextQueryOnError(i)) {
2016-05-16 19:49:56 +08:00
return this.data.query.query[i].onerror;
} else if (isBreakStatus(this.data.query.query[i].status)) {
return;
2016-05-16 19:49:56 +08:00
}
} else if (this.data.query.query[i].status === jobStatus.PENDING) {
return this.data.query.query[i].query;
}
}
};
2016-05-16 19:49:56 +08:00
JobFallback.prototype._getNextQueryFromJobFallback = function () {
if (this.data.fallback_status) {
if (this._isNextQueryOnSuccessJob()) {
2016-05-16 19:49:56 +08:00
return this.data.query.onsuccess;
} else if (this._isNextQueryOnErrorJob()) {
2016-05-16 19:49:56 +08:00
return this.data.query.onerror;
}
}
};
JobFallback.prototype._isNextQuery = function (index) {
return this.data.query.query[index].status === jobStatus.PENDING;
};
JobFallback.prototype._isNextQueryOnSuccess = function (index) {
return this.data.query.query[index].status === jobStatus.DONE &&
this.data.query.query[index].onsuccess &&
this.data.query.query[index].fallback_status === jobStatus.PENDING;
};
JobFallback.prototype._isNextQueryOnError = function (index) {
return this.data.query.query[index].status === jobStatus.FAILED &&
this.data.query.query[index].onerror &&
this.data.query.query[index].fallback_status === jobStatus.PENDING;
};
JobFallback.prototype._isNextQueryOnSuccessJob = function () {
return this.data.status === jobStatus.DONE &&
this.data.query.onsuccess &&
this.data.fallback_status === jobStatus.PENDING;
};
JobFallback.prototype._isNextQueryOnErrorJob = function () {
return this.data.status === jobStatus.FAILED &&
this.data.query.onerror &&
this.data.fallback_status === jobStatus.PENDING;
};
2016-05-16 19:49:56 +08:00
JobFallback.prototype.setQuery = function (query) {
if (!JobFallback.is(query)) {
throw new Error('You must indicate a valid SQL');
}
JobFallback.super_.prototype.setQuery.call(this, query);
};
JobFallback.prototype.setStatus = function (status, errorMesssage) {
2016-05-17 07:00:27 +08:00
var now = new Date().toISOString();
var resultFromQuery = this._setQueryStatus(status, errorMesssage);
var resultFromJob = this._setJobStatus(status, resultFromQuery.isChangeAppliedToQueryFallback, errorMesssage);
2016-05-17 07:00:27 +08:00
if (!resultFromJob.isValid && !resultFromQuery.isValid) {
throw new Error('Cannot set status from ' + this.data.status + ' to ' + status);
2016-05-17 07:00:27 +08:00
}
this.data.updated_at = now;
};
2016-05-17 07:00:27 +08:00
JobFallback.prototype._getLastStatusFromFinishedQuery = function () {
var lastStatus = jobStatus.DONE;
2016-05-16 19:49:56 +08:00
for (var i = 0; i < this.data.query.query.length; i++) {
if (this.data.query.query[i].fallback_status) {
if (isFinalStatus(this.data.query.query[i].status)) {
lastStatus = this.data.query.query[i].status;
} else {
break;
}
} else {
if (isFinalStatus(this.data.query.query[i].status)) {
lastStatus = this.data.query.query[i].status;
} else {
break;
}
}
2016-05-16 19:49:56 +08:00
}
return lastStatus;
};
JobFallback.prototype._setJobStatus = function (status, isChangeAppliedToQueryFallback, errorMesssage) {
var isValid = false;
status = this._shiftJobStatus(status, isChangeAppliedToQueryFallback);
isValid = this.isValidStatusTransition(this.data.status, status);
if (isValid) {
this.data.status = status;
} else if (this.data.fallback_status) {
isValid = this.isValidStatusTransition(this.data.fallback_status, status);
if (isValid) {
this.data.fallback_status = status;
2016-05-17 07:00:27 +08:00
}
2016-05-16 19:49:56 +08:00
}
if (status === jobStatus.FAILED && errorMesssage && !isChangeAppliedToQueryFallback) {
this.data.failed_reason = errorMesssage;
}
return {
isValid: isValid
};
};
JobFallback.prototype._shiftJobStatus = function (status, isChangeAppliedToQueryFallback) {
2016-05-23 17:20:42 +08:00
// jshint maxcomplexity: 10
// In some scenarios we have to change the normal flow in order to keep consistency
// between query's status and job's status.
if (isChangeAppliedToQueryFallback) {
if (!this._hasNextQueryFromQuery() && (status === jobStatus.DONE || status === jobStatus.FAILED)) {
status = this._getLastStatusFromFinishedQuery();
} else if (status === jobStatus.DONE || status === jobStatus.FAILED){
status = jobStatus.PENDING;
}
} else if (this._hasNextQueryFromQuery() && status !== jobStatus.RUNNING) {
status = jobStatus.PENDING;
}
return status;
};
JobFallback.prototype._setQueryStatus = function (status, errorMesssage) {
// jshint maxcomplexity: 7
var isValid = false;
var isChangeAppliedToQueryFallback = false;
for (var i = 0; i < this.data.query.query.length; i++) {
isValid = this.isValidStatusTransition(this.data.query.query[i].status, status);
if (isValid) {
this.data.query.query[i].status = status;
if (status === jobStatus.FAILED && errorMesssage) {
this.data.query.query[i].failed_reason = errorMesssage;
}
break;
}
if (this.data.query.query[i].fallback_status) {
isValid = this.isValidStatusTransition(this.data.query.query[i].fallback_status, status);
if (isValid) {
this.data.query.query[i].fallback_status = status;
if (status === jobStatus.FAILED && errorMesssage) {
this.data.query.query[i].failed_reason = errorMesssage;
}
isChangeAppliedToQueryFallback = true;
break;
}
}
2016-05-17 07:00:27 +08:00
}
return {
isValid: isValid,
isChangeAppliedToQueryFallback: isChangeAppliedToQueryFallback
};
2016-05-16 19:49:56 +08:00
};