2016-05-16 19:49:56 +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');
|
|
|
|
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
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function JobFallback (jobDefinition) {
|
2016-05-23 17:37:09 +08:00
|
|
|
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;
|
|
|
|
|
2016-06-03 00:02:25 +08:00
|
|
|
// 1. from user: {
|
2016-05-16 19:49:56 +08:00
|
|
|
// query: {
|
|
|
|
// query: [{
|
|
|
|
// query: 'select ...',
|
|
|
|
// onsuccess: 'select ..'
|
|
|
|
// }],
|
|
|
|
// onerror: 'select ...'
|
|
|
|
// }
|
|
|
|
// }
|
2016-06-03 00:02:25 +08:00
|
|
|
//
|
|
|
|
// 2. from redis: {
|
2016-05-23 21:05:14 +08:00
|
|
|
// status: 'pending',
|
|
|
|
// fallback_status: 'pending'
|
2016-05-16 19:49:56 +08:00
|
|
|
// query: {
|
|
|
|
// query: [{
|
|
|
|
// query: 'select ...',
|
|
|
|
// onsuccess: 'select ..'
|
2016-05-23 21:05:14 +08:00
|
|
|
// 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++) {
|
2019-12-24 01:19:08 +08:00
|
|
|
if (shouldInitStatus(this.data.query.query[i])) {
|
2016-10-10 18:09:13 +08:00
|
|
|
this.data.query.query[i].status = JobStatus.PENDING;
|
2016-06-02 20:28:35 +08:00
|
|
|
}
|
|
|
|
if (shouldInitQueryFallbackStatus(this.data.query.query[i])) {
|
2016-10-10 18:09:13 +08:00
|
|
|
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-10-10 18:09:13 +08:00
|
|
|
this.data.status = JobStatus.PENDING;
|
2016-05-16 19:49:56 +08:00
|
|
|
}
|
2016-06-02 20:28:35 +08:00
|
|
|
|
|
|
|
if (shouldInitFallbackStatus(this.data)) {
|
2016-10-10 18:09:13 +08:00
|
|
|
this.data.fallback_status = JobStatus.PENDING;
|
2016-06-02 20:28:35 +08:00
|
|
|
}
|
2016-05-16 19:49:56 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function shouldInitStatus (jobOrQuery) {
|
2016-06-02 20:28:35 +08:00
|
|
|
return !jobOrQuery.status;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function shouldInitQueryFallbackStatus (query) {
|
2016-06-02 20:28:35 +08:00
|
|
|
return (query.onsuccess || query.onerror) && !query.fallback_status;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function shouldInitFallbackStatus (job) {
|
2016-06-02 20:28:35 +08:00
|
|
|
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)) {
|
|
|
|
return this.fallback.getNextQuery(this.data);
|
|
|
|
}
|
2016-05-18 00:51:02 +08:00
|
|
|
};
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
JobFallback.prototype.getNextQuery = function () {
|
|
|
|
var query = this.getNextQueryFromQueries();
|
2016-05-18 00:51:02 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
if (!query) {
|
|
|
|
query = this.getNextQueryFromFallback();
|
|
|
|
}
|
2016-05-18 00:51:02 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
return query;
|
2016-05-18 00:51:02 +08:00
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2016-05-19 22:58:14 +08:00
|
|
|
JobFallback.prototype.setStatus = function (status, errorMesssage) {
|
2016-06-01 16:39:01 +08:00
|
|
|
var now = new Date().toISOString();
|
2016-05-26 23:37:37 +08:00
|
|
|
|
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-05-26 23:37:37 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
if (!hasChanged.isValid) {
|
|
|
|
throw new Error('Cannot set status to ' + status);
|
2016-05-26 23:37:37 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
this.data.updated_at = now;
|
2016-05-19 20:10:18 +08:00
|
|
|
};
|
|
|
|
|
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-05-19 20:10:18 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
status = this.shiftStatus(status, hasChanged);
|
2016-05-19 20:10:18 +08:00
|
|
|
|
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-10-10 18:09:13 +08:00
|
|
|
if (status === JobStatus.FAILED && errorMesssage && !hasChanged.appliedToFallback) {
|
2016-06-03 01:39:48 +08:00
|
|
|
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-05-19 22:58:14 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 01:39:48 +08:00
|
|
|
return result.isValid ? result : hasChanged;
|
2016-05-18 00:51:02 +08:00
|
|
|
};
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
JobFallback.prototype.shiftStatus = function (status, hasChanged) {
|
|
|
|
if (hasChanged.appliedToFallback) {
|
2016-10-10 18:09:13 +08:00
|
|
|
if (!this.hasNextQueryFromQueries() && (status === JobStatus.DONE || status === JobStatus.FAILED)) {
|
2016-06-03 00:02:25 +08:00
|
|
|
status = this.getLastFinishedStatus();
|
2019-12-24 01:19:08 +08:00
|
|
|
} else if (status === JobStatus.DONE || status === JobStatus.FAILED) {
|
2016-10-10 18:09:13 +08:00
|
|
|
status = JobStatus.PENDING;
|
2016-05-19 20:10:18 +08:00
|
|
|
}
|
2016-10-10 18:09:13 +08:00
|
|
|
} else if (this.hasNextQueryFromQueries() && status !== JobStatus.RUNNING) {
|
|
|
|
status = JobStatus.PENDING;
|
2016-05-19 20:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
};
|
|
|
|
|
2016-06-03 00:02:25 +08:00
|
|
|
JobFallback.prototype.getLastFinishedStatus = function () {
|
|
|
|
return this.queries.reduce(function (lastFinished, query) {
|
|
|
|
var status = query.getStatus(this.data);
|
|
|
|
return this.isFinalStatus(status) ? status : lastFinished;
|
2016-10-10 18:09:13 +08:00
|
|
|
}.bind(this), JobStatus.DONE);
|
2016-05-16 19:49:56 +08:00
|
|
|
};
|
2016-09-30 22:47:37 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
JobFallback.prototype.toJSON = function () {
|
|
|
|
const queries = this.data.query.query;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: this.constructor.name,
|
|
|
|
id: this.data.job_id,
|
|
|
|
username: this.data.user,
|
|
|
|
status: this.data.status,
|
|
|
|
failed_reason: this.failed_reason,
|
|
|
|
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) => {
|
|
|
|
const node = query.id ? parseQueryId(query.id) : undefined;
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: query.status,
|
|
|
|
fallback_status: query.fallback_status,
|
|
|
|
failed_reason: query.failed_reason,
|
|
|
|
waiting: elapsedTime(this.data.created_at, query.started_at),
|
|
|
|
starttime: query.started_at,
|
|
|
|
endtime: query.ended_at,
|
|
|
|
elapsed: elapsedTime(query.started_at, query.ended_at),
|
|
|
|
id: query.id,
|
|
|
|
analysis: node ? node.analysisId : undefined,
|
|
|
|
node: node ? node.nodeId : undefined,
|
|
|
|
type: node ? node.nodeType : undefined
|
|
|
|
};
|
|
|
|
})
|
|
|
|
};
|
2016-09-30 22:47:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function parseQueryId (queryId) {
|
|
|
|
var data = queryId.split(':');
|
|
|
|
|
2016-09-30 22:53:00 +08:00
|
|
|
if (data.length === 3) {
|
|
|
|
return {
|
|
|
|
analysisId: data[0],
|
|
|
|
nodeId: data[1],
|
|
|
|
nodeType: data[2]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
2016-09-30 22:47:37 +08:00
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:47 +08:00
|
|
|
function elapsedTime (startedAt, endedAt) {
|
|
|
|
if (!startedAt || !endedAt) {
|
2016-09-30 22:47:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:12:47 +08:00
|
|
|
var start = new Date(startedAt);
|
|
|
|
var end = new Date(endedAt);
|
2016-09-30 22:55:33 +08:00
|
|
|
return end.getTime() - start.getTime();
|
2016-09-30 22:47:37 +08:00
|
|
|
}
|