mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -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:
parent
d12b79e036
commit
9e78412268
2 changed files with 11 additions and 25 deletions
|
@ -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)) {
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue