Add support for automatically pruning activity logs

This commit is contained in:
DaneEveritt 2022-05-29 19:45:00 -04:00
parent 9b7af02690
commit e15985ea39
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 41 additions and 6 deletions

View file

@ -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();
}
}
}

View file

@ -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.

9
config/activity.php Normal file
View file

@ -0,0 +1,9 @@
<?php
return [
/*
* The number of days that must elapse before old activity log entries are deleted
* from the database.
*/
'prune_days' => env('APP_ACTIVITY_PRUNE_DAYS', 90),
];

View file

@ -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');
}
}