From d0e6fb496842fe54729d058c029c41f116adf7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 9 Apr 2024 19:57:56 +0200 Subject: [PATCH 1/3] Fix and test MongoDB failed queue provider --- src/Queue/Failed/MongoFailedJobProvider.php | 40 ++++- .../Failed/MongoFailedJobProviderTest.php | 150 ++++++++++++++++++ 2 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 tests/Queue/Failed/MongoFailedJobProviderTest.php diff --git a/src/Queue/Failed/MongoFailedJobProvider.php b/src/Queue/Failed/MongoFailedJobProvider.php index 0525c272e..b7a05075c 100644 --- a/src/Queue/Failed/MongoFailedJobProvider.php +++ b/src/Queue/Failed/MongoFailedJobProvider.php @@ -5,6 +5,7 @@ namespace MongoDB\Laravel\Queue\Failed; use Carbon\Carbon; +use DateTimeInterface; use Exception; use Illuminate\Queue\Failed\DatabaseFailedJobProvider; use MongoDB\BSON\UTCDateTime; @@ -55,16 +56,16 @@ public function all() /** * Get a single failed job. * - * @param mixed $id + * @param string $id * - * @return object + * @return object|null */ public function find($id) { $job = $this->getTable()->find($id); if (! $job) { - return; + return null; } $job['id'] = (string) $job['_id']; @@ -75,7 +76,7 @@ public function find($id) /** * Delete a single failed job from storage. * - * @param mixed $id + * @param string $id * * @return bool */ @@ -83,4 +84,35 @@ public function forget($id) { return $this->getTable()->where('_id', $id)->delete() > 0; } + + /** + * Get the IDs of all of the failed jobs. + * + * @param string|null $queue + * + * @return array + */ + public function ids($queue = null) + { + return $this->getTable() + ->when($queue !== null, static fn ($query) => $query->where('queue', $queue)) + ->orderBy('_id', 'desc') + ->pluck('_id') + ->all(); + } + + /** + * Prune all entries older than the given date. + * + * @param DateTimeInterface $before + * + * @return int + */ + public function prune(DateTimeInterface $before) + { + return $this + ->getTable() + ->where('failed_at', '<', $before) + ->delete(); + } } diff --git a/tests/Queue/Failed/MongoFailedJobProviderTest.php b/tests/Queue/Failed/MongoFailedJobProviderTest.php new file mode 100644 index 000000000..f113428ec --- /dev/null +++ b/tests/Queue/Failed/MongoFailedJobProviderTest.php @@ -0,0 +1,150 @@ +collection('failed_jobs') + ->raw() + ->insertMany(array_map(static fn ($i) => [ + '_id' => new ObjectId(sprintf('%024d', $i)), + 'connection' => 'mongodb', + 'queue' => $i % 2 ? 'default' : 'other', + 'failed_at' => new UTCDateTime(Date::now()->subHours($i)), + ], range(1, 5))); + } + + public function tearDown(): void + { + DB::connection('mongodb') + ->collection('failed_jobs') + ->raw() + ->drop(); + + parent::tearDown(); + } + + public function testLog(): void + { + $provider = $this->getProvider(); + + $provider->log('mongodb', 'default', '{"foo":"bar"}', new OutOfBoundsException('This is the error')); + + $ids = $provider->ids(); + + $this->assertCount(6, $ids); + + $inserted = $provider->find($ids[0]); + + $this->assertSame('mongodb', $inserted->connection); + $this->assertSame('default', $inserted->queue); + $this->assertSame('{"foo":"bar"}', $inserted->payload); + $this->assertStringContainsString('OutOfBoundsException: This is the error', $inserted->exception); + $this->assertInstanceOf(ObjectId::class, $inserted->_id); + $this->assertSame((string) $inserted->_id, $inserted->id); + } + + public function testCount(): void + { + $provider = $this->getProvider(); + + $this->assertEquals(5, $provider->count()); + $this->assertEquals(3, $provider->count('mongodb', 'default')); + $this->assertEquals(2, $provider->count('mongodb', 'other')); + } + + public function testAll(): void + { + $all = $this->getProvider()->all(); + + $this->assertCount(5, $all); + $this->assertEquals(new ObjectId(sprintf('%024d', 5)), $all[0]->_id); + $this->assertEquals(sprintf('%024d', 5), $all[0]->id, 'id field is added for compatibility with DatabaseFailedJobProvider'); + } + + public function testFindAndForget(): void + { + $provider = $this->getProvider(); + + $id = sprintf('%024d', 2); + $found = $provider->find($id); + + $this->assertIsObject($found, 'The job is found'); + $this->assertEquals(new ObjectId($id), $found->_id); + $this->assertObjectHasProperty('failed_at', $found); + + // Delete the job + $result = $provider->forget($id); + + $this->assertTrue($result, 'forget return true when the job have been deleted'); + $this->assertNull($provider->find($id), 'the job have been deleted'); + + // Delete the same job again + $result = $provider->forget($id); + + $this->assertFalse($result, 'forget return false when the job does not exist'); + + $this->assertCount(4, $provider->ids(), 'Other jobs are kept'); + } + + public function testIds(): void + { + $ids = $this->getProvider()->ids(); + + $this->assertCount(5, $ids); + $this->assertEquals(new ObjectId(sprintf('%024d', 5)), $ids[0]); + } + + public function testIdsFilteredByQuery(): void + { + $ids = $this->getProvider()->ids('other'); + + $this->assertCount(2, $ids); + $this->assertEquals(new ObjectId(sprintf('%024d', 4)), $ids[0]); + } + + public function testFlush(): void + { + $provider = $this->getProvider(); + + $this->assertEquals(5, $provider->count()); + + $provider->flush(4); + + $this->assertEquals(3, $provider->count()); + } + + public function testPrune(): void + { + $provider = $this->getProvider(); + + $this->assertEquals(5, $provider->count()); + + $result = $provider->prune(Date::now()->subHours(4)); + + $this->assertEquals(2, $result); + $this->assertEquals(3, $provider->count()); + } + + private function getProvider(): MongoFailedJobProvider + { + return new MongoFailedJobProvider(DB::getFacadeRoot(), '', 'failed_jobs'); + } +} From 9fbe1efdc9fdb9bfdecaa29959cf3a8f8a065729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 15 Apr 2024 13:24:39 +0200 Subject: [PATCH 2/3] Update src/Queue/Failed/MongoFailedJobProvider.php --- src/Queue/Failed/MongoFailedJobProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Queue/Failed/MongoFailedJobProvider.php b/src/Queue/Failed/MongoFailedJobProvider.php index b7a05075c..cf72688e2 100644 --- a/src/Queue/Failed/MongoFailedJobProvider.php +++ b/src/Queue/Failed/MongoFailedJobProvider.php @@ -90,7 +90,7 @@ public function forget($id) * * @param string|null $queue * - * @return array + * @return list */ public function ids($queue = null) { From 61368eff95400586fb31faa60c196c8ef03135c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 15 Apr 2024 15:31:08 +0200 Subject: [PATCH 3/3] Update phpdoc --- CHANGELOG.md | 1 + src/Queue/Failed/MongoFailedJobProvider.php | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 382bee76a..55a84247e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. * New aggregation pipeline builder by @GromNaN in [#2738](https://github.com/mongodb/laravel-mongodb/pull/2738) * Drop support for Composer 1.x by @GromNaN in [#2784](https://github.com/mongodb/laravel-mongodb/pull/2784) +* Fix `artisan query:retry` command by @GromNaN in [#2838](https://github.com/mongodb/laravel-mongodb/pull/2838) ## [4.2.0] - 2024-03-14 diff --git a/src/Queue/Failed/MongoFailedJobProvider.php b/src/Queue/Failed/MongoFailedJobProvider.php index cf72688e2..357f27ddc 100644 --- a/src/Queue/Failed/MongoFailedJobProvider.php +++ b/src/Queue/Failed/MongoFailedJobProvider.php @@ -8,6 +8,7 @@ use DateTimeInterface; use Exception; use Illuminate\Queue\Failed\DatabaseFailedJobProvider; +use MongoDB\BSON\ObjectId; use MongoDB\BSON\UTCDateTime; use function array_map; @@ -86,11 +87,11 @@ public function forget($id) } /** - * Get the IDs of all of the failed jobs. + * Get the IDs of all the failed jobs. * * @param string|null $queue * - * @return list + * @return list */ public function ids($queue = null) { @@ -102,7 +103,7 @@ public function ids($queue = null) } /** - * Prune all entries older than the given date. + * Prune all failed jobs older than the given date. * * @param DateTimeInterface $before *