0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Added queue depth to requests (#19987)

refs
[CFR-14](https://linear.app/tryghost/issue/CFR-14/ensure-queue-depth-is-always-set-on-req)

Added queue depth to any request that passes through the request queue
middleware instead of only adding it to the request if it is queued.
This makes it easier to report on the queue depth within Elastic.
This commit is contained in:
Michael Barrett 2024-04-11 09:24:04 +01:00 committed by GitHub
parent d12b79e036
commit 9e78412268
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 25 deletions

View file

@ -38,8 +38,6 @@ module.exports = function queueRequest(
*/ */
queue.queue.on('queue', (job) => { queue.queue.on('queue', (job) => {
debug(`Request queued: ${job.data.req.path}`); debug(`Request queued: ${job.data.req.path}`);
job.data.req.queueDepth = job.queue.getLength();
}); });
queue.queue.on('complete', (job) => { queue.queue.on('complete', (job) => {
@ -47,6 +45,8 @@ module.exports = function queueRequest(
}); });
return (req, res, next) => { return (req, res, next) => {
req.queueDepth = queue.queue.getLength();
// Do not queue requests for static assets - We assume that any path // Do not queue requests for static assets - We assume that any path
// with a file extension is a static asset // with a file extension is a static asset
if (path.extname(req.path)) { if (path.extname(req.path)) {

View file

@ -16,7 +16,8 @@ describe('Queue request middleware', function () {
queue = sinon.stub(); queue = sinon.stub();
queue.queue = { queue.queue = {
on: sinon.stub() on: sinon.stub(),
getLength: sinon.stub().returns(0)
}; };
queueFactory = sinon.stub().returns(queue); queueFactory = sinon.stub().returns(queue);
@ -60,33 +61,18 @@ describe('Queue request middleware', function () {
assert(queue.calledWith(req, res, next), 'queue should be called with the correct arguments'); assert(queue.calledWith(req, res, next), 'queue should be called with the correct arguments');
}); });
it('should record the queue depth on a request when it has queued', function () { it('should record the queue depth on a request', function () {
const queueEvent = 'queue';
const queueLength = 123; const queueLength = 123;
// Assert event listener is added queue.queue.getLength.returns(queueLength);
queueRequest(config, queueFactory);
assert(queue.queue.on.calledWith(queueEvent), `"${queueEvent}" event listener should be added`); req.path = '/foo/bar';
const listener = queue.queue.on.args.find(arg => arg[0] === queueEvent)[1]; const mw = queueRequest(config, queueFactory);
// Assert event listener implementation mw(req, res, next);
const queueJob = {
data: {
req: {
path: '/foo/bar'
}
},
queue: {
getLength() {
return queueLength;
}
}
};
listener(queueJob); assert(queue.queue.getLength, 'queue should be called once');
assert(req.queueDepth === queueLength, 'queue depth should be set on the request');
assert(queueJob.data.req.queueDepth === queueLength, 'queueDepth should be set on the request');
}); });
}); });