diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index a0dd7067b555..ab8078a191b5 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -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; @@ -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', @@ -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. * diff --git a/src/Illuminate/Queue/Console/PruneFailedJobsCommand.php b/src/Illuminate/Queue/Console/PruneFailedJobsCommand.php new file mode 100644 index 000000000000..d613d63561f4 --- /dev/null +++ b/src/Illuminate/Queue/Console/PruneFailedJobsCommand.php @@ -0,0 +1,43 @@ +laravel['queue.failer']; + + $count = 0; + + if ($failer instanceof PrunableFailedJobProvider) { + $count = $failer->prune(Carbon::now()->subHours($this->option('hours'))); + } + + $this->info("{$count} entries deleted!"); + } +} diff --git a/src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php b/src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php index 1a634f760d89..7eeacba6ed94 100644 --- a/src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php +++ b/src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php @@ -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. @@ -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; + } } diff --git a/src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php b/src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php index f452bf4ba22a..90b2d0ced86d 100644 --- a/src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php +++ b/src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php @@ -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. @@ -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; + } } diff --git a/src/Illuminate/Queue/Failed/PrunableFailedJobProvider.php b/src/Illuminate/Queue/Failed/PrunableFailedJobProvider.php new file mode 100644 index 000000000000..ea505b0cdfa4 --- /dev/null +++ b/src/Illuminate/Queue/Failed/PrunableFailedJobProvider.php @@ -0,0 +1,16 @@ +