CartoDB-SQL-API/lib/batch/models/job-multiple.js

91 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-05-14 00:50:55 +08:00
'use strict';
var util = require('util');
var JobBase = require('./job-base');
var jobStatus = require('../job-status');
2016-05-14 00:50:55 +08:00
2019-12-24 01:19:08 +08:00
function JobMultiple (jobDefinition) {
2016-05-23 17:37:09 +08:00
JobBase.call(this, jobDefinition);
2016-05-14 00:50:55 +08:00
this.init();
}
util.inherits(JobMultiple, JobBase);
module.exports = JobMultiple;
2016-05-16 07:22:47 +08:00
JobMultiple.is = function (query) {
2016-05-14 00:50:55 +08:00
if (!Array.isArray(query)) {
return false;
}
2016-05-16 07:22:47 +08:00
// 1. From user: ['select * from ...', 'select * from ...']
// 2. From redis: [ { query: 'select * from ...', status: 'pending' },
// { query: 'select * from ...', status: 'pending' } ]
2016-05-14 00:50:55 +08:00
for (var i = 0; i < query.length; i++) {
if (typeof query[i] !== 'string') {
2016-05-16 07:22:47 +08:00
if (typeof query[i].query !== 'string') {
return false;
}
2016-05-14 00:50:55 +08:00
}
}
return true;
};
JobMultiple.prototype.init = function () {
2016-05-17 07:00:27 +08:00
if (!this.data.status) {
this.data.status = jobStatus.PENDING;
}
2016-05-14 00:50:55 +08:00
for (var i = 0; i < this.data.query.length; i++) {
2016-05-16 07:22:47 +08:00
if (!this.data.query[i].query && !this.data.query[i].status) {
this.data.query[i] = {
query: this.data.query[i],
status: jobStatus.PENDING
};
}
2016-05-14 00:50:55 +08:00
}
};
JobMultiple.prototype.getNextQuery = function () {
2016-05-16 07:22:47 +08:00
for (var i = 0; i < this.data.query.length; i++) {
if (this.data.query[i].status === jobStatus.PENDING) {
return this.data.query[i].query;
2016-05-14 00:50:55 +08:00
}
}
};
JobMultiple.prototype.setQuery = function (query) {
2016-05-16 07:22:47 +08:00
if (!JobMultiple.is(query)) {
throw new Error('You must indicate a valid SQL');
2016-05-14 00:50:55 +08:00
}
2016-05-16 07:22:47 +08:00
JobMultiple.super_.prototype.setQuery.call(this, query);
2016-05-14 00:50:55 +08:00
};
JobMultiple.prototype.setStatus = function (finalStatus, errorMesssage) {
2016-05-14 00:50:55 +08:00
var initialStatus = this.data.status;
// if transition is to "done" and there are more queries to run
// then job status must be "pending" instead of "done"
// else job status transition to done (if "running")
if (finalStatus === jobStatus.DONE && this.hasNextQuery()) {
JobMultiple.super_.prototype.setStatus.call(this, jobStatus.PENDING);
} else {
JobMultiple.super_.prototype.setStatus.call(this, finalStatus, errorMesssage);
2016-05-14 00:50:55 +08:00
}
for (var i = 0; i < this.data.query.length; i++) {
var isValid = JobMultiple.super_.prototype.isValidTransition(this.data.query[i].status, finalStatus);
2016-05-14 00:50:55 +08:00
if (isValid) {
this.data.query[i].status = finalStatus;
if (finalStatus === jobStatus.FAILED && errorMesssage) {
this.data.query[i].failed_reason = errorMesssage;
}
2016-05-14 00:50:55 +08:00
return;
}
}
throw new Error('Cannot set status from ' + initialStatus + ' to ' + finalStatus);
};