Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement job not found on ES retrial #35

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions src/Service/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

final class QueueManager implements ProducerQueueManagerInterface, WorkerQueueManagerInterface
{
private const JOB_NOT_FOUND_MAX_RETRIES = 3;
private const JOB_NOT_FOUND_RETRY_DELAY_SECONDS = 3;

/**
* @var BeanstalkClient
*/
Expand Down Expand Up @@ -52,6 +55,11 @@ final class QueueManager implements ProducerQueueManagerInterface, WorkerQueueMa
*/
private $batchSize;

/**
* @var array<int, int>
*/
private static $beanstalkIdToNotFoundCountMap = [];

public function __construct(
FlowConfig $flowConfig,
BeanstalkClient $beanstalkClient,
Expand Down Expand Up @@ -128,21 +136,38 @@ public function flush(): Promise
public function getNextJob(): Promise
{
return call(function () {
try {
$rawJob = yield $this->beanstalkClient->reserve();
} catch (\Exception $ex) {
throw new FatalQueueException($ex->getMessage(), $ex->getCode(), $ex);
}

list($jobBeanstalkId, $jobUuid) = $rawJob;

try {
/** @var Job $job */
$job = yield $this->elasticSearch->fetchJob($jobUuid, $this->flowConfig->getTube());
} catch (\Throwable $exception) {
yield $this->beanstalkClient->bury($jobBeanstalkId);

throw new JobNotFoundException($jobUuid, 0, $exception);
while (true) {
try {
$rawJob = yield $this->beanstalkClient->reserve();
} catch (\Exception $ex) {
throw new FatalQueueException($ex->getMessage(), $ex->getCode(), $ex);
}

list($jobBeanstalkId, $jobUuid) = $rawJob;

try {
/** @var Job $job */
$job = yield $this->elasticSearch->fetchJob($jobUuid, $this->flowConfig->getTube());
} catch (\Throwable $exception) {
$notFoundCount = self::$beanstalkIdToNotFoundCountMap[$jobBeanstalkId] ?? 1;
if ($notFoundCount <= self::JOB_NOT_FOUND_MAX_RETRIES) {
self::$beanstalkIdToNotFoundCountMap[$jobBeanstalkId] = $notFoundCount + 1;
$this->logger->warning(
"Job with UUID {$jobUuid} not found in Elasticsearch. Retrying...",
['beanstalk_id' => $jobBeanstalkId, 'not_found_count' => $notFoundCount]
);
yield $this->beanstalkClient->release(
$jobBeanstalkId,
self::JOB_NOT_FOUND_RETRY_DELAY_SECONDS ** $notFoundCount
);
continue;
}
yield $this->beanstalkClient->bury($jobBeanstalkId);

throw new JobNotFoundException($jobUuid, 0, $exception);
}

break;
}

$this->saveJobBeanstalkId($job, $jobBeanstalkId);
Expand Down