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 command to clean batches table #35694

Merged
merged 5 commits into from Dec 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Illuminate/Bus/DatabaseBatchRepository.php
Expand Up @@ -4,11 +4,12 @@

use Carbon\CarbonImmutable;
use Closure;
use DateTimeInterface;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Str;

class DatabaseBatchRepository implements BatchRepository
class DatabaseBatchRepository implements BatchRepository, Prunable
{
/**
* The batch factory instance.
Expand Down Expand Up @@ -243,6 +244,29 @@ public function transaction(Closure $callback)
});
}

/**
* Prune all of the entries older than the given date.
*
* @param DateTimeInterface $before
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing slash

* @return int
*/
public function prune(DateTimeInterface $before)
{
$query = $this->connection->table($this->table)
->whereNotNull('finished_at')
->where('finished_at', '<', $before->getTimestamp());

$totalDeleted = 0;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you just use the query builder's each function?


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

return $totalDeleted;
}

/**
* Convert the given raw batch to a Batch object.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Bus/Prunable.php
@@ -0,0 +1,16 @@
<?php

namespace Illuminate\Bus;

use DateTimeInterface;

interface Prunable
{
/**
* Prune all of the entries older than the given date.
*
* @param DateTimeInterface $before
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing slash

* @return int
*/
public function prune(DateTimeInterface $before);
}
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Expand Up @@ -67,6 +67,7 @@
use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand;
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\RestartCommand as QueueRestartCommand;
use Illuminate\Queue\Console\RetryBatchCommand as QueueRetryBatchCommand;
use Illuminate\Queue\Console\RetryCommand as QueueRetryCommand;
Expand Down Expand Up @@ -107,6 +108,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
'QueueFlush' => 'command.queue.flush',
'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',
Expand Down Expand Up @@ -684,6 +686,18 @@ protected function registerQueueListenCommand()
});
}

/**
* Register the command.
*
* @return void
*/
protected function registerQueuePruneBatchesCommand()
{
$this->app->singleton('command.queue.prune-batches', function () {
return new PruneBatchesQueueCommand;
});
}

/**
* Register the command.
*
Expand Down
47 changes: 47 additions & 0 deletions src/Illuminate/Queue/Console/PruneBatchesCommand.php
@@ -0,0 +1,47 @@
<?php

namespace Illuminate\Queue\Console;

use Carbon\Carbon;
use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\Prunable;
use Illuminate\Console\Command;

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

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

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$count = 0;

$repository = $this->laravel[BatchRepository::class];

if ($repository instanceof Prunable) {
$hours = $this->option('hours');

$before = Carbon::now()->subHours($hours);

$count = $repository->prune($before);
}

$this->info("{$count} entries deleted!");
}
}