Skip to content

[10.x] Allow failed job providers to be countable #48177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Illuminate\Queue\Failed;

use Countable;
use DateTimeInterface;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Support\Facades\Date;

class DatabaseFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
class DatabaseFailedJobProvider implements Countable, FailedJobProviderInterface, PrunableFailedJobProvider
{
/**
* The connection resolver implementation.
Expand Down Expand Up @@ -130,6 +131,14 @@ public function prune(DateTimeInterface $before)
return $totalDeleted;
}

/**
* Count the failed jobs.
*/
public function count(): int
{
return $this->getTable()->count();
}

/**
* Get a new query builder instance for the table.
*
Expand Down
11 changes: 10 additions & 1 deletion src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Illuminate\Queue\Failed;

use Countable;
use DateTimeInterface;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Support\Facades\Date;

class DatabaseUuidFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
class DatabaseUuidFailedJobProvider implements Countable, FailedJobProviderInterface, PrunableFailedJobProvider
{
/**
* The connection resolver implementation.
Expand Down Expand Up @@ -143,6 +144,14 @@ public function prune(DateTimeInterface $before)
return $totalDeleted;
}

/**
* Count the failed jobs.
*/
public function count(): int
{
return $this->getTable()->count();
}

/**
* Get a new query builder instance for the table.
*
Expand Down
11 changes: 10 additions & 1 deletion src/Illuminate/Queue/Failed/FileFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Illuminate\Queue\Failed;

use Closure;
use Countable;
use DateTimeInterface;
use Illuminate\Support\Facades\Date;

class FileFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
class FileFailedJobProvider implements Countable, FailedJobProviderInterface, PrunableFailedJobProvider
{
/**
* The file path where the failed job file should be stored.
Expand Down Expand Up @@ -202,4 +203,12 @@ protected function write(array $jobs)
json_encode($jobs, JSON_PRETTY_PRINT)
);
}

/**
* Count the failed jobs.
*/
public function count(): int
{
return count($this->read());
}
}
12 changes: 11 additions & 1 deletion src/Illuminate/Queue/Failed/NullFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Queue\Failed;

class NullFailedJobProvider implements FailedJobProviderInterface
use Countable;

class NullFailedJobProvider implements Countable, FailedJobProviderInterface
{
/**
* Log a failed job into storage.
Expand Down Expand Up @@ -60,4 +62,12 @@ public function flush($hours = null)
{
//
}

/**
* Count the failed jobs.
*/
public function count(): int
{
return 0;
}
}
28 changes: 28 additions & 0 deletions tests/Queue/DatabaseFailedJobProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use PHPUnit\Framework\TestCase;
use RuntimeException;

class DatabaseFailedJobProviderTest extends TestCase
{
Expand Down Expand Up @@ -71,4 +72,31 @@ public function testCanProperlyLogFailedJob()
$this->assertSame(1, $db->getConnection()->table('failed_jobs')->count());
$this->assertSame($exception, $db->getConnection()->table('failed_jobs')->first()->exception);
}

public function testJobsCanBeCounted()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
$provider = new DatabaseFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');

$this->assertCount(0, $provider);

$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$this->assertCount(1, $provider);

$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$this->assertCount(3, $provider);
}
}
40 changes: 40 additions & 0 deletions tests/Queue/DatabaseUuidFailedJobProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Tests\Queue;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Queue\Failed\DatabaseUuidFailedJobProvider;
use Illuminate\Support\Str;
use PHPUnit\Framework\TestCase;
use RuntimeException;

class DatabaseUuidFailedJobProviderTest extends TestCase
{
public function testJobsCanBeCounted()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->getConnection()->getSchemaBuilder()->create('failed_jobs', function (Blueprint $table) {
$table->uuid();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
$provider = new DatabaseUuidFailedJobProvider($db->getDatabaseManager(), 'default', 'failed_jobs');

$this->assertCount(0, $provider);

$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$this->assertCount(1, $provider);

$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$provider->log('database', 'default', json_encode(['uuid' => (string) Str::uuid()]), new RuntimeException());
$this->assertCount(3, $provider);
}
}
12 changes: 12 additions & 0 deletions tests/Queue/FileFailedJobProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ public function testEmptyFailedJobsByDefault()
$this->assertEmpty($failedJobs);
}

public function testJobsCanBeCounted()
{
$this->assertCount(0, $this->provider);

$this->logFailedJob();
$this->assertCount(1, $this->provider);

$this->logFailedJob();
$this->logFailedJob();
$this->assertCount(3, $this->provider);
}

public function logFailedJob()
{
$uuid = Str::uuid();
Expand Down