From e15985ea39de5de1b23dc4f950c62fac83c4ed79 Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sun, 29 May 2022 19:45:00 -0400 Subject: [PATCH] Add support for automatically pruning activity logs --- app/Console/Kernel.php | 15 +++++++++++---- app/Models/ActivityLog.php | 19 +++++++++++++++++++ config/activity.php | 9 +++++++++ ...40349_create_activity_log_actors_table.php | 4 ++-- 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 config/activity.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index d5fa25ec4..17b3d4de4 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,8 +2,13 @@ namespace Pterodactyl\Console; +use Pterodactyl\Models\ActivityLog; use Illuminate\Console\Scheduling\Schedule; +use Illuminate\Database\Console\PruneCommand; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; +use Pterodactyl\Console\Commands\Schedule\ProcessRunnableCommand; +use Pterodactyl\Console\Commands\Maintenance\PruneOrphanedBackupsCommand; +use Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand; class Kernel extends ConsoleKernel { @@ -21,14 +26,16 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule) { // Execute scheduled commands for servers every minute, as if there was a normal cron running. - $schedule->command('p:schedule:process')->everyMinute()->withoutOverlapping(); + $schedule->command(ProcessRunnableCommand::class)->everyMinute()->withoutOverlapping(); + $schedule->command(CleanServiceBackupFilesCommand::class)->daily(); if (config('backups.prune_age')) { // Every 30 minutes, run the backup pruning command so that any abandoned backups can be deleted. - $schedule->command('p:maintenance:prune-backups')->everyThirtyMinutes(); + $schedule->command(PruneOrphanedBackupsCommand::class)->everyThirtyMinutes(); } - // Every day cleanup any internal backups of service files. - $schedule->command('p:maintenance:clean-service-backups')->daily(); + if (config('activity.prune_days')) { + $schedule->command(PruneCommand::class, ['--model' => [ActivityLog::class]])->daily(); + } } } diff --git a/app/Models/ActivityLog.php b/app/Models/ActivityLog.php index 0681b21ca..4312e332e 100644 --- a/app/Models/ActivityLog.php +++ b/app/Models/ActivityLog.php @@ -2,9 +2,12 @@ namespace Pterodactyl\Models; +use Carbon\Carbon; +use LogicException; use Illuminate\Support\Facades\Event; use Pterodactyl\Events\ActivityLogged; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\MassPrunable; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Model as IlluminateModel; @@ -42,6 +45,8 @@ use Illuminate\Database\Eloquent\Model as IlluminateModel; */ class ActivityLog extends Model { + use MassPrunable; + public $timestamps = false; protected $guarded = [ @@ -86,6 +91,20 @@ class ActivityLog extends Model return $builder->whereMorphedTo('actor', $actor); } + /** + * Returns models to be pruned. + * + * @see https://laravel.com/docs/9.x/eloquent#pruning-models + */ + public function prunable() + { + if (is_null(config('activity.prune_days'))) { + throw new LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.'); + } + + return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days'))); + } + /** * Boots the model event listeners. This will trigger an activity log event every * time a new model is inserted which can then be captured and worked with as needed. diff --git a/config/activity.php b/config/activity.php new file mode 100644 index 000000000..642be0630 --- /dev/null +++ b/config/activity.php @@ -0,0 +1,9 @@ + env('APP_ACTIVITY_PRUNE_DAYS', 90), +]; diff --git a/database/migrations/2022_05_29_140349_create_activity_log_actors_table.php b/database/migrations/2022_05_29_140349_create_activity_log_actors_table.php index 8be57bc1c..6dc45d7f8 100644 --- a/database/migrations/2022_05_29_140349_create_activity_log_actors_table.php +++ b/database/migrations/2022_05_29_140349_create_activity_log_actors_table.php @@ -15,7 +15,7 @@ class CreateActivityLogActorsTable extends Migration { Schema::create('activity_log_subjects', function (Blueprint $table) { $table->id(); - $table->foreignId('activity_log_id')->references('id')->on('activity_logs'); + $table->foreignId('activity_log_id')->references('id')->on('activity_logs')->cascadeOnDelete(); $table->numericMorphs('subject'); }); } @@ -27,6 +27,6 @@ class CreateActivityLogActorsTable extends Migration */ public function down() { - Schema::dropIfExists('activity_log_subject'); + Schema::dropIfExists('activity_log_subjects'); } }