Skip to content

Commit

Permalink
added new purge commands, scheduled purge task to run daily
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Jan 13, 2025
1 parent 67e84aa commit 08d6c24
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.5.22",
"version": "1.5.23",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
51 changes: 51 additions & 0 deletions src/Console/Commands/PurgeActivityLogs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Fleetbase\Console\Commands;

use Fleetbase\Traits\PurgeCommand;
use Illuminate\Console\Command;
use Spatie\Activitylog\Models\Activity;

class PurgeActivityLogs extends Command
{
use PurgeCommand;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'purge:activity-logs
{--days=30 : The number of days to preserve logs (default: 30)}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Purges Activity logs older than the specified number of days, with an option to back up records before deletion.';

/**
* Execute the console command.
*/
public function handle(): void
{
// Determine the number of days to preserve
$days = (int) $this->option('days');
if ($days <= 0) {
$this->error('The number of days must be a positive integer.');

return;
}

$this->info("Purging Activity logs older than {$days} days...");

// Calculate the cutoff date
$cutoffDate = now()->subDays($days);

// Backup and purge logs
$this->backupAndDelete(Activity::class, 'activity', $cutoffDate, 'backups/activity-logs');

$this->info('Purge completed successfully.');
}
}
51 changes: 51 additions & 0 deletions src/Console/Commands/PurgeScheduledTaskLogs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Fleetbase\Console\Commands;

use Fleetbase\Traits\PurgeCommand;
use Illuminate\Console\Command;
use Spatie\ScheduleMonitor\Models\MonitoredScheduledTaskLogItem;

class PurgeScheduledTaskLogs extends Command
{
use PurgeCommand;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'purge:scheduled-task-logs
{--days=30 : The number of days to preserve logs (default: 30)}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Purges Activity logs older than the specified number of days, with an option to back up records before deletion.';

/**
* Execute the console command.
*/
public function handle(): void
{
// Determine the number of days to preserve
$days = (int) $this->option('days');
if ($days <= 0) {
$this->error('The number of days must be a positive integer.');

return;
}

$this->info("Purging Scheduled Task logs older than {$days} days...");

// Calculate the cutoff date
$cutoffDate = now()->subDays($days);

// Backup and purge logs
$this->backupAndDelete(MonitoredScheduledTaskLogItem::class, 'monitored_scheduled_task_log_items', $cutoffDate, 'backups/scheduled-task-logs');

$this->info('Purge completed successfully.');
}
}
6 changes: 6 additions & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class CoreServiceProvider extends ServiceProvider
\Fleetbase\Console\Commands\FixUserCompanies::class,
\Fleetbase\Console\Commands\PurgeApiLogs::class,
\Fleetbase\Console\Commands\PurgeWebhookLogs::class,
\Fleetbase\Console\Commands\PurgeActivityLogs::class,
\Fleetbase\Console\Commands\PurgeScheduledTaskLogs::class,
\Fleetbase\Console\Commands\BackupDatabase\MysqlS3Backup::class,
];

Expand Down Expand Up @@ -126,6 +128,10 @@ public function boot()
$this->scheduleCommands(function ($schedule) {
$schedule->command('cache:prune-stale-tags')->hourly();
$schedule->command('model:prune', ['--model' => MonitoredScheduledTaskLogItem::class])->daily();
$schedule->command('purge:api-logs --no-interaction')->daily();
$schedule->command('purge:webhook-logs --no-interaction')->daily();
$schedule->command('purge:activity-logs --no-interaction')->daily();
$schedule->command('purge:scheduled-task-logs --no-interaction')->daily();
});
$this->registerObservers();
$this->registerExpansionsFrom();
Expand Down
18 changes: 15 additions & 3 deletions src/Traits/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fleetbase\Traits;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;

trait PurgeCommand
Expand All @@ -15,7 +16,18 @@ trait PurgeCommand
protected function backupAndDelete(string $modelClass, string $tableName, $cutoffDate, string $path = 'backups'): void
{
// Fetch records older than the cutoff date
$records = $modelClass::where('created_at', '<', $cutoffDate)->withTrashed()->get();
$query = $modelClass::where('created_at', '<', $cutoffDate);

// Include trashed if possible
if (Schema::hasColumn($tableName, 'deleted_at')) {
$query->where(function ($query) {
$query->whereNull('deleted_at');
$query->orWhereNotNull('deleted_at');
});
}

// Get records
$records = $query->get();

if ($records->isEmpty()) {
$this->info("No records to purge from {$tableName}.");
Expand Down Expand Up @@ -55,8 +67,8 @@ protected function backupAndDelete(string $modelClass, string $tableName, $cutof
$this->info("Purged records from {$tableName}.");

// Reset auto-increment index
$this->resetTableIndex($tableName);
$this->info("Reset auto-increment index for {$tableName}.");
// $this->resetTableIndex($tableName);
// $this->info("Reset auto-increment index for {$tableName}.");
}

/**
Expand Down

0 comments on commit 08d6c24

Please sign in to comment.