Skip to content

[FEAT-74] Add command to clean exceptions #74 #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/moonguard.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
*/
'notify_expiring_soon_if_certificate_expires_within_days' => 7,
],
'exception_deletion' => [
/*
* Enable or disable exception deletion globally.
*/
'enabled' => true,

/*
* The age in minutes of the exceptions to delete.
*/
'delete_exceptions_older_than_minutes' => 10080,
],
'exceptions' => [
/*
* Enable or disable exception logging globally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<span class="text-gray-500">Uptime</span>
@if(! $site->uptimeCheck)
<span class="text-gray-500">---</span>
@elsef(! $site->uptimeCheck?->is_enabled || ! $site->uptime_check_enabled)
@elseif(! $site->uptimeCheck?->is_enabled || ! $site->uptime_check_enabled)
<span class="text-gray-400">Disabled</span>
@elseif($site->uptimeCheck->status === \Taecontrol\MoonGuard\Enums\UptimeStatus::NOT_YET_CHECKED)
<span class="rounded-full h-4 w-4 bg-gray-300"></span>
Expand Down
48 changes: 48 additions & 0 deletions src/Console/Commands/DeleteOldExceptionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Taecontrol\MoonGuard\Console\Commands;

use Illuminate\Console\Command;
use Taecontrol\MoonGuard\Repositories\ExceptionLogGroupRepository;

class DeleteOldExceptionCommand extends Command
{
protected $signature = 'exception:delete';

protected $description = 'Delete old exceptions';

public function handle()
{
if (! $this->isEnabled()) {
$this->info('Exception deletion is disabled. If you want to enable it, check the moonguard config file.');

return;
}

$this->info('Starting deletion of old exceptions...');

$time = $this->getExceptionAge();

$this->info('Old exceptions deleted successfully!');

$this->deleteOldExceptions($time);
}

public function isEnabled(): bool
{
return config('moonguard.exception_deletion.enabled');
}

public static function getExceptionAge(): int
{
return config('moonguard.exception_deletion.delete_exceptions_older_than_minutes');
}

public static function deleteOldExceptions(int $time): void
{
$exceptions = ExceptionLogGroupRepository::query()
->where('first_seen', '<', now()->subMinutes($time));

$exceptions->delete();
}
}
10 changes: 9 additions & 1 deletion src/Console/MoonGuardCommandsScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

use Illuminate\Console\Scheduling\Schedule;
use Taecontrol\MoonGuard\Console\Commands\CheckUptimeCommand;
use Taecontrol\MoonGuard\Console\Commands\DeleteOldExceptionCommand;
use Taecontrol\MoonGuard\Console\Commands\CheckSslCertificateCommand;

class MoonGuardCommandsScheduler
{
public static function scheduleMoonGuardCommands(Schedule $schedule, string $uptimeCheckCron, string $sslCertificateCheckCron)
public static function scheduleMoonGuardCommands(Schedule $schedule, string $uptimeCheckCron, string $sslCertificateCheckCron, string $deleteOldExceptionCron)
{
/** @var bool $uptimeCheckIsEnabled */
$uptimeCheckIsEnabled = config('moonguard.uptime_check.enabled');
/** @var bool $sslCheckIsEnabled */
$sslCheckIsEnabled = config('moonguard.ssl_certificate_check.enabled');
/** @var bool $deleteOldExceptionIsEnabled */
$deleteOldExceptionIsEnabled = config('moonguard.exception_deletion.enabled');

if ($uptimeCheckIsEnabled) {
$schedule->command(CheckUptimeCommand::class)
Expand All @@ -24,5 +27,10 @@ public static function scheduleMoonGuardCommands(Schedule $schedule, string $upt
$schedule->command(CheckSslCertificateCommand::class)
->cron($sslCertificateCheckCron);
}

if ($deleteOldExceptionIsEnabled) {
$schedule->command(DeleteOldExceptionCommand::class)
->cron($deleteOldExceptionCron);
}
}
}
2 changes: 2 additions & 0 deletions src/Providers/MoonGuardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Taecontrol\MoonGuard\Console\Commands\CheckUptimeCommand;
use Taecontrol\MoonGuard\Contracts\MoonGuardExceptionLogGroup;
use Taecontrol\MoonGuard\Contracts\MoonGuardSslCertificateCheck;
use Taecontrol\MoonGuard\Console\Commands\DeleteOldExceptionCommand;
use Taecontrol\MoonGuard\Console\Commands\CheckSslCertificateCommand;

class MoonGuardServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -61,6 +62,7 @@ protected function publishCommands(): void
$this->commands([
CheckUptimeCommand::class,
CheckSslCertificateCommand::class,
DeleteOldExceptionCommand::class,
]);
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/Command/DeleteOldExceptionCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Taecontrol\MoonGuard\Tests\Feature\Command;

use Taecontrol\MoonGuard\Tests\TestCase;
use Taecontrol\MoonGuard\Models\ExceptionLogGroup;

class DeleteOldExceptionCommandTest extends TestCase
{
/** @test */
public function test_command_runs_successfully()
{
$this->artisan('exception:delete')
->assertExitCode(0);
}

/** @test */
public function test_old_exceptions_are_deleted()
{
config(['moonguard.exception_deletion.enabled' => true]);
config(['moonguard.exception_deletion.delete_exceptions_older_than_minutes' => 20]);

$oldException = ExceptionLogGroup::factory()->create([
'first_seen' => now()->subMinutes(25),
]);

$this->artisan('exception:delete')->assertExitCode(0);

$this->assertDatabaseMissing('exception_log_groups', ['id' => $oldException->id]);

$this->assertEmpty(ExceptionLogGroup::all());
}
}