2015-12-22 02:57:10 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var uuid = require('node-uuid');
|
|
|
|
|
|
|
|
function JobBackend(metadataBackend) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.db = 5;
|
|
|
|
}
|
|
|
|
util.inherits(JobBackend, EventEmitter);
|
|
|
|
|
|
|
|
JobBackend.prototype.create = function (username, sql, callback) {
|
|
|
|
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-24 00:29:11 +08:00
|
|
|
self.get(job_id, callback);
|
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;
|