2016-05-14 00:50:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
2019-10-04 00:24:39 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-05-19 22:58:14 +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 {
|
2016-05-19 23:04:36 +08:00
|
|
|
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++) {
|
2016-06-02 18:26:20 +08:00
|
|
|
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;
|
2016-05-19 22:58:14 +08:00
|
|
|
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);
|
|
|
|
};
|
2020-06-30 23:42:59 +08:00
|
|
|
|
|
|
|
JobMultiple.prototype.toJSON = function () {
|
|
|
|
const queries = this.data.query;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: this.constructor.name,
|
|
|
|
id: this.data.job_id,
|
|
|
|
username: this.data.user,
|
|
|
|
status: this.data.status,
|
|
|
|
created: this.data.created_at,
|
|
|
|
updated: this.data.updated_at,
|
|
|
|
elapsed: elapsedTime(this.data.created_at, this.data.updated_at),
|
|
|
|
dbhost: this.data.host,
|
|
|
|
queries: queries.map((query) => {
|
|
|
|
return {
|
|
|
|
status: query.status,
|
|
|
|
failed_reason: query.failed_reason
|
|
|
|
};
|
|
|
|
})
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function elapsedTime (startedAt, endedAt) {
|
|
|
|
if (!startedAt || !endedAt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var start = new Date(startedAt);
|
|
|
|
var end = new Date(endedAt);
|
|
|
|
return end.getTime() - start.getTime();
|
|
|
|
}
|