2016-10-13 03:32:29 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var asyncQ = require('queue-async');
|
|
|
|
var debug = require('../util/debug')('queue-mover');
|
|
|
|
var forever = require('../util/forever');
|
|
|
|
|
|
|
|
var QUEUE = {
|
|
|
|
OLD: {
|
|
|
|
DB: 5,
|
|
|
|
PREFIX: 'batch:queues:' // host
|
|
|
|
},
|
|
|
|
NEW: {
|
|
|
|
DB: 5,
|
|
|
|
PREFIX: 'batch:queue:' // user
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function HostUserQueueMover (jobQueue, jobService, locker, redisPool) {
|
2016-10-13 03:32:29 +08:00
|
|
|
this.jobQueue = jobQueue;
|
|
|
|
this.jobService = jobService;
|
|
|
|
this.locker = locker;
|
2016-10-17 21:02:34 +08:00
|
|
|
this.pool = redisPool;
|
2016-10-13 03:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = HostUserQueueMover;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
HostUserQueueMover.prototype.moveOldJobs = function (callback) {
|
2016-10-13 03:32:29 +08:00
|
|
|
var self = this;
|
2019-12-24 01:19:08 +08:00
|
|
|
this.getOldQueues(function (err, hosts) {
|
2016-10-13 03:32:29 +08:00
|
|
|
var async = asyncQ(4);
|
2019-12-24 01:19:08 +08:00
|
|
|
hosts.forEach(function (host) {
|
2016-10-13 03:32:29 +08:00
|
|
|
async.defer(self.moveOldQueueJobs.bind(self), host);
|
|
|
|
});
|
|
|
|
|
|
|
|
async.awaitAll(function (err) {
|
|
|
|
if (err) {
|
|
|
|
debug('Something went wrong moving jobs', err);
|
|
|
|
} else {
|
|
|
|
debug('Finished moving all jobs');
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
HostUserQueueMover.prototype.moveOldQueueJobs = function (host, callback) {
|
2016-10-13 03:32:29 +08:00
|
|
|
var self = this;
|
|
|
|
// do forever, it does not throw a stack overflow
|
|
|
|
forever(
|
|
|
|
function (next) {
|
2019-12-24 01:19:08 +08:00
|
|
|
self.locker.lock(host, function (err) {
|
2016-10-13 03:32:29 +08:00
|
|
|
// we didn't get the lock for the host
|
|
|
|
if (err) {
|
|
|
|
debug('Could not lock host=%s. Reason: %s', host, err.message);
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
debug('Locked host=%s', host);
|
|
|
|
self.processNextJob(host, next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
debug(err.name === 'EmptyQueue' ? err.message : err);
|
|
|
|
}
|
|
|
|
self.locker.unlock(host, callback);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// this.metadataBackend.redisCmd(QUEUE.DB, 'RPOP', [ QUEUE.PREFIX + user ], callback);
|
2016-10-13 03:32:29 +08:00
|
|
|
|
|
|
|
HostUserQueueMover.prototype.processNextJob = function (host, callback) {
|
|
|
|
var self = this;
|
2019-12-24 01:19:08 +08:00
|
|
|
this.pool.acquire(QUEUE.OLD.DB, function (err, client) {
|
2016-10-13 03:32:29 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
client.lpop(QUEUE.OLD.PREFIX + host, function (err, jobId) {
|
2016-10-17 18:51:01 +08:00
|
|
|
self.pool.release(QUEUE.OLD.DB, client);
|
2016-10-13 03:32:29 +08:00
|
|
|
debug('Found jobId=%s at queue=%s', jobId, host);
|
|
|
|
if (!jobId) {
|
|
|
|
var emptyQueueError = new Error('Empty queue');
|
|
|
|
emptyQueueError.name = 'EmptyQueue';
|
|
|
|
return callback(emptyQueueError);
|
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
self.jobService.get(jobId, function (err, job) {
|
2016-10-13 03:32:29 +08:00
|
|
|
if (err) {
|
|
|
|
debug(err);
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
if (job) {
|
2019-12-24 01:19:08 +08:00
|
|
|
return self.jobQueue.enqueueFirst(job.data.user, jobId, function () {
|
2016-10-13 03:32:29 +08:00
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
HostUserQueueMover.prototype.getOldQueues = function (callback) {
|
2016-10-13 03:32:29 +08:00
|
|
|
var initialCursor = ['0'];
|
|
|
|
var hosts = {};
|
|
|
|
var self = this;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
this.pool.acquire(QUEUE.OLD.DB, function (err, client) {
|
2016-10-13 03:32:29 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
self._getOldQueues(client, initialCursor, hosts, function (err, hosts) {
|
2016-10-17 18:51:01 +08:00
|
|
|
self.pool.release(QUEUE.DB, client);
|
|
|
|
return callback(err, Object.keys(hosts));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-10-13 03:32:29 +08:00
|
|
|
|
2016-10-17 18:51:01 +08:00
|
|
|
HostUserQueueMover.prototype._getOldQueues = function (client, cursor, hosts, callback) {
|
|
|
|
var self = this;
|
|
|
|
var redisParams = [cursor[0], 'MATCH', QUEUE.OLD.PREFIX + '*'];
|
2016-10-13 03:32:29 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
client.scan(redisParams, function (err, currentCursor) {
|
2016-10-17 18:51:01 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(null, hosts);
|
|
|
|
}
|
2016-10-13 03:32:29 +08:00
|
|
|
|
2016-10-17 18:51:01 +08:00
|
|
|
var queues = currentCursor[1];
|
|
|
|
if (queues) {
|
2016-10-13 03:32:29 +08:00
|
|
|
queues.forEach(function (queue) {
|
2016-10-17 18:51:01 +08:00
|
|
|
var user = queue.substr(QUEUE.OLD.PREFIX.length);
|
|
|
|
hosts[user] = true;
|
2016-10-13 03:32:29 +08:00
|
|
|
});
|
2016-10-17 18:51:01 +08:00
|
|
|
}
|
2016-10-13 03:32:29 +08:00
|
|
|
|
2016-10-17 18:51:01 +08:00
|
|
|
var hasMore = currentCursor[0] !== '0';
|
|
|
|
if (!hasMore) {
|
|
|
|
return callback(null, hosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
self._getOldQueues(client, currentCursor, hosts, callback);
|
2016-10-13 03:32:29 +08:00
|
|
|
});
|
|
|
|
};
|