Files
vkv/app/Jobs/ParseLogJob.php
Zdeněk Burda 41e3ce6f25 Initial commit
2026-01-09 21:26:40 +01:00

121 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Jobs;
use App\Http\Controllers\LogController;
use App\Models\EvaluationLock;
use App\Models\EvaluationRun;
use App\Models\Log;
use App\Services\Evaluation\EvaluationCoordinator;
use App\Jobs\UpsertClaimedLogResultJob;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Throwable;
/**
* Job: ParseLogJob
*
* Účel:
* - Naparsuje jeden EDI log a uloží Log/LogQso.
*/
class ParseLogJob implements ShouldQueue
{
use Batchable;
use Queueable;
public int $tries = 3;
public array $backoff = [30, 120, 300];
public function __construct(
protected int $evaluationRunId,
protected int $logId
) {
}
public function handle(): void
{
$run = EvaluationRun::find($this->evaluationRunId);
if (! $run || $run->isCanceled()) {
return;
}
$coordinator = new EvaluationCoordinator();
$lock = null;
$lockKey = "evaluation:parse:round:{$run->round_id}:log:{$this->logId}";
try {
$lock = EvaluationLock::acquire(
key: $lockKey,
run: $run,
ttl: 1800
);
if (! $lock) {
throw new \RuntimeException("ParseLogJob nelze spustit lock je držen (log_id={$this->logId}).");
}
$log = Log::with('file')->find($this->logId);
if (! $log || (int) $log->round_id !== (int) $run->round_id) {
return;
}
if (! $log->file || ! $log->file->path) {
$coordinator->eventWarn($run, "Log #{$log->id} nemá soubor, parser přeskočen.", [
'step' => 'parse_logs',
'round_id' => $run->round_id,
'log_id' => $log->id,
]);
EvaluationRun::where('id', $run->id)->increment('progress_done');
return;
}
try {
LogController::parseUploadedFile($log, $log->file->path);
} catch (Throwable $e) {
$coordinator->eventError($run, "Chyba parsování logu #{$log->id}: {$e->getMessage()}", [
'step' => 'parse_logs',
'round_id' => $run->round_id,
'log_id' => $log->id,
]);
}
if ($run->rules_version === 'CLAIMED') {
try {
UpsertClaimedLogResultJob::dispatchSync($log->id);
} catch (Throwable $e) {
$coordinator->eventError($run, "Chyba deklarace výsledků logu #{$log->id}: {$e->getMessage()}", [
'step' => 'claimed_upsert',
'round_id' => $run->round_id,
'log_id' => $log->id,
]);
}
}
EvaluationRun::where('id', $run->id)->increment('progress_done');
$progressTotal = (int) ($run->progress_total ?? 0);
$done = $progressTotal > 0
? (int) EvaluationRun::where('id', $run->id)->value('progress_done')
: 0;
if ($progressTotal > 0 && $done % 10 === 0) {
$coordinator->eventInfo($run, "Parsování logů: {$done}/{$progressTotal}", [
'step' => 'parse_logs',
'round_id' => $run->round_id,
'step_progress_done' => $done,
'step_progress_total' => $progressTotal,
]);
}
} catch (Throwable $e) {
$coordinator->eventError($run, "ParseLogJob selhal: {$e->getMessage()}", [
'step' => 'parse_logs',
'round_id' => $run->round_id,
'log_id' => $this->logId,
]);
throw $e;
} finally {
if ($lock) {
EvaluationLock::release($lockKey, $run);
}
}
}
}