2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
const util = require('util');
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2019-10-04 01:35:18 +08:00
|
|
|
const bodyParser = require('../middlewares/body-parser');
|
|
|
|
const user = require('../middlewares/user');
|
|
|
|
const authorization = require('../middlewares/authorization');
|
|
|
|
const connectionParams = require('../middlewares/connection-params');
|
|
|
|
const rateLimits = require('../middlewares/rate-limit');
|
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimits;
|
2019-07-29 22:24:48 +08:00
|
|
|
const params = require('../middlewares/params');
|
2020-06-30 23:42:59 +08:00
|
|
|
const tag = require('../middlewares/tag');
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2019-10-01 16:59:44 +08:00
|
|
|
module.exports = class JobController {
|
|
|
|
constructor (metadataBackend, userDatabaseService, jobService, statsdClient, userLimitsService) {
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userDatabaseService = userDatabaseService;
|
|
|
|
this.jobService = jobService;
|
|
|
|
this.statsdClient = statsdClient;
|
|
|
|
this.userLimitsService = userLimitsService;
|
|
|
|
}
|
2016-05-24 20:28:00 +08:00
|
|
|
|
2019-10-02 22:02:13 +08:00
|
|
|
route (sqlRouter) {
|
2019-10-01 16:59:44 +08:00
|
|
|
const jobMiddlewares = composeJobMiddlewares(
|
|
|
|
this.metadataBackend,
|
|
|
|
this.userDatabaseService,
|
|
|
|
this.jobService,
|
|
|
|
this.statsdClient,
|
|
|
|
this.userLimitsService
|
|
|
|
);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2019-10-02 22:02:13 +08:00
|
|
|
sqlRouter.post('/job', [
|
2020-06-30 23:42:59 +08:00
|
|
|
tag({ tags: ['job', 'create'] }),
|
2019-10-04 01:35:18 +08:00
|
|
|
bodyParser(),
|
2019-10-01 16:59:44 +08:00
|
|
|
checkBodyPayloadSize(),
|
|
|
|
params({ strategy: 'job' }),
|
2020-06-30 23:42:59 +08:00
|
|
|
jobMiddlewares(createJob, RATE_LIMIT_ENDPOINTS_GROUPS.JOB_CREATE)
|
2019-10-02 22:02:13 +08:00
|
|
|
]);
|
2019-10-01 18:25:22 +08:00
|
|
|
|
2019-10-02 22:02:13 +08:00
|
|
|
sqlRouter.get('/job/:job_id', [
|
2020-06-30 23:42:59 +08:00
|
|
|
tag({ tags: ['job', 'retrieve'] }),
|
2019-10-04 01:35:18 +08:00
|
|
|
bodyParser(),
|
2020-06-30 23:42:59 +08:00
|
|
|
jobMiddlewares(getJob, RATE_LIMIT_ENDPOINTS_GROUPS.JOB_GET)
|
2019-10-02 22:02:13 +08:00
|
|
|
]);
|
2019-10-01 18:25:22 +08:00
|
|
|
|
2019-10-02 22:02:13 +08:00
|
|
|
sqlRouter.delete('/job/:job_id', [
|
2020-06-30 23:42:59 +08:00
|
|
|
tag({ tags: ['job', 'cancel'] }),
|
2019-10-04 01:35:18 +08:00
|
|
|
bodyParser(),
|
2020-06-30 23:42:59 +08:00
|
|
|
jobMiddlewares(cancelJob, RATE_LIMIT_ENDPOINTS_GROUPS.JOB_DELETE)
|
2019-10-02 22:02:13 +08:00
|
|
|
]);
|
2019-10-01 16:59:44 +08:00
|
|
|
}
|
2015-12-31 03:16:18 +08:00
|
|
|
};
|
|
|
|
|
2020-02-27 00:19:07 +08:00
|
|
|
function composeJobMiddlewares (metadataBackend, userDatabaseService, jobService, statsdClient, userLimitsService, pubSubMetricsService) {
|
2020-06-30 23:42:59 +08:00
|
|
|
return function jobMiddlewares (job, endpointGroup) {
|
2018-06-05 19:14:50 +08:00
|
|
|
const forceToBeMaster = true;
|
2018-02-20 23:25:16 +08:00
|
|
|
|
|
|
|
return [
|
2019-10-04 01:35:18 +08:00
|
|
|
user(metadataBackend),
|
|
|
|
rateLimits(userLimitsService, endpointGroup),
|
|
|
|
authorization(metadataBackend, forceToBeMaster),
|
|
|
|
connectionParams(userDatabaseService),
|
|
|
|
job(jobService),
|
2018-02-20 23:25:16 +08:00
|
|
|
setServedByDBHostHeader(),
|
|
|
|
incrementSuccessMetrics(statsdClient),
|
2018-02-23 20:03:56 +08:00
|
|
|
sendResponse(),
|
2019-11-12 17:11:05 +08:00
|
|
|
incrementErrorMetrics(statsdClient)
|
2018-02-20 23:25:16 +08:00
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
function cancelJob (jobService) {
|
|
|
|
return function cancelJobMiddleware (req, res, next) {
|
2019-12-27 01:28:01 +08:00
|
|
|
const { job_id: jobId } = req.params;
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2019-12-27 01:28:01 +08:00
|
|
|
jobService.cancel(jobId, (err, job) => {
|
2020-06-30 23:42:59 +08:00
|
|
|
req.profiler.done('cancelJob');
|
2018-02-19 21:42:52 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-12-25 00:42:49 +08:00
|
|
|
|
2018-02-23 20:03:56 +08:00
|
|
|
res.body = job.serialize();
|
2018-02-17 01:21:06 +08:00
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
2016-10-04 22:07:13 +08:00
|
|
|
};
|
2018-02-17 01:21:06 +08:00
|
|
|
}
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
function getJob (jobService) {
|
|
|
|
return function getJobMiddleware (req, res, next) {
|
2019-12-27 01:28:01 +08:00
|
|
|
const { job_id: jobId } = req.params;
|
2018-02-19 18:04:28 +08:00
|
|
|
|
2019-12-27 01:28:01 +08:00
|
|
|
jobService.get(jobId, (err, job) => {
|
2020-06-30 23:42:59 +08:00
|
|
|
req.profiler.done('getJob');
|
2018-02-19 21:42:52 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-12-07 16:40:51 +08:00
|
|
|
|
2018-02-23 20:03:56 +08:00
|
|
|
res.body = job.serialize();
|
2016-10-28 21:08:42 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2016-10-28 21:08:42 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
function createJob (jobService) {
|
|
|
|
return function createJobMiddleware (req, res, next) {
|
2020-06-30 23:42:59 +08:00
|
|
|
const { logger } = res.locals;
|
|
|
|
const data = {
|
2018-02-17 01:21:06 +08:00
|
|
|
user: res.locals.user,
|
2019-07-26 22:06:53 +08:00
|
|
|
query: res.locals.params.sql,
|
2018-02-17 01:21:06 +08:00
|
|
|
host: res.locals.userDbParams.host,
|
2018-03-14 02:08:51 +08:00
|
|
|
port: global.settings.db_batch_port || res.locals.userDbParams.port,
|
2018-02-17 01:21:06 +08:00
|
|
|
pass: res.locals.userDbParams.pass,
|
|
|
|
dbname: res.locals.userDbParams.dbname,
|
|
|
|
dbuser: res.locals.userDbParams.user
|
|
|
|
};
|
2016-10-28 21:08:42 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
jobService.create(data, (err, job) => {
|
2020-06-30 23:42:59 +08:00
|
|
|
req.profiler.done('createJob');
|
2018-02-19 21:42:52 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
logger.info({ job: job.toJSON() });
|
2018-02-17 01:21:06 +08:00
|
|
|
|
2018-02-23 20:03:56 +08:00
|
|
|
res.statusCode = 201;
|
|
|
|
res.body = job.serialize();
|
2018-02-17 01:21:06 +08:00
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkBodyPayloadSize () {
|
2019-12-24 01:19:08 +08:00
|
|
|
return function checkBodyPayloadSizeMiddleware (req, res, next) {
|
2018-02-17 01:21:06 +08:00
|
|
|
const payload = JSON.stringify(req.body);
|
|
|
|
|
|
|
|
if (payload.length > MAX_LIMIT_QUERY_SIZE_IN_BYTES) {
|
|
|
|
return next(new Error(getMaxSizeErrorMessage(payload)), res);
|
2016-10-28 21:08:42 +08:00
|
|
|
}
|
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
2016-10-28 21:08:42 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
const ONE_KILOBYTE_IN_BYTES = 1024;
|
|
|
|
const MAX_LIMIT_QUERY_SIZE_IN_KB = 16;
|
|
|
|
const MAX_LIMIT_QUERY_SIZE_IN_BYTES = MAX_LIMIT_QUERY_SIZE_IN_KB * ONE_KILOBYTE_IN_BYTES;
|
2016-01-07 19:06:01 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function getMaxSizeErrorMessage (sql) {
|
2018-02-17 01:21:06 +08:00
|
|
|
return util.format([
|
2019-12-24 01:19:08 +08:00
|
|
|
'Your payload is too large: %s bytes. Max size allowed is %s bytes (%skb).',
|
|
|
|
'Are you trying to import data?.',
|
|
|
|
'Please, check out import api http://docs.cartodb.com/cartodb-platform/import-api/'
|
|
|
|
].join(' '),
|
|
|
|
sql.length,
|
|
|
|
MAX_LIMIT_QUERY_SIZE_IN_BYTES,
|
|
|
|
Math.round(MAX_LIMIT_QUERY_SIZE_IN_BYTES / ONE_KILOBYTE_IN_BYTES)
|
2018-02-17 01:21:06 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.MAX_LIMIT_QUERY_SIZE_IN_BYTES = MAX_LIMIT_QUERY_SIZE_IN_BYTES;
|
|
|
|
module.exports.getMaxSizeErrorMessage = getMaxSizeErrorMessage;
|
|
|
|
|
|
|
|
function setServedByDBHostHeader () {
|
|
|
|
return function setServedByDBHostHeaderMiddleware (req, res, next) {
|
|
|
|
const { userDbParams } = res.locals;
|
|
|
|
|
|
|
|
if (userDbParams.host) {
|
|
|
|
res.header('X-Served-By-DB-Host', res.locals.userDbParams.host);
|
2016-10-04 22:07:13 +08:00
|
|
|
}
|
2016-05-27 01:44:59 +08:00
|
|
|
|
2018-02-17 01:21:06 +08:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const METRICS_PREFIX = 'sqlapi.job';
|
|
|
|
|
|
|
|
function incrementSuccessMetrics (statsdClient) {
|
|
|
|
return function incrementSuccessMetricsMiddleware (req, res, next) {
|
|
|
|
if (statsdClient !== undefined) {
|
|
|
|
statsdClient.increment(`${METRICS_PREFIX}.success`);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function incrementErrorMetrics (statsdClient) {
|
|
|
|
return function incrementErrorMetricsMiddleware (err, req, res, next) {
|
|
|
|
if (statsdClient !== undefined) {
|
|
|
|
statsdClient.increment(`${METRICS_PREFIX}.error`);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(err);
|
2016-10-04 22:07:13 +08:00
|
|
|
};
|
|
|
|
}
|
2018-02-23 20:03:56 +08:00
|
|
|
|
|
|
|
function sendResponse () {
|
2020-02-27 00:19:07 +08:00
|
|
|
return function sendResponseMiddleware (req, res, next) {
|
2018-02-23 20:03:56 +08:00
|
|
|
res.status(res.statusCode || 200).send(res.body);
|
2020-02-27 00:19:07 +08:00
|
|
|
|
|
|
|
next();
|
2018-02-23 20:03:56 +08:00
|
|
|
};
|
|
|
|
}
|