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

209 lines
5.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');
2016-06-01 16:39:01 +08:00
var QueryFallback = require('./query/query_fallback');
var MainFallback = require('./query/main_fallback');
var QueryFactory = require('./query/query_factory');
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();
2016-06-01 16:39:01 +08:00
this.queries = [];
for (var i = 0; i < this.data.query.query.length; i++) {
this.queries[i] = QueryFactory.create(this.data, i);
}
if (MainFallback.is(this.data)) {
this.fallback = new MainFallback();
}
2016-05-16 19:49:56 +08:00
}
util.inherits(JobFallback, JobBase);
module.exports = JobFallback;
// 1. from user: {
2016-05-16 19:49:56 +08:00
// query: {
// query: [{
// query: 'select ...',
// onsuccess: 'select ..'
// }],
// onerror: 'select ...'
// }
// }
//
// 2. 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++) {
2016-06-01 16:39:01 +08:00
if (!QueryFallback.is(query.query[i])) {
2016-05-16 19:49:56 +08:00
return false;
}
}
return true;
};
JobFallback.prototype.init = function () {
for (var i = 0; i < this.data.query.query.length; i++) {
2016-06-02 20:28:35 +08:00
if (shouldInitStatus(this.data.query.query[i])){
this.data.query.query[i].status = jobStatus.PENDING;
2016-06-02 20:28:35 +08:00
}
if (shouldInitQueryFallbackStatus(this.data.query.query[i])) {
this.data.query.query[i].fallback_status = jobStatus.PENDING;
2016-05-16 19:49:56 +08:00
}
}
2016-06-02 20:28:35 +08:00
if (shouldInitStatus(this.data)) {
2016-05-16 19:49:56 +08:00
this.data.status = jobStatus.PENDING;
}
2016-06-02 20:28:35 +08:00
if (shouldInitFallbackStatus(this.data)) {
this.data.fallback_status = jobStatus.PENDING;
}
2016-05-16 19:49:56 +08:00
};
2016-06-02 20:28:35 +08:00
function shouldInitStatus(jobOrQuery) {
return !jobOrQuery.status;
}
function shouldInitQueryFallbackStatus(query) {
return (query.onsuccess || query.onerror) && !query.fallback_status;
}
function shouldInitFallbackStatus(job) {
return (job.query.onsuccess || job.query.onerror) && !job.fallback_status;
}
2016-06-01 16:39:01 +08:00
JobFallback.prototype.getNextQueryFromQueries = function () {
for (var i = 0; i < this.queries.length; i++) {
if (this.queries[i].hasNextQuery(this.data)) {
return this.queries[i].getNextQuery(this.data);
2016-05-16 19:49:56 +08:00
}
}
};
2016-06-03 01:39:48 +08:00
JobFallback.prototype.hasNextQueryFromQueries = function () {
return !!this.getNextQueryFromQueries();
};
2016-06-01 16:39:01 +08:00
JobFallback.prototype.getNextQueryFromFallback = function () {
if (this.fallback && this.fallback.hasNextQuery(this.data)) {
2016-06-01 16:39:01 +08:00
return this.fallback.getNextQuery(this.data);
}
};
2016-06-01 16:39:01 +08:00
JobFallback.prototype.getNextQuery = function () {
var query = this.getNextQueryFromQueries();
2016-06-01 16:39:01 +08:00
if (!query) {
query = this.getNextQueryFromFallback();
}
2016-06-01 16:39:01 +08:00
return query;
};
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-06-01 16:39:01 +08:00
var now = new Date().toISOString();
2016-06-03 01:39:48 +08:00
var hasChanged = this.setQueryStatus(status, this.data, errorMesssage);
hasChanged = this.setJobStatus(status, this.data, hasChanged, errorMesssage);
hasChanged = this.setFallbackStatus(status, this.data, hasChanged);
2016-06-01 16:39:01 +08:00
if (!hasChanged.isValid) {
throw new Error('Cannot set status to ' + status);
}
2016-06-01 16:39:01 +08:00
this.data.updated_at = now;
};
2016-06-03 01:39:48 +08:00
JobFallback.prototype.setQueryStatus = function (status, job, errorMesssage) {
return this.queries.reduce(function (hasChanged, query) {
var result = query.setStatus(status, this.data, hasChanged, errorMesssage);
return result.isValid ? result : hasChanged;
}.bind(this), { isValid: false, appliedToFallback: false });
};
2016-06-01 16:39:01 +08:00
JobFallback.prototype.setJobStatus = function (status, job, hasChanged, errorMesssage) {
2016-06-03 01:39:48 +08:00
var result = {
isValid: false,
appliedToFallback: false
};
2016-06-01 16:39:01 +08:00
status = this.shiftStatus(status, hasChanged);
2016-06-03 01:39:48 +08:00
result.isValid = this.isValidTransition(job.status, status);
if (result.isValid) {
2016-06-01 16:39:01 +08:00
job.status = status;
2016-06-03 01:39:48 +08:00
if (status === jobStatus.FAILED && errorMesssage && !hasChanged.appliedToFallback) {
job.failed_reason = errorMesssage;
}
2016-05-16 19:49:56 +08:00
}
2016-06-03 01:39:48 +08:00
return result.isValid ? result : hasChanged;
};
JobFallback.prototype.setFallbackStatus = function (status, job, hasChanged) {
var result = hasChanged;
if (this.fallback && !this.hasNextQueryFromQueries()) {
result = this.fallback.setStatus(status, job, hasChanged);
}
2016-06-03 01:39:48 +08:00
return result.isValid ? result : hasChanged;
};
2016-06-01 16:39:01 +08:00
JobFallback.prototype.shiftStatus = function (status, hasChanged) {
2016-05-24 17:26:23 +08:00
// jshint maxcomplexity: 7
2016-06-01 16:39:01 +08:00
if (hasChanged.appliedToFallback) {
if (!this.hasNextQueryFromQueries() && (status === jobStatus.DONE || status === jobStatus.FAILED)) {
status = this.getLastFinishedStatus();
} else if (status === jobStatus.DONE || status === jobStatus.FAILED){
status = jobStatus.PENDING;
}
} else if (this.hasNextQueryFromQueries() && status !== jobStatus.RUNNING) {
status = jobStatus.PENDING;
}
return status;
};
JobFallback.prototype.getLastFinishedStatus = function () {
return this.queries.reduce(function (lastFinished, query) {
var status = query.getStatus(this.data);
return this.isFinalStatus(status) ? status : lastFinished;
}.bind(this), jobStatus.DONE);
2016-05-16 19:49:56 +08:00
};