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) {
|
2019-12-27 01:12:47 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
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;
|
2020-05-18 17:32:41 +08:00
|
|
|
this.pool.acquire(QUEUE.OLD.DB)
|
|
|
|
.then(client => {
|
|
|
|
client.lpop(QUEUE.OLD.PREFIX + host, function (err, jobId) {
|
|
|
|
self.pool.release(QUEUE.OLD.DB, client)
|
|
|
|
.then(() => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('Found jobId=%s at queue=%s', jobId, host);
|
|
|
|
if (!jobId) {
|
|
|
|
var emptyQueueError = new Error('Empty queue');
|
|
|
|
emptyQueueError.name = 'EmptyQueue';
|
|
|
|
return callback(emptyQueueError);
|
|
|
|
}
|
|
|
|
self.jobService.get(jobId, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
debug(err);
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
if (job) {
|
|
|
|
return self.jobQueue.enqueueFirst(job.data.user, jobId, function () {
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => callback(err));
|
2016-10-13 03:32:29 +08:00
|
|
|
});
|
2020-05-18 17:32:41 +08:00
|
|
|
})
|
|
|
|
.catch(err => callback(err));
|
2016-10-13 03:32:29 +08:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2020-05-18 17:32:41 +08:00
|
|
|
this.pool.acquire(QUEUE.OLD.DB)
|
|
|
|
.then(client => {
|
|
|
|
self._getOldQueues(client, initialCursor, hosts, function (err, hosts) {
|
|
|
|
self.pool.release(QUEUE.DB, client)
|
|
|
|
.then(() => err ? callback(err) : callback(null, Object.keys(hosts)))
|
|
|
|
.catch(err => callback(err));
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => callback(err));
|
2016-10-17 18:51:01 +08:00
|
|
|
};
|
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
|
|
|
});
|
|
|
|
};
|