CartoDB-SQL-API/app/services/stream_copy.js

105 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2018-05-25 23:42:30 +08:00
const PSQL = require('cartodb-psql');
const copyTo = require('pg-copy-streams').to;
const copyFrom = require('pg-copy-streams').from;
const ACTION_TO = 'to';
const ACTION_FROM = 'from';
const DEFAULT_TIMEOUT = "'5h'";
const cancelQuery = pid => `SELECT pg_cancel_backend(${pid}) as cancelled`;
const terminateQuery = pid => `SELECT pg_terminate_backend(${pid}) as terminated`;
const timeoutQuery = timeout => `SET statement_timeout=${timeout}`;
module.exports = class StreamCopy {
constructor(sql, userDbParams, logger) {
this.dbParams = Object.assign({}, userDbParams, {
2018-07-20 16:22:54 +08:00
port: global.settings.db_batch_port || userDbParams.port
});
2018-06-12 22:56:18 +08:00
this.sql = sql;
2018-06-21 20:53:07 +08:00
this.stream = null;
this.timeout = global.settings.copy_timeout || DEFAULT_TIMEOUT;
this.logger = logger;
2018-06-12 22:56:18 +08:00
}
static get ACTION_TO() {
return ACTION_TO;
}
static get ACTION_FROM() {
return ACTION_FROM;
}
getPGStream(action, callback) {
const pg = new PSQL(this.dbParams);
pg.connect((err, client, done) => {
2018-06-08 22:58:32 +08:00
if (err) {
return callback(err);
2018-06-08 22:58:32 +08:00
}
client.query(timeoutQuery(this.timeout), (err) => {
2018-07-20 22:24:33 +08:00
if (err) {
return callback(err);
2018-07-20 22:24:33 +08:00
}
2019-05-24 21:39:56 +08:00
this.clientProcessID = client.processID;
2019-05-28 01:24:48 +08:00
this.stream = action === ACTION_TO ? copyTo(this.sql) : copyFrom(this.sql);
2018-07-20 22:24:33 +08:00
const pgstream = client.query(this.stream);
if (action === ACTION_TO) {
pgstream.on('end', () => done());
pgstream.on('error', () => this._cancel(client.processID, action));
} else if (action === ACTION_FROM) {
pgstream.on('finish', () => done());
pgstream.on('error', err => client.connection.sendCopyFail(err.message));
}
pgstream.on('error', err => done(err));
2018-07-20 22:24:33 +08:00
callback(null, pgstream);
2018-07-20 22:24:33 +08:00
});
2018-06-08 22:50:12 +08:00
});
2018-05-25 23:42:30 +08:00
}
2018-06-21 20:53:07 +08:00
getRowCount() {
return this.stream.rowCount;
}
2019-05-28 01:24:48 +08:00
_cancel (pid, action) {
const pg = new PSQL(this.dbParams);
2019-05-28 01:24:48 +08:00
const actionType = action === ACTION_TO ? ACTION_TO : ACTION_FROM;
pg.query(cancelQuery(pid), (err, result) => {
if (err) {
return this.logger.error(err);
}
2019-05-16 18:19:03 +08:00
const isCancelled = result.rows.length && result.rows[0].cancelled;
if (isCancelled) {
return this.logger.info(`Canceled "copy ${actionType}" stream query successfully (pid: ${pid})`);
}
return pg.query(terminateQuery(pid), (err, result) => {
if (err) {
return this.logger.error(err);
}
const isTerminated = result.rows.length && result.rows[0].terminated;
if (!isTerminated) {
return this.logger.error(
new Error(`Unable to terminate "copy ${actionType}" stream query (pid: ${pid})`)
);
}
return this.logger.info(`Terminated "copy ${actionType}" stream query successfully (pid: ${pid})`);
});
});
}
2018-05-26 00:50:56 +08:00
};