2015-12-16 22:57:58 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var PSQL = require('cartodb-psql');
|
2015-12-17 01:13:48 +08:00
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2015-12-16 22:57:58 +08:00
|
|
|
|
|
|
|
function Job() {
|
2015-12-17 01:13:48 +08:00
|
|
|
EventEmitter.call(this);
|
2015-12-16 22:57:58 +08:00
|
|
|
}
|
2015-12-17 01:13:48 +08:00
|
|
|
util.inherits(Job, EventEmitter);
|
2015-12-16 22:57:58 +08:00
|
|
|
|
|
|
|
Job.prototype.run = function (userDatabaseMetada, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var pg = new PSQL(userDatabaseMetada, {}, { destroyOnError: true });
|
|
|
|
|
|
|
|
this.getJob(pg, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!job) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.setJobRunning(pg, job, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2015-12-17 01:13:48 +08:00
|
|
|
self.emit('job:running', job);
|
|
|
|
|
2015-12-16 22:57:58 +08:00
|
|
|
self.runJob(pg, job, function (err, jobResult) {
|
|
|
|
if (err) {
|
|
|
|
return self.setJobFailed(pg, job, err.message, function () {
|
2015-12-17 01:13:48 +08:00
|
|
|
self.emit('job:failed', { job: job.job_id, error: err });
|
2015-12-16 22:57:58 +08:00
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
self.setJobDone(pg, job, function () {
|
2015-12-17 01:13:48 +08:00
|
|
|
self.emit('job:done', job);
|
2015-12-16 22:57:58 +08:00
|
|
|
callback(null, jobResult);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-17 01:13:48 +08:00
|
|
|
Job.prototype.createJob = function (pg, username, sql, callback) {
|
|
|
|
var persistJobQuery = [
|
|
|
|
'INSERT INTO cdb_jobs (',
|
|
|
|
'user_id, query',
|
|
|
|
') VALUES (',
|
|
|
|
'\'' + username + '\', ',
|
|
|
|
'\'' + sql + '\' ',
|
|
|
|
') RETURNING job_id;'
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
pg.query(persistJobQuery, callback);
|
|
|
|
};
|
|
|
|
|
2015-12-16 22:57:58 +08:00
|
|
|
Job.prototype.getJob = function (pg, callback) {
|
|
|
|
var getNextJob = "SELECT * FROM cdb_jobs WHERE status='pending' ORDER BY updated_at ASC LIMIT 1";
|
|
|
|
|
|
|
|
pg.query(getNextJob, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, result.rows[0]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Job.prototype.runJob = function (pg, job, callback) {
|
|
|
|
var query = job.query;
|
|
|
|
|
|
|
|
pg.query(query, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
Job.prototype.setJobRunning = function (pg, job, callback) {
|
|
|
|
var runningJobQuery = [
|
|
|
|
'UPDATE cdb_jobs SET ',
|
|
|
|
'status = \'running\', ',
|
|
|
|
'updated_at = now() ',
|
|
|
|
' WHERE ',
|
|
|
|
'job_id = \'' + job.job_id + '\' ',
|
|
|
|
' RETURNING job_id;'
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
pg.query(runningJobQuery, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
Job.prototype.setJobDone = function (pg, job, callback) {
|
|
|
|
var doneJobQuery = [
|
|
|
|
'UPDATE cdb_jobs SET ',
|
|
|
|
'status = \'done\', ',
|
|
|
|
'updated_at = now() ',
|
|
|
|
' WHERE ',
|
|
|
|
'job_id = \'' + job.job_id + '\' ',
|
|
|
|
' RETURNING job_id;'
|
|
|
|
].join('\n');
|
|
|
|
|
2015-12-17 01:13:48 +08:00
|
|
|
pg.query(doneJobQuery, callback);
|
2015-12-16 22:57:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Job.prototype.setJobFailed = function (pg, job, message, callback) {
|
|
|
|
var failedJobQuery = [
|
|
|
|
'UPDATE cdb_jobs SET ',
|
|
|
|
'status = \'failed\', ',
|
|
|
|
'failed_reason = \'' + message + '\', ',
|
|
|
|
'updated_at = now() ',
|
|
|
|
' WHERE ',
|
|
|
|
'job_id = \'' + job.job_id + '\' ',
|
|
|
|
' RETURNING job_id;'
|
|
|
|
].join('\n');
|
|
|
|
|
2015-12-17 01:13:48 +08:00
|
|
|
pg.query(failedJobQuery, callback);
|
2015-12-16 22:57:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Job;
|