121 lines
3.9 KiB
PHP
121 lines
3.9 KiB
PHP
<?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);
|
||
}
|
||
}
|
||
}
|
||
}
|