From 9e78412268bd2a4d52b5ca3a8455800985dbe10c Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Thu, 11 Apr 2024 09:24:04 +0100 Subject: [PATCH] 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. --- .../web/parent/middleware/queue-request.js | 4 +-- .../parent/middleware/queue-request.test.js | 32 ++++++------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/ghost/core/core/server/web/parent/middleware/queue-request.js b/ghost/core/core/server/web/parent/middleware/queue-request.js index 6a585e0044..908d0e9939 100644 --- a/ghost/core/core/server/web/parent/middleware/queue-request.js +++ b/ghost/core/core/server/web/parent/middleware/queue-request.js @@ -38,8 +38,6 @@ module.exports = function queueRequest( */ queue.queue.on('queue', (job) => { debug(`Request queued: ${job.data.req.path}`); - - job.data.req.queueDepth = job.queue.getLength(); }); queue.queue.on('complete', (job) => { @@ -47,6 +45,8 @@ module.exports = function queueRequest( }); return (req, res, next) => { + req.queueDepth = queue.queue.getLength(); + // Do not queue requests for static assets - We assume that any path // with a file extension is a static asset if (path.extname(req.path)) { diff --git a/ghost/core/test/unit/server/web/parent/middleware/queue-request.test.js b/ghost/core/test/unit/server/web/parent/middleware/queue-request.test.js index 8693246d27..3783c1381e 100644 --- a/ghost/core/test/unit/server/web/parent/middleware/queue-request.test.js +++ b/ghost/core/test/unit/server/web/parent/middleware/queue-request.test.js @@ -16,7 +16,8 @@ describe('Queue request middleware', function () { queue = sinon.stub(); queue.queue = { - on: sinon.stub() + on: sinon.stub(), + getLength: sinon.stub().returns(0) }; 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'); }); - it('should record the queue depth on a request when it has queued', function () { - const queueEvent = 'queue'; + it('should record the queue depth on a request', function () { const queueLength = 123; - // Assert event listener is added - queueRequest(config, queueFactory); + queue.queue.getLength.returns(queueLength); - 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 - const queueJob = { - data: { - req: { - path: '/foo/bar' - } - }, - queue: { - getLength() { - return queueLength; - } - } - }; + mw(req, res, next); - listener(queueJob); - - assert(queueJob.data.req.queueDepth === queueLength, 'queueDepth should be set on the request'); + assert(queue.queue.getLength, 'queue should be called once'); + assert(req.queueDepth === queueLength, 'queue depth should be set on the request'); }); });