Skip to content

Fixed queue attempts bug #1535

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ FROM php:7.1-cli
RUN pecl install xdebug

RUN apt-get update && \
apt-get install -y autoconf pkg-config libssl-dev git && \
pecl install mongodb git zlib1g-dev && docker-php-ext-enable mongodb && \
docker-php-ext-install -j$(nproc) pdo pdo_mysql zip && docker-php-ext-enable xdebug
apt-get install -y autoconf pkg-config libssl-dev git zlib1g-dev && \
pecl install mongodb && docker-php-ext-enable mongodb && \
docker-php-ext-install -j$(nproc) pdo pdo_mysql zip && docker-php-ext-enable xdebug

RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/ \
Expand Down
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Queue/MongoJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MongoJob extends DatabaseJob
*/
public function isReserved()
{
return $this->job->reserved;
return $this->job->reserved_at != null;
}

/**
Expand Down
25 changes: 8 additions & 17 deletions src/Jenssegers/Mongodb/Queue/MongoQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ protected function getNextAvailableJobAndReserve($queue)
$job = $this->database->getCollection($this->table)->findOneAndUpdate(
[
'queue' => $this->getQueue($queue),
'reserved' => 0,
'reserved_at' => null,
'available_at' => ['$lte' => Carbon::now()->getTimestamp()],
],
[
'$set' => [
'reserved' => 1,
'reserved_at' => Carbon::now()->getTimestamp(),
],
'$inc' => [
'attempts' => 1,
]
],
[
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
Expand Down Expand Up @@ -105,20 +107,11 @@ protected function releaseJobsThatHaveBeenReservedTooLong($queue)

$reserved = $this->database->collection($this->table)
->where('queue', $this->getQueue($queue))
->where(function ($query) use ($expiration, $now) {
// Check for available jobs
$query->where(function ($query) use ($now) {
$query->whereNull('reserved_at');
$query->where('available_at', '<=', $now);
});

// Check for jobs that are reserved but have expired
$query->orWhere('reserved_at', '<=', $expiration);
})->get();
->where('reserved_at', '<=', $expiration)
->get();

foreach ($reserved as $job) {
$attempts = $job['attempts'] + 1;
$this->releaseJob($job['_id'], $attempts);
$this->releaseJob($job['_id']);
}
}

Expand All @@ -129,12 +122,10 @@ protected function releaseJobsThatHaveBeenReservedTooLong($queue)
* @param int $attempts
* @return void
*/
protected function releaseJob($id, $attempts)
protected function releaseJob($id)
{
$this->database->table($this->table)->where('_id', $id)->update([
'reserved' => 0,
'reserved_at' => null,
'attempts' => $attempts,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testQueueJobExpired()
Queue::getDatabase()
->table(Config::get('queue.connections.database.table'))
->where('_id', $id)
->update(['reserved' => 1, 'reserved_at' => $expiry]);
->update(['reserved_at' => $expiry]);

// Expect an attempted older job in the queue
$job = Queue::pop('test');
Expand Down