This commit is contained in:
ct
2025-08-10 21:10:33 +08:00
parent 480bd9055d
commit 583a804073
43 changed files with 7623 additions and 270 deletions

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Console\Commands;
use App\Jobs\CleanupOldResults;
use App\Models\CrawlShotJob;
use Carbon\Carbon;
use Illuminate\Console\Command;
class PruneStorage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crawlshot:prune-storage
{--hours=24 : How many hours old files should be to be pruned}
{--dry-run : Show what would be deleted without actually deleting}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Prune expired crawlshot HTML and image results older than specified hours (default: 24)';
/**
* Execute the console command.
*/
public function handle()
{
$hours = (int) $this->option('hours');
$dryRun = $this->option('dry-run');
$this->info("Pruning crawlshot storage files older than {$hours} hours...");
if ($dryRun) {
$this->warn('DRY RUN MODE - No files will actually be deleted');
}
// Find jobs older than specified hours
$cutoffTime = Carbon::now()->subHours($hours);
$oldJobs = CrawlShotJob::where('created_at', '<', $cutoffTime)
->whereNotNull('file_path')
->get();
if ($oldJobs->isEmpty()) {
$this->info('No files found to prune.');
return Command::SUCCESS;
}
$this->info("Found {$oldJobs->count()} files to prune:");
$deletedFiles = 0;
$deletedRecords = 0;
$errors = 0;
foreach ($oldJobs as $job) {
$this->line("- {$job->type} job {$job->uuid} ({$job->file_path})");
if (!$dryRun) {
// Delete the file if it exists
if ($job->file_path && file_exists($job->file_path)) {
if (unlink($job->file_path)) {
$deletedFiles++;
} else {
$this->error(" Failed to delete file: {$job->file_path}");
$errors++;
}
}
// Delete the database record
try {
$job->delete();
$deletedRecords++;
} catch (\Exception $e) {
$this->error(" Failed to delete database record: {$e->getMessage()}");
$errors++;
}
}
}
if (!$dryRun) {
$this->info("Cleanup completed:");
$this->line(" - Files deleted: {$deletedFiles}");
$this->line(" - Database records deleted: {$deletedRecords}");
if ($errors > 0) {
$this->error(" - Errors encountered: {$errors}");
return Command::FAILURE;
}
} else {
$this->info("Would have deleted {$oldJobs->count()} files and records");
}
return Command::SUCCESS;
}
}