2015-12-22 02:57:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var uuid = require('node-uuid');
|
2015-12-25 00:42:49 +08:00
|
|
|
var queue = require('queue-async');
|
2015-12-22 02:57:10 +08:00
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
function JobBackend(metadataBackend, jobQueueProducer, jobPublisher, userIndexer) {
|
2015-12-22 02:57:10 +08:00
|
|
|
EventEmitter.call(this);
|
|
|
|
this.metadataBackend = metadataBackend;
|
2015-12-25 00:42:49 +08:00
|
|
|
this.jobQueueProducer = jobQueueProducer;
|
|
|
|
this.jobPublisher = jobPublisher;
|
|
|
|
this.userIndexer = userIndexer;
|
2015-12-22 02:57:10 +08:00
|
|
|
this.db = 5;
|
|
|
|
}
|
|
|
|
util.inherits(JobBackend, EventEmitter);
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
JobBackend.prototype.create = function (username, sql, host, callback) {
|
2015-12-22 02:57:10 +08:00
|
|
|
var self = this;
|
2015-12-24 00:29:11 +08:00
|
|
|
var job_id = uuid.v4();
|
|
|
|
var now = new Date().toISOString();
|
2015-12-22 02:57:10 +08:00
|
|
|
var redisParams = [
|
2015-12-24 00:29:11 +08:00
|
|
|
'job:' + job_id,
|
2015-12-22 02:57:10 +08:00
|
|
|
'user', username,
|
|
|
|
'status', 'pending',
|
|
|
|
'query', sql,
|
2015-12-24 00:29:11 +08:00
|
|
|
'created_at', now,
|
|
|
|
'updated_at', now
|
2015-12-22 02:57:10 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
this.metadataBackend.redisCmd(this.db, 'HMSET', redisParams , function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
self.jobQueueProducer.enqueue(job_id, host, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// broadcast to consumers
|
|
|
|
self.jobPublisher.publish(host);
|
|
|
|
|
|
|
|
self.userIndexer.add(username, job_id, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.get(job_id, callback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBackend.prototype.list = function (username, callback) {
|
|
|
|
var self = this;
|
|
|
|
this.userIndexer.list(username, function (err, job_ids) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var jobsQueue = queue(job_ids.length);
|
|
|
|
|
|
|
|
job_ids.forEach(function(job_id) {
|
|
|
|
jobsQueue.defer(self.get.bind(self), job_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
jobsQueue.awaitAll(function (err, jobs) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback(null, jobs);
|
|
|
|
});
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
JobBackend.prototype.get = function (job_id, callback) {
|
2015-12-22 02:57:10 +08:00
|
|
|
var redisParams = [
|
2015-12-24 00:29:11 +08:00
|
|
|
'job:' + job_id,
|
2015-12-22 02:57:10 +08:00
|
|
|
'user',
|
|
|
|
'status',
|
|
|
|
'query',
|
|
|
|
'created_at',
|
2015-12-24 00:29:11 +08:00
|
|
|
'updated_at',
|
|
|
|
'failed_reason'
|
2015-12-22 02:57:10 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
this.metadataBackend.redisCmd(this.db, 'HMGET', redisParams , function (err, jobValues) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2015-12-24 00:29:11 +08:00
|
|
|
function isJobFound(jobValues) {
|
|
|
|
return jobValues[0] && jobValues[1] && jobValues[2] && jobValues[3] && jobValues[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isJobFound(jobValues)) {
|
|
|
|
return callback(new Error('Job with id ' + job_id + ' not found'));
|
2015-12-22 02:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, {
|
2015-12-24 00:29:11 +08:00
|
|
|
job_id: job_id,
|
2015-12-22 02:57:10 +08:00
|
|
|
user: jobValues[0],
|
|
|
|
status: jobValues[1],
|
|
|
|
query: jobValues[2],
|
|
|
|
created_at: jobValues[3],
|
2015-12-24 00:29:11 +08:00
|
|
|
updated_at: jobValues[4],
|
|
|
|
failed_reason: jobValues[5] ? jobValues[5] : undefined
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBackend.prototype.setRunning = function (job) {
|
|
|
|
var self = this;
|
|
|
|
var redisParams = [
|
2015-12-24 00:29:11 +08:00
|
|
|
'job:' + job.job_id,
|
2015-12-22 02:57:10 +08:00
|
|
|
'status', 'running',
|
2015-12-24 00:29:11 +08:00
|
|
|
'updated_at', new Date().toISOString()
|
2015-12-22 02:57:10 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
this.metadataBackend.redisCmd(this.db, 'HMSET', redisParams, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('running', job);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBackend.prototype.setDone = function (job) {
|
|
|
|
var self = this;
|
|
|
|
var redisParams = [
|
2015-12-24 00:29:11 +08:00
|
|
|
'job:' + job.job_id,
|
2015-12-22 02:57:10 +08:00
|
|
|
'status', 'done',
|
2015-12-24 00:29:11 +08:00
|
|
|
'updated_at', new Date().toISOString()
|
2015-12-22 02:57:10 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
this.metadataBackend.redisCmd(this.db, 'HMSET', redisParams , function (err) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('done', job);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBackend.prototype.setFailed = function (job, err) {
|
|
|
|
var self = this;
|
|
|
|
var redisParams = [
|
2015-12-24 00:29:11 +08:00
|
|
|
'job:' + job.job_id,
|
2015-12-22 02:57:10 +08:00
|
|
|
'status', 'failed',
|
|
|
|
'failed_reason', err.message,
|
2015-12-24 00:29:11 +08:00
|
|
|
'updated_at', new Date().toISOString()
|
2015-12-22 02:57:10 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
this.metadataBackend.redisCmd(this.db, 'HMSET', redisParams , function (err) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('failed', job);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = JobBackend;
|