2015-12-31 03:16:18 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var PSQL = require('cartodb-psql');
|
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
function JobCanceller(metadataBackend, userDatabaseMetadataService, jobBackend) {
|
2015-12-31 03:16:18 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userDatabaseMetadataService = userDatabaseMetadataService;
|
2016-01-08 22:47:59 +08:00
|
|
|
this.jobBackend = jobBackend;
|
2015-12-31 03:16:18 +08:00
|
|
|
}
|
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
JobCanceller.prototype.cancel = function (job_id, callback) {
|
2015-12-31 03:16:18 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
self.jobBackend.get(job_id, function (err, job) {
|
2015-12-31 03:16:18 +08:00
|
|
|
if (err) {
|
2016-01-08 22:47:59 +08:00
|
|
|
return callback(err);
|
2015-12-31 03:16:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (job.status === 'pending') {
|
2016-01-08 22:47:59 +08:00
|
|
|
return self.jobBackend.setCancelled(job, callback);
|
2015-12-31 03:16:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (job.status !== 'running') {
|
2016-01-08 23:24:53 +08:00
|
|
|
return callback(new Error('Job is ' + job.status + ', cancel is not allowed'));
|
2015-12-31 03:16:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
self.userDatabaseMetadataService.getUserMetadata(job.user, function (err, userDatabaseMetadata) {
|
|
|
|
if (err) {
|
2016-01-08 22:47:59 +08:00
|
|
|
return callback(err);
|
2015-12-31 03:16:18 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 23:25:25 +08:00
|
|
|
self._query(job, userDatabaseMetadata, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobBackend.setCancelled(job, callback);
|
|
|
|
});
|
2016-01-08 22:47:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-12-31 03:16:18 +08:00
|
|
|
|
2016-01-13 23:25:25 +08:00
|
|
|
JobCanceller.prototype.drain = function (job_id, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.cancel(job_id, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobBackend.setPending(job, callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
JobCanceller.prototype._query = function (job, userDatabaseMetadata, callback) {
|
|
|
|
var pg = new PSQL(userDatabaseMetadata, {}, { destroyOnError: true });
|
|
|
|
var getPIDQuery = 'SELECT pid FROM pg_stat_activity WHERE query = \'' + job.query +
|
|
|
|
' /* ' + job.job_id + ' */\'';
|
|
|
|
|
|
|
|
pg.query(getPIDQuery, function(err, result) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-12-31 03:16:18 +08:00
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
if (!result.rows[0] || !result.rows[0].pid) {
|
|
|
|
return callback(new Error('Query not running currently'));
|
|
|
|
}
|
2015-12-31 03:16:18 +08:00
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
var pid = result.rows[0].pid;
|
2016-01-13 23:25:25 +08:00
|
|
|
var cancelQuery = 'SELECT pg_cancel_backend(' + pid + ')';
|
2015-12-31 03:16:18 +08:00
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
pg.query(cancelQuery, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-12-31 03:16:18 +08:00
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
var isCancelled = result.rows[0].pg_cancel_backend;
|
2015-12-31 03:16:18 +08:00
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
if (!isCancelled) {
|
|
|
|
return callback(new Error('Query has not been cancelled'));
|
|
|
|
}
|
2015-12-31 03:16:18 +08:00
|
|
|
|
2016-01-08 22:47:59 +08:00
|
|
|
callback(null, job);
|
2015-12-31 03:16:18 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = JobCanceller;
|