2015-12-22 02:57:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var errorCodes = require('../postgresql/error-codes').codeToCondition;
|
|
|
|
var jobStatus = require('./job-status');
|
2016-05-27 01:44:59 +08:00
|
|
|
var Profiler = require('step-profiler');
|
2016-10-10 18:01:36 +08:00
|
|
|
var _ = require('underscore');
|
2015-12-22 02:57:10 +08:00
|
|
|
|
2016-10-21 20:35:24 +08:00
|
|
|
var REDIS_LIMITS = {
|
|
|
|
DB: 5,
|
|
|
|
PREFIX: 'limits:batch:' // + username
|
|
|
|
};
|
|
|
|
|
|
|
|
function JobRunner(jobService, jobQueue, queryRunner, metadataBackend, statsdClient) {
|
2016-05-14 00:50:55 +08:00
|
|
|
this.jobService = jobService;
|
2016-03-31 18:39:03 +08:00
|
|
|
this.jobQueue = jobQueue;
|
|
|
|
this.queryRunner = queryRunner;
|
2016-10-21 20:35:24 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2016-05-27 01:44:59 +08:00
|
|
|
this.statsdClient = statsdClient;
|
2015-12-22 02:57:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
JobRunner.prototype.run = function (job_id, callback) {
|
2015-12-22 02:57:10 +08:00
|
|
|
var self = this;
|
2016-01-08 18:32:01 +08:00
|
|
|
|
2016-06-22 22:10:42 +08:00
|
|
|
var profiler = new Profiler({ statsd_client: self.statsdClient });
|
|
|
|
profiler.start('sqlapi.batch.job');
|
2016-05-27 01:44:59 +08:00
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
self.jobService.get(job_id, function (err, job) {
|
2015-12-22 02:57:10 +08:00
|
|
|
if (err) {
|
2016-01-08 22:47:59 +08:00
|
|
|
return callback(err);
|
2015-12-22 02:57:10 +08:00
|
|
|
}
|
2015-12-29 17:19:10 +08:00
|
|
|
|
2016-10-21 19:09:17 +08:00
|
|
|
self.getQueryStatementTimeout(job.data.user, function(err, timeout) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2016-10-10 18:01:36 +08:00
|
|
|
}
|
2016-05-16 07:22:47 +08:00
|
|
|
|
2016-10-21 19:09:17 +08:00
|
|
|
var query = job.getNextQuery();
|
2016-03-31 18:39:03 +08:00
|
|
|
|
2016-10-21 19:09:17 +08:00
|
|
|
if (_.isObject(query)) {
|
|
|
|
if (Number.isFinite(query.timeout) && query.timeout > 0) {
|
|
|
|
timeout = Math.min(timeout, query.timeout);
|
|
|
|
}
|
|
|
|
query = query.query;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
job.setStatus(jobStatus.RUNNING);
|
|
|
|
} catch (err) {
|
2016-01-08 22:47:59 +08:00
|
|
|
return callback(err);
|
2015-12-22 02:57:10 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 19:09:17 +08:00
|
|
|
self.jobService.save(job, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
profiler.done('running');
|
2016-05-27 01:44:59 +08:00
|
|
|
|
2016-10-21 19:09:17 +08:00
|
|
|
self._run(job, query, timeout, profiler, callback);
|
|
|
|
});
|
2016-01-08 22:47:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-12-22 02:57:10 +08:00
|
|
|
|
2016-10-21 19:09:17 +08:00
|
|
|
JobRunner.prototype.getQueryStatementTimeout = function(username, callback) {
|
|
|
|
var timeout = 12 * 3600 * 1000;
|
|
|
|
if (Number.isFinite(global.settings.batch_query_timeout)) {
|
|
|
|
timeout = global.settings.batch_query_timeout;
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:35:24 +08:00
|
|
|
var batchLimitsKey = REDIS_LIMITS.PREFIX + username;
|
|
|
|
this.metadataBackend.redisCmd(REDIS_LIMITS.DB, 'HGET', [batchLimitsKey, 'timeout'], function(err, timeoutLimit) {
|
|
|
|
if (timeoutLimit !== null && Number.isFinite(+timeoutLimit)) {
|
|
|
|
timeout = +timeoutLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, timeout);
|
|
|
|
});
|
2016-10-21 19:09:17 +08:00
|
|
|
};
|
|
|
|
|
2016-10-10 18:00:54 +08:00
|
|
|
JobRunner.prototype._run = function (job, query, timeout, profiler, callback) {
|
2016-03-18 23:05:36 +08:00
|
|
|
var self = this;
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2018-02-15 19:26:36 +08:00
|
|
|
const dbparams = {
|
|
|
|
pass: job.data.pass,
|
|
|
|
user: job.data.dbuser,
|
|
|
|
dbname: job.data.dbname,
|
|
|
|
port: job.data.port,
|
|
|
|
host: job.data.host
|
|
|
|
};
|
|
|
|
|
|
|
|
self.queryRunner.run(job.data.job_id, query, job.data.user, timeout, dbparams, function (err /*, result */) {
|
2016-03-18 21:57:18 +08:00
|
|
|
if (err) {
|
2016-06-03 17:43:21 +08:00
|
|
|
if (!err.code) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-05-23 23:51:56 +08:00
|
|
|
// if query has been cancelled then it's going to get the current
|
|
|
|
// job status saved by query_canceller
|
2016-10-10 17:58:44 +08:00
|
|
|
if (cancelledByUser(err)) {
|
2016-05-23 23:51:56 +08:00
|
|
|
return self.jobService.get(job.data.job_id, callback);
|
|
|
|
}
|
2016-03-18 21:57:18 +08:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:51:56 +08:00
|
|
|
try {
|
2016-03-31 18:39:03 +08:00
|
|
|
if (err) {
|
2016-06-22 22:10:42 +08:00
|
|
|
profiler.done('failed');
|
2016-05-23 23:51:56 +08:00
|
|
|
job.setStatus(jobStatus.FAILED, err.message);
|
|
|
|
} else {
|
2016-06-22 22:10:42 +08:00
|
|
|
profiler.done('success');
|
2016-05-23 23:51:56 +08:00
|
|
|
job.setStatus(jobStatus.DONE);
|
2016-03-21 22:10:05 +08:00
|
|
|
}
|
2016-05-23 23:51:56 +08:00
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-03-21 22:10:05 +08:00
|
|
|
|
2016-05-23 23:51:56 +08:00
|
|
|
self.jobService.save(job, function (err, job) {
|
|
|
|
if (err) {
|
2016-05-14 00:50:55 +08:00
|
|
|
return callback(err);
|
2016-03-21 22:10:05 +08:00
|
|
|
}
|
|
|
|
|
2016-06-22 22:10:42 +08:00
|
|
|
profiler.done('done');
|
|
|
|
profiler.end();
|
|
|
|
profiler.sendStats();
|
2015-12-31 22:42:31 +08:00
|
|
|
|
2016-05-23 23:51:56 +08:00
|
|
|
if (!job.hasNextQuery()) {
|
|
|
|
return callback(null, job);
|
|
|
|
}
|
|
|
|
|
2016-10-13 03:32:29 +08:00
|
|
|
self.jobQueue.enqueueFirst(job.data.user, job.data.job_id, function (err) {
|
2016-03-31 18:39:03 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-12-31 22:42:31 +08:00
|
|
|
|
2016-05-23 23:51:56 +08:00
|
|
|
callback(null, job);
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-10 17:58:44 +08:00
|
|
|
function cancelledByUser(err) {
|
|
|
|
return errorCodes[err.code.toString()] === 'query_canceled' && err.message.match(/user.*request/);
|
|
|
|
}
|
|
|
|
|
2015-12-22 02:57:10 +08:00
|
|
|
module.exports = JobRunner;
|