diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index 618362b0af..3b34f2cdec 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -709,16 +709,23 @@ App::get('/v1/health/queue/failed/:name') Event::MIGRATIONS_QUEUE_NAME, Event::HAMSTER_CLASS_NAME ]), 'The name of the queue') + ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) ->label('sdk.description', '/docs/references/health/get-failed-queue-jobs.md') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_HEALTH_QUEUE) ->inject('response') ->inject('queue') - ->action(function (string $name, Response $response, Connection $queue) { + ->action(function (string $name, int|string $threshold, Response $response, Connection $queue) { + $threshold = \intval($threshold); + $client = new Client($name, $queue); $failed = $client->countFailedJobs(); + if ($failed >= $threshold) { + throw new Exception(Exception::QUEUE_SIZE_EXCEEDED, "Queue failed jobs threshold hit. Current size is {$failed} and threshold is {$threshold}."); + } + $response->dynamic(new Document([ 'size' => $failed ]), Response::MODEL_HEALTH_QUEUE); });