feat: ✨ add job to clean activity logs older than one year #96
This commit is contained in:
parent
9e7cfc2a76
commit
a50ca7f133
3 changed files with 96 additions and 0 deletions
46
app/Console/Commands/CleanOldActivities.php
Normal file
46
app/Console/Commands/CleanOldActivities.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\CleanOldActivities as CleanOldActivitiesJob;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class CleanOldActivities extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'uwil:activity-logs:clean {--force : Delete all activity logs regardless of date.}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Clean all logs older than one year. Use --force to delete all logs.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$messageTitle = "<comment>activity logs</comment>";
|
||||
$this->components->info("Cleaning {$messageTitle}...");
|
||||
|
||||
$force = $this->option('force');
|
||||
|
||||
if ($force && !$this->confirm('Are you sure you want to delete all activity logs ? This cannot be undone.')) {
|
||||
$this->components->error("Clean of {$messageTitle} aborted.");
|
||||
return;
|
||||
}
|
||||
|
||||
(new CleanOldActivitiesJob(force: $force))->handle();
|
||||
|
||||
$messageContent = $force ? "All {$messageTitle} deleted" : "Old {$messageTitle} cleaned";
|
||||
$this->components->success($messageContent);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\CleanOldActivities;
|
||||
use App\Jobs\UpdatePlanificatedStatus;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
|
@ -16,6 +17,7 @@ class Kernel extends ConsoleKernel
|
|||
*/
|
||||
protected function schedule(Schedule $schedule): void
|
||||
{
|
||||
$schedule->job(CleanOldActivities::class)->daily();
|
||||
$schedule->job(UpdatePlanificatedStatus::class)->everyFiveMinutes();
|
||||
}
|
||||
|
||||
|
|
|
|||
48
app/Jobs/CleanOldActivities.php
Normal file
48
app/Jobs/CleanOldActivities.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\ActivityLog;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
/**
|
||||
* Permanently deletes entries that have been stored for more than one year.
|
||||
*/
|
||||
class CleanOldActivities implements ShouldQueue, ShouldBeUnique
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* Create the controller instance.
|
||||
*
|
||||
* @param boolean $force
|
||||
*/
|
||||
public function __construct(public readonly bool $force = false)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete entries if it has been stored for more than one year.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$query = ActivityLog::query();
|
||||
|
||||
if (!$this->force) {
|
||||
$query->where('created_at', '<', Carbon::now()->subYear());
|
||||
}
|
||||
|
||||
$query->cursor()->each(fn($model) => $model->delete());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue