CartoDB-SQL-API/batch/job_runner.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
var errorCodes = require('../app/postgresql/error_codes').codeToCondition;
var PSQL = require('cartodb-psql');
var queue = require('queue-async');
function JobRunner(jobBackend, userDatabaseMetadataService) {
this.jobBackend = jobBackend;
this.userDatabaseMetadataService = userDatabaseMetadataService;
}
JobRunner.prototype.run = function (job_id, callback) {
var self = this;
self.jobBackend.get(job_id, function (err, job) {
if (err) {
return callback(err);
}
if (job.status !== 'pending') {
var error = new Error('Cannot run job ' + job.job_id + ' due to its status is ' + job.status);
error.name = 'InvalidJobStatus';
return callback(error);
}
self.userDatabaseMetadataService.getUserMetadata(job.user, function (err, userDatabaseMetadata) {
if (err) {
return callback(err);
}
self.jobBackend.setRunning(job, function (err, job) {
if (err) {
return callback(err);
}
self._series(job, userDatabaseMetadata, callback);
});
});
});
};
JobRunner.prototype._series = function(job, userDatabaseMetadata, callback) {
var jobQueue = queue(1); // performs in series
if (!Array.isArray(job.query)) {
job.query = [ job.query ];
}
for (var i = 0; i < job.query.length; i++) {
jobQueue.defer(this._query.bind(this), job, userDatabaseMetadata, i);
}
jobQueue.await(function (err, result) {
if (err) {
return callback(err);
}
// last result is the good one
if (Array.isArray(result)) {
return callback(null, result[result.length - 1]);
}
callback(null, result);
})
};
JobRunner.prototype._query = function (job, userDatabaseMetadata, index, callback) {
var self = this;
var pg = new PSQL(userDatabaseMetadata, {}, { destroyOnError: true });
pg.query('SET statement_timeout=0', function (err) {
if(err) {
return self.jobBackend.setFailed(job, err, callback);
}
// mark query to allow to users cancel their queries whether users request for it
var sql = job.query[index] + ' /* ' + job.job_id + ' */';
pg.eventedQuery(sql, function (err, query) {
if (err) {
return self.jobBackend.setFailed(job, err, callback);
}
query.on('error', function (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') {
return self.jobBackend.get(job.job_id, callback);
}
self.jobBackend.setFailed(job, err, callback);
});
query.on('end', function (result) {
// only if result is present then query is done sucessfully otherwise an error has happened
// and it was handled by error listener
if (result) {
return self.jobBackend.setDone(job, callback);
}
});
});
});
};
module.exports = JobRunner;