Skip to content

Commit e15985e

Browse files
committed
Add support for automatically pruning activity logs
1 parent 9b7af02 commit e15985e

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

app/Console/Kernel.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace Pterodactyl\Console;
44

5+
use Pterodactyl\Models\ActivityLog;
56
use Illuminate\Console\Scheduling\Schedule;
7+
use Illuminate\Database\Console\PruneCommand;
68
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
9+
use Pterodactyl\Console\Commands\Schedule\ProcessRunnableCommand;
10+
use Pterodactyl\Console\Commands\Maintenance\PruneOrphanedBackupsCommand;
11+
use Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand;
712

813
class Kernel extends ConsoleKernel
914
{
@@ -21,14 +26,16 @@ protected function commands()
2126
protected function schedule(Schedule $schedule)
2227
{
2328
// Execute scheduled commands for servers every minute, as if there was a normal cron running.
24-
$schedule->command('p:schedule:process')->everyMinute()->withoutOverlapping();
29+
$schedule->command(ProcessRunnableCommand::class)->everyMinute()->withoutOverlapping();
30+
$schedule->command(CleanServiceBackupFilesCommand::class)->daily();
2531

2632
if (config('backups.prune_age')) {
2733
// Every 30 minutes, run the backup pruning command so that any abandoned backups can be deleted.
28-
$schedule->command('p:maintenance:prune-backups')->everyThirtyMinutes();
34+
$schedule->command(PruneOrphanedBackupsCommand::class)->everyThirtyMinutes();
2935
}
3036

31-
// Every day cleanup any internal backups of service files.
32-
$schedule->command('p:maintenance:clean-service-backups')->daily();
37+
if (config('activity.prune_days')) {
38+
$schedule->command(PruneCommand::class, ['--model' => [ActivityLog::class]])->daily();
39+
}
3340
}
3441
}

app/Models/ActivityLog.php

+19
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Pterodactyl\Models;
44

5+
use Carbon\Carbon;
6+
use LogicException;
57
use Illuminate\Support\Facades\Event;
68
use Pterodactyl\Events\ActivityLogged;
79
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Database\Eloquent\MassPrunable;
811
use Illuminate\Database\Eloquent\Relations\MorphTo;
912
use Illuminate\Database\Eloquent\Model as IlluminateModel;
1013

@@ -42,6 +45,8 @@
4245
*/
4346
class ActivityLog extends Model
4447
{
48+
use MassPrunable;
49+
4550
public $timestamps = false;
4651

4752
protected $guarded = [
@@ -86,6 +91,20 @@ public function scopeForActor(Builder $builder, IlluminateModel $actor): Builder
8691
return $builder->whereMorphedTo('actor', $actor);
8792
}
8893

94+
/**
95+
* Returns models to be pruned.
96+
*
97+
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
98+
*/
99+
public function prunable()
100+
{
101+
if (is_null(config('activity.prune_days'))) {
102+
throw new LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.');
103+
}
104+
105+
return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days')));
106+
}
107+
89108
/**
90109
* Boots the model event listeners. This will trigger an activity log event every
91110
* time a new model is inserted which can then be captured and worked with as needed.

config/activity.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* The number of days that must elapse before old activity log entries are deleted
6+
* from the database.
7+
*/
8+
'prune_days' => env('APP_ACTIVITY_PRUNE_DAYS', 90),
9+
];

database/migrations/2022_05_29_140349_create_activity_log_actors_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create('activity_log_subjects', function (Blueprint $table) {
1717
$table->id();
18-
$table->foreignId('activity_log_id')->references('id')->on('activity_logs');
18+
$table->foreignId('activity_log_id')->references('id')->on('activity_logs')->cascadeOnDelete();
1919
$table->numericMorphs('subject');
2020
});
2121
}
@@ -27,6 +27,6 @@ public function up()
2727
*/
2828
public function down()
2929
{
30-
Schema::dropIfExists('activity_log_subject');
30+
Schema::dropIfExists('activity_log_subjects');
3131
}
3232
}

0 commit comments

Comments
 (0)