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');
|
2016-01-04 22:20:06 +08:00
|
|
|
var JOBS_TTL_AFTER_RESOLUTION = 48 * 3600;
|
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;
|
2015-12-29 22:46:04 +08:00
|
|
|
this.redisPrefix = 'batch:jobs:';
|
2015-12-22 02:57:10 +08:00
|
|
|
}
|
|
|
|
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-29 22:46:04 +08:00
|
|
|
this.redisPrefix + 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-31 22:42:31 +08:00
|
|
|
JobBackend.prototype.update = function (job_id, sql, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.get(job_id, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job.status !== 'pending') {
|
|
|
|
return callback(new Error('Job is not pending, it couldn\'t be updated'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var now = new Date().toISOString();
|
|
|
|
var redisParams = [
|
|
|
|
self.redisPrefix + job_id,
|
|
|
|
'query', sql,
|
|
|
|
'updated_at', now
|
|
|
|
];
|
|
|
|
|
|
|
|
self.metadataBackend.redisCmd(self.db, 'HMSET', redisParams , function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.get(job_id, callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-25 00:42:49 +08:00
|
|
|
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) {
|
2016-01-05 02:08:13 +08:00
|
|
|
jobsQueue.defer(self._getForList.bind(self), job_id, username);
|
2015-12-25 00:42:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
jobsQueue.awaitAll(function (err, jobs) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-01-05 02:08:13 +08:00
|
|
|
|
|
|
|
callback(null, jobs.filter(function (job) {
|
|
|
|
return job ? true : false;
|
|
|
|
}));
|
2015-12-25 00:42:49 +08:00
|
|
|
});
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-01-05 02:08:13 +08:00
|
|
|
JobBackend.prototype._getForList = function (job_id, username, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.get(job_id, function (err, job) {
|
|
|
|
|
|
|
|
if (err && err.name === 'NotFoundError') {
|
|
|
|
return self.userIndexer.remove(username, job_id, function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error removing key %s in user set', job_id, err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, job);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-29 22:46:04 +08:00
|
|
|
this.redisPrefix + 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)) {
|
2016-01-05 02:08:13 +08:00
|
|
|
var notFoundError = new Error('Job with id ' + job_id + ' not found');
|
|
|
|
notFoundError.name = 'NotFoundError';
|
|
|
|
return callback(notFoundError);
|
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-29 22:46:04 +08:00
|
|
|
this.redisPrefix + 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;
|
2016-01-04 22:20:06 +08:00
|
|
|
var redisKey = this.redisPrefix + job.job_id;
|
2015-12-22 02:57:10 +08:00
|
|
|
var redisParams = [
|
2016-01-04 22:20:06 +08:00
|
|
|
redisKey,
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-04 22:20:06 +08:00
|
|
|
self.metadataBackend.redisCmd(self.db, 'EXPIRE', [ redisKey, JOBS_TTL_AFTER_RESOLUTION ], function (err) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('done', job);
|
|
|
|
});
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBackend.prototype.setFailed = function (job, err) {
|
|
|
|
var self = this;
|
2016-01-04 22:20:06 +08:00
|
|
|
var redisKey = this.redisPrefix + job.job_id;
|
2015-12-22 02:57:10 +08:00
|
|
|
var redisParams = [
|
2016-01-04 22:20:06 +08:00
|
|
|
redisKey,
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-05 02:08:13 +08:00
|
|
|
self.metadataBackend.redisCmd(self.db, 'EXPIRE', [ redisKey, JOBS_TTL_AFTER_RESOLUTION ], function (err) {
|
2016-01-04 22:20:06 +08:00
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('failed', job);
|
|
|
|
});
|
2015-12-22 02:57:10 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-31 03:16:18 +08:00
|
|
|
JobBackend.prototype.setCancelled = function (job) {
|
|
|
|
var self = this;
|
2016-01-04 22:20:06 +08:00
|
|
|
var redisKey = this.redisPrefix + job.job_id;
|
2015-12-31 03:16:18 +08:00
|
|
|
var redisParams = [
|
2016-01-04 22:20:06 +08:00
|
|
|
redisKey,
|
2015-12-31 03:16:18 +08:00
|
|
|
'status', 'cancelled',
|
|
|
|
'updated_at', new Date().toISOString()
|
|
|
|
];
|
|
|
|
|
|
|
|
this.metadataBackend.redisCmd(this.db, 'HMSET', redisParams , function (err) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
2016-01-05 02:08:13 +08:00
|
|
|
self.metadataBackend.redisCmd(self.db, 'EXPIRE', [ redisKey, JOBS_TTL_AFTER_RESOLUTION ], function (err) {
|
2016-01-04 22:20:06 +08:00
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('cancelled', job);
|
|
|
|
});
|
|
|
|
|
2015-12-31 03:16:18 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-12-22 02:57:10 +08:00
|
|
|
module.exports = JobBackend;
|