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;
|
|
|
|
|
2018-06-21 19:39:33 +08:00
|
|
|
const ACTION_TO = 'to';
|
|
|
|
const ACTION_FROM = 'from';
|
2018-07-20 21:32:32 +08:00
|
|
|
const DEFAULT_TIMEOUT = "'5h'";
|
2018-06-21 19:39:33 +08:00
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
const cancelQuery = pid => `SELECT pg_cancel_backend(${pid})`;
|
|
|
|
const timeoutQuery = timeout => `SET statement_timeout=${timeout}`;
|
2018-07-20 21:32:32 +08:00
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
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;
|
2018-07-20 21:32:32 +08:00
|
|
|
this.timeout = global.settings.copy_timeout || DEFAULT_TIMEOUT;
|
2019-05-14 21:39:54 +08:00
|
|
|
this.logger = logger;
|
2018-06-12 22:56:18 +08:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:39:33 +08:00
|
|
|
static get ACTION_TO() {
|
|
|
|
return ACTION_TO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static get ACTION_FROM() {
|
|
|
|
return ACTION_FROM;
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
getPGStream(action, callback) {
|
|
|
|
const pg = new PSQL(this.dbParams);
|
|
|
|
|
|
|
|
pg.connect((err, client, done) => {
|
2018-06-08 22:58:32 +08:00
|
|
|
if (err) {
|
2019-05-14 21:39:54 +08:00
|
|
|
return callback(err);
|
2018-06-08 22:58:32 +08:00
|
|
|
}
|
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
client.query(timeoutQuery(this.timeout), (err) => {
|
2018-07-20 22:24:33 +08:00
|
|
|
if (err) {
|
2019-05-14 21:39:54 +08:00
|
|
|
return callback(err);
|
2018-07-20 22:24:33 +08:00
|
|
|
}
|
2018-06-14 00:26:58 +08:00
|
|
|
|
2018-07-24 00:41:27 +08:00
|
|
|
const streamMaker = action === ACTION_TO ? copyTo : copyFrom;
|
2018-07-20 22:24:33 +08:00
|
|
|
this.stream = streamMaker(this.sql);
|
|
|
|
const pgstream = client.query(this.stream);
|
2018-06-21 21:16:47 +08:00
|
|
|
|
2018-07-20 22:24:33 +08:00
|
|
|
pgstream
|
2018-09-17 22:54:51 +08:00
|
|
|
.on('end', () => {
|
|
|
|
if(action === ACTION_TO) {
|
|
|
|
pgstream.connection.stream.resume();
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
})
|
2018-07-20 22:24:33 +08:00
|
|
|
.on('error', err => done(err))
|
2019-05-14 21:39:54 +08:00
|
|
|
.on('cancelQuery', () => this.cancel(client, action));
|
2018-07-20 22:24:33 +08:00
|
|
|
|
2019-05-14 21:39:54 +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 19:21:23 +08:00
|
|
|
|
2018-06-21 20:53:07 +08:00
|
|
|
getRowCount() {
|
|
|
|
return this.stream.rowCount;
|
2018-06-21 19:21:23 +08:00
|
|
|
}
|
2019-05-14 21:39:54 +08:00
|
|
|
|
|
|
|
cancel (client, action) {
|
|
|
|
const pid = client.processID;
|
|
|
|
const pg = new PSQL(this.dbParams);
|
|
|
|
|
|
|
|
pg.query(cancelQuery(pid), (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
return this.logger.error(err);
|
|
|
|
}
|
|
|
|
const actionType = action === ACTION_TO ? ACTION_TO : ACTION_FROM;
|
|
|
|
const isCancelled = result.rows[0].pg_cancel_backend;
|
|
|
|
|
|
|
|
if (!isCancelled) {
|
|
|
|
return this.logger.error(new Error(`Unable to cancel "copy ${actionType}" stream query (pid: ${pid})`));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.logger.info(`Canceled "copy ${actionType}" stream query successfully (pid: ${pid})`);
|
|
|
|
});
|
|
|
|
}
|
2018-05-26 00:50:56 +08:00
|
|
|
};
|