2015-12-22 02:57:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var JobBackend = require('./job_backend');
|
|
|
|
var PSQL = require('cartodb-psql');
|
2015-12-25 00:42:49 +08:00
|
|
|
var JobPublisher = require('./job_publisher');
|
2015-12-29 17:19:10 +08:00
|
|
|
var JobQueue = require('./job_queue');
|
2015-12-25 00:42:49 +08:00
|
|
|
var UserIndexer = require('./user_indexer');
|
2015-12-31 22:42:31 +08:00
|
|
|
var QUERY_CANCELED = 57014;
|
2015-12-22 02:57:10 +08:00
|
|
|
|
|
|
|
function JobRunner(metadataBackend, userDatabaseMetadataService) {
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userDatabaseMetadataService = userDatabaseMetadataService;
|
|
|
|
}
|
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
JobRunner.prototype.run = function (job_id) {
|
2015-12-22 02:57:10 +08:00
|
|
|
var self = this;
|
2015-12-29 17:19:10 +08:00
|
|
|
var jobQueue = new JobQueue(this.metadataBackend);
|
2015-12-25 00:42:49 +08:00
|
|
|
var jobPublisher = new JobPublisher();
|
|
|
|
var userIndexer = new UserIndexer(this.metadataBackend);
|
2015-12-29 17:19:10 +08:00
|
|
|
var jobBackend = new JobBackend(this.metadataBackend, jobQueue, jobPublisher, userIndexer);
|
2015-12-22 02:57:10 +08:00
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
jobBackend.get(job_id, function (err, job) {
|
2015-12-22 02:57:10 +08:00
|
|
|
if (err) {
|
|
|
|
return jobBackend.emit('error', err);
|
|
|
|
}
|
2015-12-29 17:19:10 +08:00
|
|
|
|
2015-12-31 03:16:18 +08:00
|
|
|
if (job.status !== 'pending') {
|
|
|
|
return jobBackend.emit('error',
|
|
|
|
new Error('Cannot run job ' + job.job_id + ' due to its status is ' + job.status));
|
|
|
|
}
|
|
|
|
|
2015-12-22 02:57:10 +08:00
|
|
|
self.userDatabaseMetadataService.getUserMetadata(job.user, function (err, userDatabaseMetadata) {
|
|
|
|
if (err) {
|
|
|
|
return jobBackend.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var pg = new PSQL(userDatabaseMetadata, {}, { destroyOnError: true });
|
|
|
|
|
|
|
|
jobBackend.setRunning(job);
|
|
|
|
|
2015-12-22 22:43:00 +08:00
|
|
|
pg.query('SET statement_timeout=0', function(err) {
|
|
|
|
if(err) {
|
|
|
|
return jobBackend.setFailed(job, err);
|
|
|
|
}
|
|
|
|
|
2016-01-04 22:20:06 +08:00
|
|
|
// mark query to allow to users cancel their queries whether users request for it
|
2015-12-31 03:16:18 +08:00
|
|
|
var sql = job.query + ' /* ' + job.job_id + ' */';
|
|
|
|
|
|
|
|
pg.eventedQuery(sql, function (err, query /* , queryCanceller */) {
|
2015-12-24 00:29:11 +08:00
|
|
|
if (err) {
|
|
|
|
return jobBackend.setFailed(job, err);
|
|
|
|
}
|
|
|
|
|
2015-12-22 22:43:00 +08:00
|
|
|
query.on('error', function (err) {
|
2015-12-31 22:42:31 +08:00
|
|
|
if (err.code === QUERY_CANCELED) {
|
|
|
|
return jobBackend.setCancelled(job);
|
|
|
|
}
|
|
|
|
|
2015-12-22 22:43:00 +08:00
|
|
|
jobBackend.setFailed(job, err);
|
|
|
|
});
|
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
query.on('end', function (result) {
|
|
|
|
if (result) {
|
|
|
|
jobBackend.setDone(job);
|
|
|
|
}
|
2015-12-22 22:43:00 +08:00
|
|
|
});
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return jobBackend;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = JobRunner;
|