Skip to content
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

[8.x] Add a queue:prune-failed command #37696

Merged
merged 3 commits into from
Jun 16, 2021
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
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use Illuminate\Queue\Console\ListenCommand as QueueListenCommand;
use Illuminate\Queue\Console\ListFailedCommand as ListFailedQueueCommand;
use Illuminate\Queue\Console\PruneBatchesCommand as PruneBatchesQueueCommand;
use Illuminate\Queue\Console\PruneFailedJobsCommand;
use Illuminate\Queue\Console\RestartCommand as QueueRestartCommand;
use Illuminate\Queue\Console\RetryBatchCommand as QueueRetryBatchCommand;
use Illuminate\Queue\Console\RetryCommand as QueueRetryCommand;
Expand Down Expand Up @@ -109,6 +110,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'QueueForget' => 'command.queue.forget',
'QueueListen' => 'command.queue.listen',
'QueuePruneBatches' => 'command.queue.prune-batches',
'QueuePruneFailedJobs' => 'command.queue.prune-failed-jobs',
'QueueRestart' => 'command.queue.restart',
'QueueRetry' => 'command.queue.retry',
'QueueRetryBatch' => 'command.queue.retry-batch',
Expand Down Expand Up @@ -698,6 +700,18 @@ protected function registerQueuePruneBatchesCommand()
});
}

/**
* Register the command.
*
* @return void
*/
protected function registerQueuePruneFailedJobsCommand()
{
$this->app->singleton('command.queue.prune-failed-jobs', function () {
return new PruneFailedJobsCommand();
});
}

/**
* Register the command.
*
Expand Down
43 changes: 43 additions & 0 deletions src/Illuminate/Queue/Console/PruneFailedJobsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Illuminate\Queue\Console;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Queue\Failed\PrunableFailedJobProvider;

class PruneFailedJobsCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'queue:prune-failed
{--hours=24 : The number of hours to retain failed jobs data}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Prune stale entries from the failed jobs table';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$failer = $this->laravel['queue.failer'];

$count = 0;

if ($failer instanceof PrunableFailedJobProvider) {
$count = $failer->prune(Carbon::now()->subHours($this->option('hours')));
}

$this->info("{$count} entries deleted!");
}
}
25 changes: 24 additions & 1 deletion src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Illuminate\Queue\Failed;

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

class DatabaseFailedJobProvider implements FailedJobProviderInterface
class DatabaseFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
{
/**
* The connection resolver implementation.
Expand Down Expand Up @@ -114,4 +115,26 @@ protected function getTable()
{
return $this->resolver->connection($this->database)->table($this->table);
}

/**
* Prune all of the entries older than the given date.
*
* @param \DateTimeInterface $before
* @return int
*/
public function prune(DateTimeInterface $before)
{
$query = $this->getTable()
->where('failed_at', '<', $before);

$totalDeleted = 0;

do {
$deleted = $query->take(1000)->delete();

$totalDeleted += $deleted;
} while ($deleted !== 0);

return $totalDeleted;
}
}
25 changes: 24 additions & 1 deletion src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Illuminate\Queue\Failed;

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

class DatabaseUuidFailedJobProvider implements FailedJobProviderInterface
class DatabaseUuidFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
{
/**
* The connection resolver implementation.
Expand Down Expand Up @@ -127,4 +128,26 @@ protected function getTable()
{
return $this->resolver->connection($this->database)->table($this->table);
}

/**
* Prune all of the entries older than the given date.
*
* @param \DateTimeInterface $before
* @return int
*/
public function prune(DateTimeInterface $before)
{
$query = $this->getTable()
->where('failed_at', '<', $before);

$totalDeleted = 0;

do {
$deleted = $query->take(1000)->delete();

$totalDeleted += $deleted;
} while ($deleted !== 0);

return $totalDeleted;
}
}
16 changes: 16 additions & 0 deletions src/Illuminate/Queue/Failed/PrunableFailedJobProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Illuminate\Queue\Failed;

use DateTimeInterface;

interface PrunableFailedJobProvider
{
/**
* Prune all of the entries older than the given date.
*
* @param \DateTimeInterface $before
* @return int
*/
public function prune(DateTimeInterface $before);
}