Job retries

This commit is contained in:
Juan Pablo Vial
2025-06-27 18:42:47 -04:00
parent 1bbee1121b
commit 7f8bd607e3
4 changed files with 99 additions and 13 deletions

View File

@ -5,12 +5,13 @@ use Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Exception\ServiceAction\{Create, Read};
use Incoviba\Exception\ServiceAction\{Create, Delete, Read, Update};
use Incoviba\Service;
class Queue extends Ideal\Service
{
public function __construct(LoggerInterface $logger, protected Service\Job $jobService, Worker $defaultWorker)
public function __construct(LoggerInterface $logger, protected Service\Job $jobService, Worker $defaultWorker,
protected int $maxRetries = 5)
{
parent::__construct($logger);
$this->register('default', $defaultWorker);
@ -94,9 +95,23 @@ class Queue extends Ideal\Service
$errors = [];
foreach ($jobs as $job) {
if ($job->retries >= $this->maxRetries) {
try {
$this->jobService->remove($job);
} catch (Read | Delete $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception]);
}
continue;
}
try {
$this->runJob($job->id, $request);
} catch (Exception) {
$job->retries ++;
try {
$this->jobService->update($job);
} catch (Read | Update $exception) {
$this->logger->error($exception->getMessage(), ['exception' => $exception]);
}
$errors []= $job->id;
}
}