2015-12-22 02:57:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-01-23 01:22:21 +08:00
|
|
|
var errorCodes = require('../app/postgresql/error_codes').codeToCondition;
|
2016-04-06 00:50:04 +08:00
|
|
|
var jobStatus = require('./job_status');
|
2015-12-22 02:57:10 +08:00
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
function JobRunner(jobService, jobQueue, queryRunner, userDatabaseMetadataService) {
|
|
|
|
this.jobService = jobService;
|
2016-03-31 18:39:03 +08:00
|
|
|
this.jobQueue = jobQueue;
|
|
|
|
this.queryRunner = queryRunner;
|
2016-05-14 00:50:55 +08:00
|
|
|
this.userDatabaseMetadataService = userDatabaseMetadataService; // TODO: move to queryRunner
|
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-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-05-16 07:22:47 +08:00
|
|
|
var query = job.getNextQuery();
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
try {
|
|
|
|
job.setStatus(jobStatus.RUNNING);
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
2016-03-31 18:39:03 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
self.jobService.save(job, 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
|
|
|
}
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
self._run(job, query, callback);
|
2016-01-08 22:47:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-12-22 02:57:10 +08:00
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
JobRunner.prototype._run = function (job, query, callback) {
|
2016-03-18 23:05:36 +08:00
|
|
|
var self = this;
|
2016-05-14 00:50:55 +08:00
|
|
|
|
|
|
|
// TODO: move to query
|
|
|
|
self.userDatabaseMetadataService.getUserMetadata(job.data.user, function (err, userDatabaseMetadata) {
|
2016-03-18 21:57:18 +08:00
|
|
|
if (err) {
|
2016-03-31 18:39:03 +08:00
|
|
|
return callback(err);
|
2016-03-18 21:57:18 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
self.queryRunner.run(job.data.job_id, query, userDatabaseMetadata, function (err /*, result */) {
|
2016-03-31 18:39:03 +08:00
|
|
|
if (err) {
|
|
|
|
// if query has been cancelled then it's going to get the current
|
|
|
|
// job status saved by query_canceller
|
|
|
|
if (errorCodes[err.code.toString()] === 'query_canceled') {
|
2016-05-14 00:50:55 +08:00
|
|
|
return self.jobService.get(job.data.job_id, callback);
|
2016-03-31 18:39:03 +08:00
|
|
|
}
|
2016-03-21 22:10:05 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
try {
|
2016-05-17 07:00:27 +08:00
|
|
|
if (err) {
|
|
|
|
job.setStatus(jobStatus.FAILED);
|
|
|
|
} else {
|
|
|
|
job.setStatus(jobStatus.DONE);
|
|
|
|
}
|
2016-05-14 00:50:55 +08:00
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
2016-03-21 22:10:05 +08:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
self.jobService.save(job, function (err, job) {
|
2016-03-31 18:39:03 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-12-31 22:42:31 +08:00
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
if (!job.hasNextQuery()) {
|
2016-05-14 00:50:55 +08:00
|
|
|
return callback(null, job);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobQueue.enqueue(job.data.job_id, userDatabaseMetadata.host, function (err) {
|
2016-03-31 18:39:03 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-01-08 22:47:59 +08:00
|
|
|
|
2016-03-31 18:39:03 +08:00
|
|
|
callback(null, job);
|
|
|
|
});
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = JobRunner;
|