From 3b87b6f972de4445bed7458c810671f2bbc861cb Mon Sep 17 00:00:00 2001 From: dmason30 Date: Tue, 22 Dec 2020 15:11:18 +0000 Subject: [PATCH 1/5] Add command to clean batches table --- src/Illuminate/Bus/BatchRepository.php | 8 ++++ .../Bus/DatabaseBatchRepository.php | 22 ++++++++++ .../Providers/ArtisanServiceProvider.php | 14 +++++++ .../Queue/Console/FlushBatchCommand.php | 40 +++++++++++++++++++ .../Testing/Fakes/BatchRepositoryFake.php | 12 ++++++ 5 files changed, 96 insertions(+) create mode 100644 src/Illuminate/Queue/Console/FlushBatchCommand.php diff --git a/src/Illuminate/Bus/BatchRepository.php b/src/Illuminate/Bus/BatchRepository.php index 098ccef20ed6..978da8204843 100644 --- a/src/Illuminate/Bus/BatchRepository.php +++ b/src/Illuminate/Bus/BatchRepository.php @@ -89,4 +89,12 @@ public function delete(string $batchId); * @return mixed */ public function transaction(Closure $callback); + + /** + * Prune all of the entries older than the given date. + * + * @param \DateTimeInterface $before + * @return int + */ + public function prune(\DateTimeInterface $before); } diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index d911c380d551..a1efdb459654 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -243,6 +243,28 @@ public function transaction(Closure $callback) }); } + /** + * Prune all of the entries older than the given date. + * + * @param \DateTimeInterface $before + * @return int + */ + public function prune(\DateTimeInterface $before) + { + $query = $this->connection->table($this->table) + ->where('finished_at', '<', $before); + + $totalDeleted = 0; + + do { + $deleted = $query->take(1000)->delete(); + + $totalDeleted += $deleted; + } while ($deleted !== 0); + + return $totalDeleted; + } + /** * Convert the given raw batch to a Batch object. * diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index 9cce44c00e3f..ec4d7a0ca64b 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -64,6 +64,7 @@ use Illuminate\Queue\Console\ClearCommand as QueueClearCommand; use Illuminate\Queue\Console\FailedTableCommand; use Illuminate\Queue\Console\FlushFailedCommand as FlushFailedQueueCommand; +use Illuminate\Queue\Console\FlushBatchCommand as FlushBatchQueueCommand; use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand; use Illuminate\Queue\Console\ListenCommand as QueueListenCommand; use Illuminate\Queue\Console\ListFailedCommand as ListFailedQueueCommand; @@ -105,6 +106,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid 'QueueClear' => 'command.queue.clear', 'QueueFailed' => 'command.queue.failed', 'QueueFlush' => 'command.queue.flush', + 'QueueFlushBatch' => 'command.queue.flush-batch', 'QueueForget' => 'command.queue.forget', 'QueueListen' => 'command.queue.listen', 'QueueRestart' => 'command.queue.restart', @@ -672,6 +674,18 @@ protected function registerQueueFlushCommand() }); } + /** + * Register the command. + * + * @return void + */ + protected function registerQueueFlushBatchCommand() + { + $this->app->singleton('command.queue.flush-batch', function () { + return new FlushBatchQueueCommand; + }); + } + /** * Register the command. * diff --git a/src/Illuminate/Queue/Console/FlushBatchCommand.php b/src/Illuminate/Queue/Console/FlushBatchCommand.php new file mode 100644 index 000000000000..edfefb03fb1f --- /dev/null +++ b/src/Illuminate/Queue/Console/FlushBatchCommand.php @@ -0,0 +1,40 @@ +option('hours'); + + $before = Carbon::now()->subHours($hours); + + $count = $this->laravel[BatchRepository::class]->prune($before); + + $this->info("{$count} entries deleted successfully."); + } +} diff --git a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php index 55681f4d5534..c3533fa2ea2e 100644 --- a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php @@ -8,6 +8,7 @@ use Illuminate\Bus\BatchRepository; use Illuminate\Bus\PendingBatch; use Illuminate\Bus\UpdatedBatchJobCounts; +use Illuminate\Database\Query\Builder; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Str; @@ -134,4 +135,15 @@ public function transaction(Closure $callback) { return $callback(); } + + /** + * Prune all of the entries older than the given date. + * + * @param \DateTimeInterface $before + * @return int + */ + public function prune(\DateTimeInterface $before) + { + return 0; + } } From a41160515d626f5957129db8ed0c3a5601567dad Mon Sep 17 00:00:00 2001 From: dmason30 Date: Tue, 22 Dec 2020 15:33:14 +0000 Subject: [PATCH 2/5] use timestamp --- src/Illuminate/Bus/DatabaseBatchRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index a1efdb459654..cc56ead8d649 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -252,7 +252,7 @@ public function transaction(Closure $callback) public function prune(\DateTimeInterface $before) { $query = $this->connection->table($this->table) - ->where('finished_at', '<', $before); + ->where('finished_at', '<', $before->getTimestamp()); $totalDeleted = 0; From ee8c0b5abe54a892fb51df1a9ea8337d6d5851d4 Mon Sep 17 00:00:00 2001 From: dmason30 Date: Tue, 22 Dec 2020 15:44:51 +0000 Subject: [PATCH 3/5] linting --- src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php | 2 +- src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index ec4d7a0ca64b..fc46527915a0 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -63,8 +63,8 @@ use Illuminate\Queue\Console\BatchesTableCommand; use Illuminate\Queue\Console\ClearCommand as QueueClearCommand; use Illuminate\Queue\Console\FailedTableCommand; -use Illuminate\Queue\Console\FlushFailedCommand as FlushFailedQueueCommand; use Illuminate\Queue\Console\FlushBatchCommand as FlushBatchQueueCommand; +use Illuminate\Queue\Console\FlushFailedCommand as FlushFailedQueueCommand; use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand; use Illuminate\Queue\Console\ListenCommand as QueueListenCommand; use Illuminate\Queue\Console\ListFailedCommand as ListFailedQueueCommand; diff --git a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php index c3533fa2ea2e..41850ab334f0 100644 --- a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php @@ -8,7 +8,6 @@ use Illuminate\Bus\BatchRepository; use Illuminate\Bus\PendingBatch; use Illuminate\Bus\UpdatedBatchJobCounts; -use Illuminate\Database\Query\Builder; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Str; From 910fad8cb2934f2c231f806b8b398ba95ef38dfe Mon Sep 17 00:00:00 2001 From: dmason30 Date: Tue, 22 Dec 2020 16:22:59 +0000 Subject: [PATCH 4/5] Remove interface method and add import --- src/Illuminate/Bus/BatchRepository.php | 8 -------- src/Illuminate/Bus/DatabaseBatchRepository.php | 6 ++++-- .../Queue/Console/FlushBatchCommand.php | 16 +++++++++++----- .../Testing/Fakes/BatchRepositoryFake.php | 5 +++-- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Illuminate/Bus/BatchRepository.php b/src/Illuminate/Bus/BatchRepository.php index 978da8204843..098ccef20ed6 100644 --- a/src/Illuminate/Bus/BatchRepository.php +++ b/src/Illuminate/Bus/BatchRepository.php @@ -89,12 +89,4 @@ public function delete(string $batchId); * @return mixed */ public function transaction(Closure $callback); - - /** - * Prune all of the entries older than the given date. - * - * @param \DateTimeInterface $before - * @return int - */ - public function prune(\DateTimeInterface $before); } diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index cc56ead8d649..3f025b06f9f2 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -4,6 +4,7 @@ use Carbon\CarbonImmutable; use Closure; +use DateTimeInterface; use Illuminate\Database\Connection; use Illuminate\Database\Query\Expression; use Illuminate\Support\Str; @@ -246,12 +247,13 @@ public function transaction(Closure $callback) /** * Prune all of the entries older than the given date. * - * @param \DateTimeInterface $before + * @param DateTimeInterface $before * @return int */ - public function prune(\DateTimeInterface $before) + public function prune(DateTimeInterface $before) { $query = $this->connection->table($this->table) + ->whereNotNull('finished_at') ->where('finished_at', '<', $before->getTimestamp()); $totalDeleted = 0; diff --git a/src/Illuminate/Queue/Console/FlushBatchCommand.php b/src/Illuminate/Queue/Console/FlushBatchCommand.php index edfefb03fb1f..eaafe61908c7 100644 --- a/src/Illuminate/Queue/Console/FlushBatchCommand.php +++ b/src/Illuminate/Queue/Console/FlushBatchCommand.php @@ -25,16 +25,22 @@ class FlushBatchCommand extends Command /** * Execute the console command. * - * @return int|null + * @return void */ public function handle() { - $hours = $this->option('hours'); + $count = 0; - $before = Carbon::now()->subHours($hours); + $repository = $this->laravel[BatchRepository::class]; - $count = $this->laravel[BatchRepository::class]->prune($before); + if (method_exists($repository, 'prune')) { + $hours = $this->option('hours'); - $this->info("{$count} entries deleted successfully."); + $before = Carbon::now()->subHours($hours); + + $count = $repository->prune($before); + } + + $this->info("{$count} entries deleted!"); } } diff --git a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php index 41850ab334f0..7c109db7b882 100644 --- a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php @@ -4,6 +4,7 @@ use Carbon\CarbonImmutable; use Closure; +use DateTimeInterface; use Illuminate\Bus\Batch; use Illuminate\Bus\BatchRepository; use Illuminate\Bus\PendingBatch; @@ -138,10 +139,10 @@ public function transaction(Closure $callback) /** * Prune all of the entries older than the given date. * - * @param \DateTimeInterface $before + * @param DateTimeInterface $before * @return int */ - public function prune(\DateTimeInterface $before) + public function prune(DateTimeInterface $before) { return 0; } From 8dc442578670197c05e80be90fb711c427f3c99c Mon Sep 17 00:00:00 2001 From: dmason30 Date: Wed, 23 Dec 2020 01:05:02 +0000 Subject: [PATCH 5/5] Change command signature and add new interface --- src/Illuminate/Bus/DatabaseBatchRepository.php | 2 +- src/Illuminate/Bus/Prunable.php | 16 ++++++++++++++++ .../Providers/ArtisanServiceProvider.php | 16 ++++++++-------- ...hBatchCommand.php => PruneBatchesCommand.php} | 7 ++++--- .../Testing/Fakes/BatchRepositoryFake.php | 12 ------------ 5 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 src/Illuminate/Bus/Prunable.php rename src/Illuminate/Queue/Console/{FlushBatchCommand.php => PruneBatchesCommand.php} (78%) diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index 3f025b06f9f2..bfad496fc352 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -9,7 +9,7 @@ use Illuminate\Database\Query\Expression; use Illuminate\Support\Str; -class DatabaseBatchRepository implements BatchRepository +class DatabaseBatchRepository implements BatchRepository, Prunable { /** * The batch factory instance. diff --git a/src/Illuminate/Bus/Prunable.php b/src/Illuminate/Bus/Prunable.php new file mode 100644 index 000000000000..5441a00dc5bf --- /dev/null +++ b/src/Illuminate/Bus/Prunable.php @@ -0,0 +1,16 @@ + 'command.queue.clear', 'QueueFailed' => 'command.queue.failed', 'QueueFlush' => 'command.queue.flush', - 'QueueFlushBatch' => 'command.queue.flush-batch', 'QueueForget' => 'command.queue.forget', 'QueueListen' => 'command.queue.listen', + 'QueuePruneBatches' => 'command.queue.prune-batches', 'QueueRestart' => 'command.queue.restart', 'QueueRetry' => 'command.queue.retry', 'QueueRetryBatch' => 'command.queue.retry-batch', @@ -679,10 +679,10 @@ protected function registerQueueFlushCommand() * * @return void */ - protected function registerQueueFlushBatchCommand() + protected function registerQueueListenCommand() { - $this->app->singleton('command.queue.flush-batch', function () { - return new FlushBatchQueueCommand; + $this->app->singleton('command.queue.listen', function ($app) { + return new QueueListenCommand($app['queue.listener']); }); } @@ -691,10 +691,10 @@ protected function registerQueueFlushBatchCommand() * * @return void */ - protected function registerQueueListenCommand() + protected function registerQueuePruneBatchesCommand() { - $this->app->singleton('command.queue.listen', function ($app) { - return new QueueListenCommand($app['queue.listener']); + $this->app->singleton('command.queue.prune-batches', function () { + return new PruneBatchesQueueCommand; }); } diff --git a/src/Illuminate/Queue/Console/FlushBatchCommand.php b/src/Illuminate/Queue/Console/PruneBatchesCommand.php similarity index 78% rename from src/Illuminate/Queue/Console/FlushBatchCommand.php rename to src/Illuminate/Queue/Console/PruneBatchesCommand.php index eaafe61908c7..d30b6fdb0e8d 100644 --- a/src/Illuminate/Queue/Console/FlushBatchCommand.php +++ b/src/Illuminate/Queue/Console/PruneBatchesCommand.php @@ -4,16 +4,17 @@ use Carbon\Carbon; use Illuminate\Bus\BatchRepository; +use Illuminate\Bus\Prunable; use Illuminate\Console\Command; -class FlushBatchCommand extends Command +class PruneBatchesCommand extends Command { /** * The console command signature. * * @var string */ - protected $signature = 'queue:flush-batch {--hours=24 : The number of hours to retain batch data}'; + protected $signature = 'queue:prune-batches {--hours=24 : The number of hours to retain batch data}'; /** * The console command description. @@ -33,7 +34,7 @@ public function handle() $repository = $this->laravel[BatchRepository::class]; - if (method_exists($repository, 'prune')) { + if ($repository instanceof Prunable) { $hours = $this->option('hours'); $before = Carbon::now()->subHours($hours); diff --git a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php index 7c109db7b882..55681f4d5534 100644 --- a/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php @@ -4,7 +4,6 @@ use Carbon\CarbonImmutable; use Closure; -use DateTimeInterface; use Illuminate\Bus\Batch; use Illuminate\Bus\BatchRepository; use Illuminate\Bus\PendingBatch; @@ -135,15 +134,4 @@ public function transaction(Closure $callback) { return $callback(); } - - /** - * Prune all of the entries older than the given date. - * - * @param DateTimeInterface $before - * @return int - */ - public function prune(DateTimeInterface $before) - { - return 0; - } }