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

130 lines
4.0 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\EvaluationRun;
use App\Models\EvaluationRuleSet;
use App\Models\Log;
use App\Models\LogOverride;
use App\Models\LogQso;
use App\Models\WorkingQso;
use App\Services\Evaluation\EvaluationCoordinator;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Bus;
use Throwable;
/**
* Job: DispatchBuildWorkingSetJobsJob
*
* Účel:
* - Rozdělí build working set do menších jobů podle log_id.
* - Spustí batch jobů BuildWorkingSetLogJob a po dokončení pokračuje pipeline.
*/
class DispatchBuildWorkingSetJobsJob 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 {
$ruleSet = EvaluationRuleSet::find($run->rule_set_id);
if (! $ruleSet) {
$coordinator->eventError($run, 'Working set nelze připravit: chybí ruleset.', [
'step' => 'build_working_set',
'round_id' => $run->round_id,
]);
return;
}
$coordinator->eventInfo($run, 'Working set: krok spuštěn.', [
'step' => 'build_working_set',
'round_id' => $run->round_id,
]);
$run->update([
'status' => 'RUNNING',
'current_step' => 'build_working_set',
'progress_total' => 0,
'progress_done' => 0,
]);
WorkingQso::where('evaluation_run_id', $run->id)->delete();
$logIds = Log::where('round_id', $run->round_id)->pluck('id');
$logOverrides = LogOverride::where('evaluation_run_id', $run->id)->get()->keyBy('log_id');
$ignoredLogIds = $logOverrides
->filter(fn ($override) => $override->forced_log_status === 'IGNORED')
->keys()
->all();
if ($ignoredLogIds) {
$logIds = $logIds->reject(fn ($id) => in_array($id, $ignoredLogIds, true))->values();
}
$total = $logIds->isEmpty()
? 0
: LogQso::whereIn('log_id', $logIds)->count();
$run->update([
'progress_total' => $total,
'progress_done' => 0,
]);
$coordinator->eventInfo($run, 'Příprava working setu.', [
'step' => 'build_working_set',
'round_id' => $run->round_id,
'step_progress_done' => 0,
'step_progress_total' => $total,
]);
$jobs = [];
foreach ($logIds as $logId) {
$jobs[] = new BuildWorkingSetLogJob($run->id, (int) $logId);
}
$next = function () use ($run) {
Bus::chain([
new PauseEvaluationRunJob(
$run->id,
'WAITING_REVIEW_INPUT',
'waiting_review_input',
'Čeká na kontrolu vstupů.'
),
])->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) {
$coordinator->eventError($run, 'Working set: krok selhal.', [
'step' => 'build_working_set',
'round_id' => $run->round_id,
'error' => $e->getMessage(),
]);
throw $e;
}
}
}