Skip to content

Commit 1cd1346

Browse files
[10.x] Allow failed job providers to be countable (#48177)
* Allow failed job providers to be countable * Fix return type * Rename test names * Lint * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent f981223 commit 1cd1346

7 files changed

+121
-4
lines changed

src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Illuminate\Queue\Failed;
44

5+
use Countable;
56
use DateTimeInterface;
67
use Illuminate\Database\ConnectionResolverInterface;
78
use Illuminate\Support\Facades\Date;
89

9-
class DatabaseFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
10+
class DatabaseFailedJobProvider implements Countable, FailedJobProviderInterface, PrunableFailedJobProvider
1011
{
1112
/**
1213
* The connection resolver implementation.
@@ -130,6 +131,14 @@ public function prune(DateTimeInterface $before)
130131
return $totalDeleted;
131132
}
132133

134+
/**
135+
* Count the failed jobs.
136+
*/
137+
public function count(): int
138+
{
139+
return $this->getTable()->count();
140+
}
141+
133142
/**
134143
* Get a new query builder instance for the table.
135144
*

src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Illuminate\Queue\Failed;
44

5+
use Countable;
56
use DateTimeInterface;
67
use Illuminate\Database\ConnectionResolverInterface;
78
use Illuminate\Support\Facades\Date;
89

9-
class DatabaseUuidFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
10+
class DatabaseUuidFailedJobProvider implements Countable, FailedJobProviderInterface, PrunableFailedJobProvider
1011
{
1112
/**
1213
* The connection resolver implementation.
@@ -143,6 +144,14 @@ public function prune(DateTimeInterface $before)
143144
return $totalDeleted;
144145
}
145146

147+
/**
148+
* Count the failed jobs.
149+
*/
150+
public function count(): int
151+
{
152+
return $this->getTable()->count();
153+
}
154+
146155
/**
147156
* Get a new query builder instance for the table.
148157
*

src/Illuminate/Queue/Failed/FileFailedJobProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace Illuminate\Queue\Failed;
44

55
use Closure;
6+
use Countable;
67
use DateTimeInterface;
78
use Illuminate\Support\Facades\Date;
89

9-
class FileFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
10+
class FileFailedJobProvider implements Countable, FailedJobProviderInterface, PrunableFailedJobProvider
1011
{
1112
/**
1213
* The file path where the failed job file should be stored.
@@ -202,4 +203,12 @@ protected function write(array $jobs)
202203
json_encode($jobs, JSON_PRETTY_PRINT)
203204
);
204205
}
206+
207+
/**
208+
* Count the failed jobs.
209+
*/
210+
public function count(): int
211+
{
212+
return count($this->read());
213+
}
205214
}

src/Illuminate/Queue/Failed/NullFailedJobProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Illuminate\Queue\Failed;
44

5-
class NullFailedJobProvider implements FailedJobProviderInterface
5+
use Countable;
6+
7+
class NullFailedJobProvider implements Countable, FailedJobProviderInterface
68
{
79
/**
810
* Log a failed job into storage.
@@ -60,4 +62,12 @@ public function flush($hours = null)
6062
{
6163
//
6264
}
65+
66+
/**
67+
* Count the failed jobs.
68+
*/
69+
public function count(): int
70+
{
71+
return 0;
72+
}
6373
}

tests/Queue/DatabaseFailedJobProviderTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Facades\Date;
1010
use Illuminate\Support\Str;
1111
use PHPUnit\Framework\TestCase;
12+
use RuntimeException;
1213

1314
class DatabaseFailedJobProviderTest extends TestCase
1415
{
@@ -71,4 +72,31 @@ public function testCanProperlyLogFailedJob()
7172
$this->assertSame(1, $db->getConnection()->table('failed_jobs')->count());
7273
$this->assertSame($exception, $db->getConnection()->table('failed_jobs')->first()->exception);
7374
}
75+
76+
public function testJobsCanBeCounted()
77+
{
78+
$db = new DB;
79+
$db->addConnection([
80+
'driver' => 'sqlite',
81+
'database' => ':memory:',
82+
]);
83+
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
84+
$table->id();
85+
$table->text('connection');
86+
$table->text('queue');
87+
$table->longText('payload');
88+
$table->longText('exception');
89+
$table->timestamp('failed_at')->useCurrent();
90+
});
91+
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
92+
93+
$this->assertCount(0, $provider);
94+
95+
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
96+
$this->assertCount(1, $provider);
97+
98+
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
99+
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
100+
$this->assertCount(3, $provider);
101+
}
74102
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Queue;
4+
5+
use Illuminate\Database\Capsule\Manager as DB;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Queue\Failed\DatabaseUuidFailedJobProvider;
8+
use Illuminate\Support\Str;
9+
use PHPUnit\Framework\TestCase;
10+
use RuntimeException;
11+
12+
class DatabaseUuidFailedJobProviderTest extends TestCase
13+
{
14+
public function testJobsCanBeCounted()
15+
{
16+
$db = new DB;
17+
$db->addConnection([
18+
'driver' => 'sqlite',
19+
'database' => ':memory:',
20+
]);
21+
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
22+
$table->uuid();
23+
$table->text('connection');
24+
$table->text('queue');
25+
$table->longText('payload');
26+
$table->longText('exception');
27+
$table->timestamp('failed_at')->useCurrent();
28+
});
29+
$provider = new DatabaseUuidFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');
30+
31+
$this->assertCount(0, $provider);
32+
33+
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
34+
$this->assertCount(1, $provider);
35+
36+
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
37+
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
38+
$this->assertCount(3, $provider);
39+
}
40+
}

tests/Queue/FileFailedJobProviderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ public function testEmptyFailedJobsByDefault()
140140
$this->assertEmpty($failedJobs);
141141
}
142142

143+
public function testJobsCanBeCounted()
144+
{
145+
$this->assertCount(0, $this->provider);
146+
147+
$this->logFailedJob();
148+
$this->assertCount(1, $this->provider);
149+
150+
$this->logFailedJob();
151+
$this->logFailedJob();
152+
$this->assertCount(3, $this->provider);
153+
}
154+
143155
public function logFailedJob()
144156
{
145157
$uuid = Str::uuid();

0 commit comments

Comments
 (0)