-
Notifications
You must be signed in to change notification settings - Fork 0
Release v0.1.21 — Phase 21: Cleanup & Retention #293
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
36 commits
Select commit
Hold shift + click to select a range
a609138
CONV-330: Create FileRetentionPolicy service
menvil d9b0c65
Merge feature/CONV-330-create-file-retention-policy-service
menvil b0867c0
CONV-331: Test upload file expiration by plan
menvil b742184
Merge feature/CONV-331-test-upload-file-expiration-by-plan
menvil a5ef100
CONV-332: Apply expiration to uploaded files
menvil ed0d708
Merge feature/CONV-332-apply-expiration-to-uploaded-files
menvil 8077a59
CONV-333: Test result file expiration by plan
menvil ff9adbb
Merge feature/CONV-333-test-result-file-expiration-by-plan
menvil 0e32432
CONV-334: Apply expiration to conversion result files
menvil ae1fd79
Merge feature/CONV-334-apply-expiration-to-conversion-result-files
menvil 52346c9
CONV-335: Add expired file status handling
menvil 355f280
Merge feature/CONV-335-add-expired-file-status-handling
menvil 16f085a
CONV-336: Create CleanupExpiredFilesJob skeleton
menvil e966b7c
Merge feature/CONV-336-create-cleanup-expired-files-job-skeleton
menvil dd30db5
CONV-337: Test cleanup deletes expired physical files
menvil bef7f65
Merge feature/CONV-337-test-cleanup-deletes-expired-physical-files
menvil c266318
CONV-338: Implement expired physical file deletion
menvil fade7d7
Merge feature/CONV-338-implement-expired-physical-file-deletion
menvil 7121ea8
CONV-339: Test cleanup marks file records expired
menvil 77ffa0b
Merge feature/CONV-339-test-cleanup-marks-file-records-expired
menvil a786632
CONV-340: Implement expired file record marking
menvil fdeb463
Merge feature/CONV-340-implement-expired-file-record-marking
menvil cc1366c
CONV-341: Add manual cleanup command
menvil 2ffb7d3
Merge feature/CONV-341-add-manual-cleanup-command
menvil 59d071a
CONV-342: Schedule expired files cleanup
menvil b31d1d5
Merge feature/CONV-342-schedule-expired-files-cleanup
menvil f95d3f2
CONV-343: Block expired web downloads
menvil 8d11a3f
Merge feature/CONV-343-block-expired-web-downloads
menvil bea4aaf
CONV-344: Block expired API downloads
menvil 4cf8592
Merge feature/CONV-344-block-expired-api-downloads
menvil 5340e58
CONV-345: Show retention and expiration info in UI
menvil 9e5a67e
Merge feature/CONV-345-show-retention-and-expiration-info-in-ui
menvil 6048196
CONV-346: Add cleanup and retention final smoke tests
menvil b495fd5
Merge feature/CONV-346-add-cleanup-and-retention-final-smoke-tests
menvil 6297662
Fix cleanup disk hardcoded to local and Carbon test time leak
menvil 5bc372f
Fix review findings: command messaging, job deletion guard, retention…
menvil 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Console\Commands; | ||
|
|
||
| use App\Jobs\CleanupExpiredFilesJob; | ||
| use Illuminate\Console\Command; | ||
|
|
||
| final class CleanupExpiredFilesCommand extends Command | ||
| { | ||
| protected $signature = 'files:cleanup-expired {--sync : Run cleanup synchronously instead of dispatching a queue job}'; | ||
|
|
||
| protected $description = 'Delete expired physical files and mark their records as expired'; | ||
|
|
||
| public function handle(): int | ||
| { | ||
| if ($this->option('sync')) { | ||
| app(CleanupExpiredFilesJob::class)->handle(); | ||
| $this->info('Expired files cleanup completed.'); | ||
| } else { | ||
| CleanupExpiredFilesJob::dispatch(); | ||
| $this->info('Expired files cleanup queued.'); | ||
| } | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
| } |
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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Exceptions\Files; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| final class InvalidRetentionPolicyException extends RuntimeException | ||
| { | ||
| public static function forInvalidDays(int|string|null $raw): self | ||
| { | ||
| return new self('Retention days must be a positive integer, got ['.var_export($raw, true).'].'); | ||
| } | ||
| } |
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,47 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Jobs; | ||
|
|
||
| use App\Enums\FileStatus; | ||
| use App\Models\FileRecord; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Foundation\Queue\Queueable; | ||
| use Illuminate\Support\Facades\Log; | ||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| final class CleanupExpiredFilesJob implements ShouldQueue | ||
| { | ||
| use Queueable; | ||
|
|
||
| public function handle(): void | ||
| { | ||
| FileRecord::query() | ||
| ->whereNotNull('expires_at') | ||
| ->where('expires_at', '<=', now()) | ||
| ->whereNotIn('status', [FileStatus::Expired, FileStatus::Deleted]) | ||
| ->chunkById(100, function ($files): void { | ||
| foreach ($files as $file) { | ||
| if ($file->stored_path) { | ||
| $exists = Storage::disk('local')->exists($file->stored_path); | ||
|
|
||
| if ($exists) { | ||
| $deleted = Storage::disk('local')->delete($file->stored_path); | ||
|
|
||
| if (! $deleted) { | ||
| Log::warning('CleanupExpiredFilesJob: failed to delete file', [ | ||
| 'file_id' => $file->id, | ||
| 'path' => $file->stored_path, | ||
| ]); | ||
|
|
||
| continue; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $file->forceFill(['status' => FileStatus::Expired])->save(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Services\Files; | ||
|
|
||
| use App\Exceptions\Files\InvalidRetentionPolicyException; | ||
| use App\Models\User; | ||
| use App\Services\FeatureAccess\FeatureAccessService; | ||
| use Carbon\CarbonInterface; | ||
| use Illuminate\Support\Facades\Date; | ||
|
|
||
| final class FileRetentionPolicy | ||
| { | ||
| public function __construct( | ||
| private readonly FeatureAccessService $features, | ||
| ) {} | ||
|
|
||
| public function expiresAtFor(User $user, ?CarbonInterface $from = null): CarbonInterface | ||
| { | ||
| $raw = $this->features->limit($user, 'retention_days'); | ||
|
|
||
| $validated = filter_var($raw, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); | ||
|
|
||
| if ($validated === false) { | ||
| throw InvalidRetentionPolicyException::forInvalidDays($raw); | ||
| } | ||
|
|
||
| return ($from ?? Date::now())->copy()->addDays($validated); | ||
| } | ||
| } |
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,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| <title>File Expired</title> | ||
| </head> | ||
| <body> | ||
| <h1>File Expired</h1> | ||
| <p>This file has expired and was deleted. Upload the source file again to convert it.</p> | ||
| </body> | ||
| </html> |
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
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,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Jobs\CleanupExpiredFilesJob; | ||
| use Illuminate\Support\Facades\Bus; | ||
|
|
||
| it('dispatches the cleanup job when run without --sync', function () { | ||
| Bus::fake(); | ||
|
|
||
| $this->artisan('files:cleanup-expired') | ||
| ->expectsOutputToContain('Expired files cleanup queued') | ||
| ->assertExitCode(0); | ||
|
|
||
| Bus::assertDispatched(CleanupExpiredFilesJob::class); | ||
| }); | ||
|
|
||
| it('runs cleanup synchronously with --sync flag and does not dispatch', function () { | ||
| Bus::fake(); | ||
|
|
||
| $this->artisan('files:cleanup-expired --sync') | ||
| ->expectsOutputToContain('Expired files cleanup completed') | ||
| ->assertExitCode(0); | ||
|
|
||
| Bus::assertNotDispatched(CleanupExpiredFilesJob::class); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.