Skip to content

Commit

Permalink
Allow failed job providers to be countable
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Aug 25, 2023
1 parent 7f287ed commit 4def2bf
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 4 deletions.
13 changes: 12 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 FailedJobProviderInterface, PrunableFailedJobProvider, Countable
{
/**
* The connection resolver implementation.
Expand Down Expand Up @@ -130,6 +131,16 @@ public function prune(DateTimeInterface $before)
return $totalDeleted;
}

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

/**
* Get a new query builder instance for the table.
*
Expand Down
13 changes: 12 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 FailedJobProviderInterface, PrunableFailedJobProvider, Countable
{
/**
* The connection resolver implementation.
Expand Down Expand Up @@ -143,6 +144,16 @@ public function prune(DateTimeInterface $before)
return $totalDeleted;
}

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

/**
* Get a new query builder instance for the table.
*
Expand Down
13 changes: 12 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 FailedJobProviderInterface, PrunableFailedJobProvider, Countable
{
/**
* The file path where the failed job file should be stored.
Expand Down Expand Up @@ -148,6 +149,16 @@ public function prune(DateTimeInterface $before)
});
}

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

/**
* Execute the given callback while holding a lock.
*
Expand Down
14 changes: 13 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 FailedJobProviderInterface, Countable
{
/**
* Log a failed job into storage.
Expand Down Expand Up @@ -60,4 +62,14 @@ public function flush($hours = null)
{
//
}

/**
* Count the failed jobs.
*
* @return int
*/
public function count()
{
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 testCountFailedJobs()
{
$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);
}
}
46 changes: 46 additions & 0 deletions tests/Queue/DatabaseUuidFailedJobProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Illuminate\Tests\Queue;

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

class DatabaseUuidFailedJobProviderTest extends TestCase
{
public function testCountFailedJobs()
{
$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 testCountFailedJobs()
{
$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

0 comments on commit 4def2bf

Please sign in to comment.