From 4d3f45ef2b5f0a6b668e222d82c132de392a0e85 Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Thu, 17 Oct 2019 19:05:37 -0300 Subject: [PATCH 1/9] increment attempts only when taken from list --- src/Jenssegers/Mongodb/Queue/MongoQueue.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Jenssegers/Mongodb/Queue/MongoQueue.php b/src/Jenssegers/Mongodb/Queue/MongoQueue.php index 44249456a..34e905ea8 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoQueue.php +++ b/src/Jenssegers/Mongodb/Queue/MongoQueue.php @@ -64,7 +64,7 @@ protected function getNextAvailableJobAndReserve($queue) $job = $this->database->getCollection($this->table)->findOneAndUpdate( [ 'queue' => $this->getQueue($queue), - 'reserved' => 0, + 'reserved' => ['$ne' => 1], 'available_at' => ['$lte' => Carbon::now()->getTimestamp()], ], [ @@ -72,6 +72,9 @@ protected function getNextAvailableJobAndReserve($queue) 'reserved' => 1, 'reserved_at' => Carbon::now()->getTimestamp(), ], + '$inc' => [ + 'attempts' => 1, + ], ], [ 'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER, @@ -98,20 +101,12 @@ 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(); + ->whereNotNull('reserved_at'); + ->where('reserved_at', '<=', $expiration); + ->get(); foreach ($reserved as $job) { - $attempts = $job['attempts'] + 1; - $this->releaseJob($job['_id'], $attempts); + $this->releaseJob($job['_id'], $job['attempts']); } } From e68ca24be0d325f4ae2059461563132a6f898b44 Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Thu, 17 Oct 2019 19:19:35 -0300 Subject: [PATCH 2/9] test for increment attempts only when taken from list --- src/Jenssegers/Mongodb/Queue/MongoQueue.php | 4 ++-- tests/QueueTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Queue/MongoQueue.php b/src/Jenssegers/Mongodb/Queue/MongoQueue.php index 34e905ea8..8aa17dd7e 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoQueue.php +++ b/src/Jenssegers/Mongodb/Queue/MongoQueue.php @@ -101,8 +101,8 @@ protected function releaseJobsThatHaveBeenReservedTooLong($queue) $reserved = $this->database->collection($this->table) ->where('queue', $this->getQueue($queue)) - ->whereNotNull('reserved_at'); - ->where('reserved_at', '<=', $expiration); + ->whereNotNull('reserved_at') + ->where('reserved_at', '<=', $expiration) ->get(); foreach ($reserved as $job) { diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 21306bfb1..b5b62e31a 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -63,4 +63,20 @@ public function testFindFailJobNull(): void $this->assertNull($provider->find(1)); } + + public function testIncrementAttempts(): void + { + Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); + Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); + + $job = Queue::pop('test'); + + $jobs = Queue::getDatabase() + ->table(Config::get('queue.connections.database.table')) + ->get(); + + $this->assertEquals(1, $jobs[0]['attempts']); + $this->assertEquals(1, $jobs[0]['reserved']); + $this->assertEquals(0, $jobs[1]['attempts']); + } } From 106f022bf8b0571346eb8780cbfaf5bfbfecc731 Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Thu, 17 Oct 2019 19:33:18 -0300 Subject: [PATCH 3/9] fix standards --- tests/QueueTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index b5b62e31a..81cb5160b 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -69,7 +69,7 @@ public function testIncrementAttempts(): void Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); - $job = Queue::pop('test'); + Queue::pop('test'); $jobs = Queue::getDatabase() ->table(Config::get('queue.connections.database.table')) From 7842537808c758be2c2cebf5ea49e524fb17825e Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Fri, 18 Oct 2019 11:07:46 -0300 Subject: [PATCH 4/9] create more tests case --- tests/QueueTest.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 81cb5160b..126c52aa5 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -66,17 +66,20 @@ public function testFindFailJobNull(): void public function testIncrementAttempts(): void { - Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); - Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); + $id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); + $this->assertNotNull($id); + $id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); + $this->assertNotNull($id); - Queue::pop('test'); + $job = Queue::pop('test'); + $this->assertEquals(1, $job->attempts()); + $job->delete(); - $jobs = Queue::getDatabase() + $others_jobs = Queue::getDatabase() ->table(Config::get('queue.connections.database.table')) ->get(); - $this->assertEquals(1, $jobs[0]['attempts']); - $this->assertEquals(1, $jobs[0]['reserved']); - $this->assertEquals(0, $jobs[1]['attempts']); + $this->assertCount(1, $others_jobs); + $this->assertEquals(0, $others_jobs[0]['attempts']); } } From 2f61c95510ad9c06ff075dcc9bd8eaa155baad0d Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Fri, 18 Oct 2019 11:18:23 -0300 Subject: [PATCH 5/9] remove unused variable --- src/Jenssegers/Mongodb/Queue/MongoQueue.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Queue/MongoQueue.php b/src/Jenssegers/Mongodb/Queue/MongoQueue.php index 8aa17dd7e..369ef6b72 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoQueue.php +++ b/src/Jenssegers/Mongodb/Queue/MongoQueue.php @@ -97,7 +97,6 @@ protected function getNextAvailableJobAndReserve($queue) protected function releaseJobsThatHaveBeenReservedTooLong($queue) { $expiration = Carbon::now()->subSeconds($this->retryAfter)->getTimestamp(); - $now = time(); $reserved = $this->database->collection($this->table) ->where('queue', $this->getQueue($queue)) From fb42690c9632ac0726ff109ef30be0bf9c082bed Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Fri, 18 Oct 2019 14:34:23 -0300 Subject: [PATCH 6/9] change var name Co-Authored-By: esron --- tests/QueueTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 126c52aa5..a37407c76 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -66,7 +66,7 @@ public function testFindFailJobNull(): void public function testIncrementAttempts(): void { - $id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); + $job_id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); $this->assertNotNull($id); $id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); $this->assertNotNull($id); From 86c019e539344aefa9ee76cb0a3c4fea7e66302c Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Fri, 18 Oct 2019 14:34:33 -0300 Subject: [PATCH 7/9] change var name Co-Authored-By: esron --- tests/QueueTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index a37407c76..e9f671df5 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -67,7 +67,7 @@ public function testFindFailJobNull(): void public function testIncrementAttempts(): void { $job_id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); - $this->assertNotNull($id); + $this->assertNotNull($job_id); $id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); $this->assertNotNull($id); From c1ea92e965d8a0b31fd22377b6d2ec854f1b15c3 Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Fri, 18 Oct 2019 14:34:44 -0300 Subject: [PATCH 8/9] change var name Co-Authored-By: esron --- tests/QueueTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index e9f671df5..5ac4c3b20 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -68,7 +68,7 @@ public function testIncrementAttempts(): void { $job_id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); $this->assertNotNull($job_id); - $id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); + $job_id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); $this->assertNotNull($id); $job = Queue::pop('test'); From 518443cd1feb1c4592a5ff38608cc3f4e26f5bea Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Fri, 18 Oct 2019 14:34:53 -0300 Subject: [PATCH 9/9] Update tests/QueueTest.php Co-Authored-By: esron --- tests/QueueTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 5ac4c3b20..66075f980 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -69,7 +69,7 @@ public function testIncrementAttempts(): void $job_id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test'); $this->assertNotNull($job_id); $job_id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test'); - $this->assertNotNull($id); + $this->assertNotNull($job_id); $job = Queue::pop('test'); $this->assertEquals(1, $job->attempts());