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 4 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
24 changes: 24 additions & 0 deletions src/Illuminate/Bus/DatabaseBatchRepository.php
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonImmutable;
use Closure;
use DateTimeInterface;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Str;
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
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Expand Up @@ -63,6 +63,7 @@
use Illuminate\Queue\Console\BatchesTableCommand;
use Illuminate\Queue\Console\ClearCommand as QueueClearCommand;
use Illuminate\Queue\Console\FailedTableCommand;
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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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.
*
Expand Down
46 changes: 46 additions & 0 deletions src/Illuminate/Queue/Console/FlushBatchCommand.php
@@ -0,0 +1,46 @@
<?php

namespace Illuminate\Queue\Console;

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

class FlushBatchCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'queue:flush-batch {--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 (method_exists($repository, 'prune')) {
dmason30 marked this conversation as resolved.
Show resolved Hide resolved
$hours = $this->option('hours');

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

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

$this->info("{$count} entries deleted!");
}
}
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonImmutable;
use Closure;
use DateTimeInterface;
use Illuminate\Bus\Batch;
use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\PendingBatch;
Expand Down Expand Up @@ -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;
}
}