Repository navigation
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d08042f
[FEAT-74] Add command to clean exceptions #74
w1ls0nv3l dc8890a
php-cs-fixer fix
w1ls0nv3l 0ed69f0
delete log
w1ls0nv3l d257a37
requested changes
w1ls0nv3l 5238fad
php-cs-fixer fix
w1ls0nv3l 4dd597e
requested changes
w1ls0nv3l 88d2877
Create DeleteOldExceptionCommandTest
w1ls0nv3l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.