2015-12-07 16:40:51 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
|
|
|
var step = require('step');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
var UserDatabaseService = require('../services/user_database_service');
|
2015-12-16 22:57:58 +08:00
|
|
|
var JobPublisher = require('../../batch/job_publisher');
|
2015-12-29 17:19:10 +08:00
|
|
|
var JobQueue = require('../../batch/job_queue');
|
2015-12-25 00:42:49 +08:00
|
|
|
var UserIndexer = require('../../batch/user_indexer');
|
2015-12-22 02:57:10 +08:00
|
|
|
var JobBackend = require('../../batch/job_backend');
|
2015-12-31 03:16:18 +08:00
|
|
|
var JobCanceller = require('../../batch/job_canceller');
|
|
|
|
var UserDatabaseMetadataService = require('../../batch/user_database_metadata_service');
|
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
var CdbRequest = require('../models/cartodb_request');
|
|
|
|
var handleException = require('../utils/error_handler');
|
|
|
|
|
|
|
|
var cdbReq = new CdbRequest();
|
|
|
|
var userDatabaseService = new UserDatabaseService();
|
|
|
|
|
|
|
|
function JobController(metadataBackend, tableCache, statsd_client) {
|
2015-12-29 17:19:10 +08:00
|
|
|
var jobQueue = new JobQueue(metadataBackend);
|
2015-12-25 00:42:49 +08:00
|
|
|
var jobPublisher = new JobPublisher();
|
|
|
|
var userIndexer = new UserIndexer(metadataBackend);
|
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.tableCache = tableCache;
|
|
|
|
this.statsd_client = statsd_client;
|
2015-12-29 17:19:10 +08:00
|
|
|
this.jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
|
2015-12-31 03:16:18 +08:00
|
|
|
this.userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JobController.prototype.route = function (app) {
|
2015-12-22 02:57:10 +08:00
|
|
|
app.post(global.settings.base_url + '/job', this.createJob.bind(this));
|
2015-12-25 00:42:49 +08:00
|
|
|
app.get(global.settings.base_url + '/job', this.listJob.bind(this));
|
2015-12-24 00:29:11 +08:00
|
|
|
app.get(global.settings.base_url + '/job/:job_id', this.getJob.bind(this));
|
2015-12-31 03:16:18 +08:00
|
|
|
app.delete(global.settings.base_url + '/job/:job_id', this.cancelJob.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
JobController.prototype.cancelJob = function (req, res) {
|
|
|
|
var self = this;
|
|
|
|
var job_id = req.params.job_id;
|
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
|
|
|
if (!_.isString(job_id)) {
|
|
|
|
return handleException(new Error("You must indicate a job id"), res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
}
|
|
|
|
|
|
|
|
req.aborted = false;
|
|
|
|
req.on("close", function() {
|
|
|
|
if (req.formatter && _.isFunction(req.formatter.cancel)) {
|
|
|
|
req.formatter.cancel();
|
|
|
|
}
|
|
|
|
req.aborted = true; // TODO: there must be a builtin way to check this
|
|
|
|
});
|
|
|
|
|
|
|
|
function checkAborted(step) {
|
|
|
|
if ( req.aborted ) {
|
|
|
|
var err = new Error("Request aborted during " + step);
|
|
|
|
// We'll use status 499, same as ngnix in these cases
|
|
|
|
// see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
|
|
|
|
err.http_status = 499;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
|
|
|
var options = {
|
|
|
|
req: req,
|
|
|
|
params: params,
|
|
|
|
checkAborted: checkAborted,
|
|
|
|
metadataBackend: self.metadataBackend,
|
|
|
|
cdbUsername: cdbUsername
|
|
|
|
};
|
|
|
|
userDatabaseService.getUserDatabase(options, this);
|
|
|
|
},
|
|
|
|
function cancelJob(err, userDatabase) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
checkAborted('cancelJob');
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
|
|
|
var jobCanceller = new JobCanceller(self.metadataBackend, self.userDatabaseMetadataService);
|
|
|
|
|
|
|
|
jobCanceller.cancel(job_id)
|
|
|
|
.on('cancelled', function () {
|
|
|
|
next(null, {
|
|
|
|
cancelled: true,
|
|
|
|
host: userDatabase.host
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.on('error', function (err) {
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
|
|
|
return handleException(err, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('cancelJob');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
cancelled: result.cancelled
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2015-12-07 16:40:51 +08:00
|
|
|
};
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
JobController.prototype.listJob = function (req, res) {
|
2015-12-22 18:06:46 +08:00
|
|
|
var self = this;
|
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
2015-12-22 18:45:25 +08:00
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
}
|
|
|
|
|
|
|
|
req.aborted = false;
|
|
|
|
req.on("close", function() {
|
|
|
|
if (req.formatter && _.isFunction(req.formatter.cancel)) {
|
|
|
|
req.formatter.cancel();
|
|
|
|
}
|
|
|
|
req.aborted = true; // TODO: there must be a builtin way to check this
|
|
|
|
});
|
|
|
|
|
|
|
|
function checkAborted(step) {
|
|
|
|
if ( req.aborted ) {
|
|
|
|
var err = new Error("Request aborted during " + step);
|
|
|
|
// We'll use status 499, same as ngnix in these cases
|
|
|
|
// see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
|
|
|
|
err.http_status = 499;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
|
|
|
var options = {
|
|
|
|
req: req,
|
|
|
|
params: params,
|
|
|
|
checkAborted: checkAborted,
|
|
|
|
metadataBackend: self.metadataBackend,
|
|
|
|
cdbUsername: cdbUsername
|
|
|
|
};
|
|
|
|
userDatabaseService.getUserDatabase(options, this);
|
|
|
|
},
|
2015-12-25 00:47:41 +08:00
|
|
|
function listJob(err, userDatabase) {
|
2015-12-22 18:45:25 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2015-12-23 21:55:49 +08:00
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
|
|
|
|
2015-12-22 18:45:25 +08:00
|
|
|
var next = this;
|
|
|
|
|
|
|
|
checkAborted('persistJob');
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:47:41 +08:00
|
|
|
self.jobBackend.list(cdbUsername, function (err, jobs) {
|
2015-12-22 18:45:25 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(null, {
|
2015-12-25 00:47:41 +08:00
|
|
|
jobs: jobs,
|
2015-12-31 03:16:18 +08:00
|
|
|
host: userDatabase.host
|
2015-12-22 18:45:25 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
2015-12-23 21:55:49 +08:00
|
|
|
return handleException(err, res);
|
2015-12-22 18:45:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('enqueueJob');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:47:41 +08:00
|
|
|
res.send(result.jobs);
|
2015-12-22 18:45:25 +08:00
|
|
|
}
|
|
|
|
);
|
2015-12-22 18:06:46 +08:00
|
|
|
};
|
2015-12-22 18:02:16 +08:00
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
JobController.prototype.getJob = function (req, res) {
|
2015-12-07 16:40:51 +08:00
|
|
|
var self = this;
|
2015-12-25 00:42:49 +08:00
|
|
|
var job_id = req.params.job_id;
|
2015-12-07 16:40:51 +08:00
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
if (!_.isString(job_id)) {
|
|
|
|
return handleException(new Error("You must indicate a job id"), res);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
}
|
|
|
|
|
|
|
|
req.aborted = false;
|
|
|
|
req.on("close", function() {
|
|
|
|
if (req.formatter && _.isFunction(req.formatter.cancel)) {
|
|
|
|
req.formatter.cancel();
|
|
|
|
}
|
|
|
|
req.aborted = true; // TODO: there must be a builtin way to check this
|
|
|
|
});
|
|
|
|
|
|
|
|
function checkAborted(step) {
|
|
|
|
if ( req.aborted ) {
|
|
|
|
var err = new Error("Request aborted during " + step);
|
|
|
|
// We'll use status 499, same as ngnix in these cases
|
|
|
|
// see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
|
|
|
|
err.http_status = 499;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
|
|
|
var options = {
|
|
|
|
req: req,
|
|
|
|
params: params,
|
|
|
|
checkAborted: checkAborted,
|
|
|
|
metadataBackend: self.metadataBackend,
|
|
|
|
cdbUsername: cdbUsername
|
|
|
|
};
|
|
|
|
userDatabaseService.getUserDatabase(options, this);
|
|
|
|
},
|
2015-12-25 00:42:49 +08:00
|
|
|
function getJob(err, userDatabase) {
|
2015-12-07 16:40:51 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2015-12-23 21:55:49 +08:00
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
var next = this;
|
|
|
|
|
2015-12-22 18:06:46 +08:00
|
|
|
checkAborted('persistJob');
|
2015-12-07 16:40:51 +08:00
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
self.jobBackend.get(job_id, function (err, job) {
|
2015-12-07 16:40:51 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2015-12-07 16:40:51 +08:00
|
|
|
next(null, {
|
2015-12-25 00:42:49 +08:00
|
|
|
job: job,
|
2015-12-31 03:16:18 +08:00
|
|
|
host: userDatabase.host
|
2015-12-09 07:02:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-12-25 00:42:49 +08:00
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
|
|
|
return handleException(err, res);
|
|
|
|
}
|
2015-12-09 07:02:08 +08:00
|
|
|
|
2015-12-23 21:55:49 +08:00
|
|
|
if ( req.profiler ) {
|
2015-12-25 00:42:49 +08:00
|
|
|
req.profiler.done('enqueueJob');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
2015-12-23 21:55:49 +08:00
|
|
|
}
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(result.job);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// jshint maxcomplexity:21
|
|
|
|
JobController.prototype.createJob = function (req, res) {
|
|
|
|
var self = this;
|
|
|
|
var body = (req.body) ? req.body : {};
|
|
|
|
var params = _.extend({}, req.query, body); // clone so don't modify req.params or req.body so oauth is not broken
|
|
|
|
var sql = (params.query === "" || _.isUndefined(params.query)) ? null : params.query;
|
|
|
|
var cdbUsername = cdbReq.userByReq(req);
|
|
|
|
|
|
|
|
if (!_.isString(sql)) {
|
|
|
|
return handleException(new Error("You must indicate a sql query"), res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.start('sqlapi.job');
|
|
|
|
}
|
|
|
|
|
|
|
|
req.aborted = false;
|
|
|
|
req.on("close", function() {
|
|
|
|
if (req.formatter && _.isFunction(req.formatter.cancel)) {
|
|
|
|
req.formatter.cancel();
|
|
|
|
}
|
|
|
|
req.aborted = true; // TODO: there must be a builtin way to check this
|
|
|
|
});
|
|
|
|
|
|
|
|
function checkAborted(step) {
|
|
|
|
if ( req.aborted ) {
|
|
|
|
var err = new Error("Request aborted during " + step);
|
|
|
|
// We'll use status 499, same as ngnix in these cases
|
|
|
|
// see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
|
|
|
|
err.http_status = 499;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('init');
|
|
|
|
}
|
|
|
|
|
|
|
|
step(
|
|
|
|
function getUserDBInfo() {
|
|
|
|
var options = {
|
|
|
|
req: req,
|
|
|
|
params: params,
|
|
|
|
checkAborted: checkAborted,
|
|
|
|
metadataBackend: self.metadataBackend,
|
|
|
|
cdbUsername: cdbUsername
|
|
|
|
};
|
|
|
|
userDatabaseService.getUserDatabase(options, this);
|
|
|
|
},
|
|
|
|
function persistJob(err, userDatabase) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
if (!userDatabase.authenticated) {
|
|
|
|
throw new Error('permission denied');
|
|
|
|
}
|
2015-12-22 18:06:46 +08:00
|
|
|
|
2015-12-09 07:02:08 +08:00
|
|
|
var next = this;
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
checkAborted('persistJob');
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('setDBAuth');
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobBackend.create(cdbUsername, sql, userDatabase.host, function (err, result) {
|
2015-12-09 07:02:08 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(null, {
|
2015-12-25 00:42:49 +08:00
|
|
|
job: result,
|
|
|
|
host: userDatabase.host
|
2015-12-07 16:40:51 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function handleResponse(err, result) {
|
|
|
|
if ( err ) {
|
2015-12-23 21:55:49 +08:00
|
|
|
return handleException(err, res);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( req.profiler ) {
|
|
|
|
req.profiler.done('enqueueJob');
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.settings.api_hostname) {
|
|
|
|
res.header('X-Served-By-Host', global.settings.api_hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', result.host);
|
|
|
|
}
|
2015-12-07 18:29:55 +08:00
|
|
|
res.send(result.job);
|
2015-12-07 16:40:51 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = JobController;
|