diff --git a/docker/Dockerfile b/docker/Dockerfile index 62c68e45d..c37b559b1 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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/ \ diff --git a/src/Jenssegers/Mongodb/Queue/MongoJob.php b/src/Jenssegers/Mongodb/Queue/MongoJob.php index f1a61cf46..86b44dfcd 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoJob.php +++ b/src/Jenssegers/Mongodb/Queue/MongoJob.php @@ -13,7 +13,7 @@ class MongoJob extends DatabaseJob */ public function isReserved() { - return $this->job->reserved; + return $this->job->reserved_at != null; } /** diff --git a/src/Jenssegers/Mongodb/Queue/MongoQueue.php b/src/Jenssegers/Mongodb/Queue/MongoQueue.php index e9cd8da9c..b994fb411 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoQueue.php +++ b/src/Jenssegers/Mongodb/Queue/MongoQueue.php @@ -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, @@ -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']); } } @@ -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, ]); } diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 35be33f9a..f80a145fa 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -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');