2016-05-14 00:50:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var JobBase = require('./job_base');
|
|
|
|
var jobStatus = require('./job_status');
|
|
|
|
|
|
|
|
function JobMultiple(data) {
|
|
|
|
JobBase.call(this, data);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < this.data.query.length; i++) {
|
2016-05-16 07:22:47 +08:00
|
|
|
var isValid = JobMultiple.super_.prototype.isValidStatusTransition(this.data.query[i].status, finalStatus);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
this.data.query[i].status = finalStatus;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Cannot set status from ' + initialStatus + ' to ' + finalStatus);
|
|
|
|
};
|