Skip to content

Commit f54a6af

Browse files
committed
Fix and test MongoDB failed queue provider
1 parent c9b9d2c commit f54a6af

File tree

2 files changed

+179
-4
lines changed

2 files changed

+179
-4
lines changed

src/Queue/Failed/MongoFailedJobProvider.php

+29-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MongoDB\Laravel\Queue\Failed;
66

77
use Carbon\Carbon;
8+
use DateTimeInterface;
89
use Exception;
910
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
1011
use MongoDB\BSON\UTCDateTime;
@@ -55,16 +56,16 @@ public function all()
5556
/**
5657
* Get a single failed job.
5758
*
58-
* @param mixed $id
59+
* @param string $id
5960
*
60-
* @return object
61+
* @return object|null
6162
*/
6263
public function find($id)
6364
{
6465
$job = $this->getTable()->find($id);
6566

6667
if (! $job) {
67-
return;
68+
return null;
6869
}
6970

7071
$job['id'] = (string) $job['_id'];
@@ -75,12 +76,36 @@ public function find($id)
7576
/**
7677
* Delete a single failed job from storage.
7778
*
78-
* @param mixed $id
79+
* @param string $id
7980
*
8081
* @return bool
8182
*/
8283
public function forget($id)
8384
{
8485
return $this->getTable()->where('_id', $id)->delete() > 0;
8586
}
87+
88+
/**
89+
* Get the IDs of all of the failed jobs.
90+
*
91+
* @param string|null $queue
92+
*
93+
* @return array
94+
*/
95+
public function ids($queue = null)
96+
{
97+
return $this->getTable()
98+
->when($queue !== null, static fn ($query) => $query->where('queue', $queue))
99+
->orderBy('_id', 'desc')
100+
->pluck('_id')
101+
->all();
102+
}
103+
104+
public function prune(DateTimeInterface $before)
105+
{
106+
return $this
107+
->getTable()
108+
->where('failed_at', '<', $before)
109+
->delete();
110+
}
86111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Queue\Failed;
4+
5+
use Illuminate\Support\Facades\Date;
6+
use Illuminate\Support\Facades\DB;
7+
use MongoDB\BSON\ObjectId;
8+
use MongoDB\BSON\UTCDateTime;
9+
use MongoDB\Laravel\Queue\Failed\MongoFailedJobProvider;
10+
use MongoDB\Laravel\Tests\TestCase;
11+
use OutOfBoundsException;
12+
13+
use function array_map;
14+
use function range;
15+
use function sprintf;
16+
17+
class MongoFailedJobProviderTest extends TestCase
18+
{
19+
public function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
DB::connection('mongodb')
24+
->collection('failed_jobs')
25+
->raw()
26+
->insertMany(array_map(static fn ($i) => [
27+
'_id' => new ObjectId(sprintf('%024d', $i)),
28+
'connection' => 'mongodb',
29+
'queue' => $i % 2 ? 'default' : 'other',
30+
'failed_at' => new UTCDateTime(Date::now()->subHours($i)),
31+
], range(1, 5)));
32+
}
33+
34+
public function tearDown(): void
35+
{
36+
DB::connection('mongodb')
37+
->collection('failed_jobs')
38+
->raw()
39+
->drop();
40+
41+
parent::tearDown();
42+
}
43+
44+
public function testLog(): void
45+
{
46+
$provider = $this->getProvider();
47+
48+
$provider->log('mongodb', 'default', '{"foo":"bar"}', new OutOfBoundsException('This is the error'));
49+
50+
$ids = $provider->ids();
51+
52+
$this->assertCount(6, $ids);
53+
54+
$inserted = $provider->find($ids[0]);
55+
56+
$this->assertSame('mongodb', $inserted->connection);
57+
$this->assertSame('default', $inserted->queue);
58+
$this->assertSame('{"foo":"bar"}', $inserted->payload);
59+
$this->assertStringContainsString('OutOfBoundsException: This is the error', $inserted->exception);
60+
$this->assertInstanceOf(ObjectId::class, $inserted->_id);
61+
$this->assertSame((string) $inserted->_id, $inserted->id);
62+
}
63+
64+
public function testCount(): void
65+
{
66+
$provider = $this->getProvider();
67+
68+
$this->assertEquals(5, $provider->count());
69+
$this->assertEquals(3, $provider->count('mongodb', 'default'));
70+
$this->assertEquals(2, $provider->count('mongodb', 'other'));
71+
}
72+
73+
public function testAll(): void
74+
{
75+
$all = $this->getProvider()->all();
76+
77+
$this->assertCount(5, $all);
78+
$this->assertEquals(new ObjectId(sprintf('%024d', 5)), $all[0]->_id);
79+
$this->assertEquals(sprintf('%024d', 5), $all[0]->id, 'id field is added for compatibility with DatabaseFailedJobProvider');
80+
}
81+
82+
public function testFindAndForget(): void
83+
{
84+
$provider = $this->getProvider();
85+
86+
$id = sprintf('%024d', 2);
87+
$found = $provider->find($id);
88+
89+
$this->assertIsObject($found, 'The job is found');
90+
$this->assertEquals(new ObjectId($id), $found->_id);
91+
$this->assertObjectHasProperty('failed_at', $found);
92+
93+
// Delete the job
94+
$result = $provider->forget($id);
95+
96+
$this->assertTrue($result, 'forget return true when the job have been deleted');
97+
$this->assertNull($provider->find($id), 'the job have been deleted');
98+
99+
// Delete the same job again
100+
$result = $provider->forget($id);
101+
102+
$this->assertFalse($result, 'forget return false when the job does not exist');
103+
104+
$this->assertCount(4, $provider->ids(), 'Other jobs are kept');
105+
}
106+
107+
public function testIds(): void
108+
{
109+
$ids = $this->getProvider()->ids();
110+
111+
$this->assertCount(5, $ids);
112+
$this->assertEquals(new ObjectId(sprintf('%024d', 5)), $ids[0]);
113+
}
114+
115+
public function testIdsFilteredByQuery(): void
116+
{
117+
$ids = $this->getProvider()->ids('other');
118+
119+
$this->assertCount(2, $ids);
120+
$this->assertEquals(new ObjectId(sprintf('%024d', 4)), $ids[0]);
121+
}
122+
123+
public function testFlush(): void
124+
{
125+
$provider = $this->getProvider();
126+
127+
$this->assertEquals(5, $provider->count());
128+
129+
$provider->flush(4);
130+
131+
$this->assertEquals(3, $provider->count());
132+
}
133+
134+
public function testPrune(): void
135+
{
136+
$provider = $this->getProvider();
137+
138+
$this->assertEquals(5, $provider->count());
139+
140+
$result = $provider->prune(Date::now()->subHours(4));
141+
142+
$this->assertEquals(2, $result);
143+
$this->assertEquals(3, $provider->count());
144+
}
145+
146+
private function getProvider(): MongoFailedJobProvider
147+
{
148+
return new MongoFailedJobProvider(DB::getFacadeRoot(), '', 'failed_jobs');
149+
}
150+
}

0 commit comments

Comments
 (0)