98 lines
2.8 KiB
PHP
98 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\EvaluationRun;
|
|
use App\Models\Log;
|
|
use App\Jobs\DispatchBuildWorkingSetJobsJob;
|
|
use App\Jobs\RecalculateClaimedRanksJob;
|
|
use App\Services\Evaluation\EvaluationCoordinator;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Facades\Bus;
|
|
use Illuminate\Support\Carbon;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Job: DispatchParseLogsJobsJob
|
|
*
|
|
* Účel:
|
|
* - Rozdělí parsování logů do menších jobů podle log_id.
|
|
* - Spustí batch jobů ParseLogJob a po dokončení pokračuje pipeline.
|
|
*/
|
|
class DispatchParseLogsJobsJob implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
public int $tries = 2;
|
|
public array $backoff = [30];
|
|
|
|
public function __construct(
|
|
protected int $evaluationRunId
|
|
) {
|
|
}
|
|
|
|
public function handle(): void
|
|
{
|
|
$run = EvaluationRun::find($this->evaluationRunId);
|
|
if (! $run || $run->isCanceled()) {
|
|
return;
|
|
}
|
|
|
|
$coordinator = new EvaluationCoordinator();
|
|
|
|
try {
|
|
$coordinator->eventInfo($run, 'Parsování logů: krok spuštěn.', [
|
|
'step' => 'parse_logs',
|
|
'round_id' => $run->round_id,
|
|
]);
|
|
|
|
$logIds = Log::where('round_id', $run->round_id)->pluck('id');
|
|
$total = $logIds->count();
|
|
|
|
$run->update([
|
|
'status' => 'RUNNING',
|
|
'current_step' => 'parse_logs',
|
|
'progress_total' => $total,
|
|
'progress_done' => 0,
|
|
'started_at' => $run->started_at ?? Carbon::now(),
|
|
]);
|
|
|
|
$jobs = [];
|
|
foreach ($logIds as $logId) {
|
|
$jobs[] = new ParseLogJob($run->id, (int) $logId);
|
|
}
|
|
|
|
$next = function () use ($run) {
|
|
if ($run->rules_version === 'CLAIMED') {
|
|
RecalculateClaimedRanksJob::dispatch($run->id)
|
|
->delay(now()->addSeconds(10))
|
|
->onQueue('evaluation');
|
|
}
|
|
Bus::chain([
|
|
new DispatchBuildWorkingSetJobsJob($run->id),
|
|
])->onQueue('evaluation')->dispatch();
|
|
};
|
|
|
|
if (! $jobs) {
|
|
$next();
|
|
return;
|
|
}
|
|
|
|
$batch = Bus::batch($jobs)
|
|
->then($next)
|
|
->onQueue('evaluation')
|
|
->dispatch();
|
|
$run->update(['batch_id' => $batch->id]);
|
|
} catch (Throwable $e) {
|
|
$message = "DispatchParseLogsJobsJob selhal (run {$run->id}): {$e->getMessage()}";
|
|
\Log::error($message);
|
|
$coordinator->eventError($run, $message, [
|
|
'step' => 'parse_logs',
|
|
'round_id' => $run->round_id,
|
|
]);
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|