43 lines
1.7 KiB
PHP
43 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Jobs\ScanDocumentForMalware;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ScanPendingDocuments extends Command
|
|
{
|
|
protected $signature = 'documents:scan-pending {--apply : Dispatch scans to the queue} {--limit=100 : Maximum documents to process in this batch}';
|
|
|
|
protected $description = 'List or dispatch a controlled batch of document malware scans';
|
|
|
|
public function handle(): int
|
|
{
|
|
$limit = max(1, min(1000, (int) $this->option('limit')));
|
|
$ids = DB::table('documents')->whereNull('deleted_at')->where('status', '!=', 'deleted')
|
|
->where(function ($query): void {
|
|
$query->whereIn('malware_scan_status', ['pending', 'inconclusive', 'failed'])
|
|
->orWhere(fn ($stale) => $stale->where('malware_scan_status', 'scanning')->where('updated_at', '<', now()->subMinutes(15)));
|
|
})->orderBy('created_at')->limit($limit)->pluck('id');
|
|
|
|
$this->info(count($ids).' document(s) eligible for this batch.');
|
|
if (! $this->option('apply')) {
|
|
$this->comment('Dry run only. Add --apply to dispatch scans.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
foreach ($ids as $id) {
|
|
if (DB::table('documents')->where('id', $id)->where('malware_scan_status', 'scanning')->exists()) {
|
|
DB::table('documents')->where('id', $id)->update(['malware_scan_status' => 'pending', 'status' => 'quarantined', 'updated_at' => now()]);
|
|
}
|
|
ScanDocumentForMalware::dispatch((string) $id);
|
|
}
|
|
|
|
$this->info('Queued '.$ids->count().' scan(s). No editorial publication state was changed.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|