Files

81 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2026-07-20 10:22:05 +03:00
<?php
namespace App\Jobs;
use App\Models\Finding;
use App\Osint\RunnerRegistry;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\RateLimited;
2026-07-20 10:22:05 +03:00
class RunFinding implements ShouldQueue
{
use Queueable;
/**
* Business-logic failures (a runner throwing) are caught in handle()
* below and never bubble up to the queue, so this only needs to cover
* genuine retries: rate-limiter releases (see middleware()) and worker
* crashes. A bulk run against a busy investigation can easily queue
* dozens of same-source lookups behind one rate limit, so this is set
* high enough to ride that out rather than give up mid-queue.
*/
public int $tries = 30;
2026-07-20 10:22:05 +03:00
public function __construct(
public readonly int $findingId,
) {
}
/**
* @return array<int, object>
*/
public function middleware(): array
{
$sourceKey = Finding::find($this->findingId)?->source?->key;
if ($sourceKey === null) {
return [];
}
return [new RateLimited('osint-runner:'.$sourceKey)];
}
2026-07-20 10:22:05 +03:00
public function handle(RunnerRegistry $registry): void
{
$finding = Finding::with(['target', 'source'])->findOrFail($this->findingId);
$finding->update(['status' => 'running']);
try {
$result = $registry->resolve($finding->source->key)->run($finding->target);
$finding->update([
'status' => 'completed',
'raw_payload' => $result->raw,
'summary' => $result->summary,
'ran_at' => now(),
]);
} catch (\Throwable $e) {
$finding->update([
'status' => 'failed',
'error' => $e->getMessage(),
'ran_at' => now(),
]);
}
}
/**
* Only reached if the job itself fails outside handle()'s own
* try/catch — e.g. retries exhausted while rate-limited.
*/
public function failed(\Throwable $exception): void
{
Finding::where('id', $this->findingId)->update([
'status' => 'failed',
'error' => 'Gave up after retries: '.$exception->getMessage(),
'ran_at' => now(),
]);
}
2026-07-20 10:22:05 +03:00
}