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

106 lines
3.1 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\EvaluationRun;
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: DispatchUnpairedJobsJob
*
* Účel:
* - Rozdělí klasifikaci nenapárovaných QSO do bucketů podle band_id + rcall_norm.
* - Spustí batch jobů UnpairedClassificationBucketJob a po dokončení naváže
* DuplicateResolutionJob.
*/
class DispatchUnpairedJobsJob 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();
$coordinator->eventInfo($run, 'Dispatch unpaired: krok spuštěn.', [
'step' => 'dispatch_unpaired',
'round_id' => $run->round_id,
]);
try {
$buckets = WorkingQso::where('evaluation_run_id', $run->id)
->whereNotNull('rcall_norm')
->distinct()
->get(['band_id', 'rcall_norm']);
$jobs = [];
foreach ($buckets as $bucket) {
$jobs[] = new UnpairedClassificationBucketJob(
$run->id,
$bucket->band_id !== null ? (int) $bucket->band_id : null,
$bucket->rcall_norm
);
}
$run->refresh();
$progressDone = (int) $run->progress_done;
$progressTotal = $progressDone + count($jobs) + 1;
$run->update([
'progress_total' => $progressTotal,
'progress_done' => $progressDone,
]);
$next = function () use ($run) {
Bus::chain([
new DuplicateResolutionJob($run->id),
new PauseEvaluationRunJob(
$run->id,
'WAITING_REVIEW_MATCH',
'waiting_review_match',
'Čeká na kontrolu matchingu.'
),
])->onQueue('evaluation')->dispatch();
};
if (! $jobs) {
$next();
return;
}
$batch = Bus::batch($jobs)
->then($next)
->onQueue('evaluation')
->dispatch();
$run->update(['batch_id' => $batch->id]);
$coordinator->eventInfo($run, 'Dispatch unpaired: krok dokončen.', [
'step' => 'dispatch_unpaired',
'round_id' => $run->round_id,
]);
} catch (Throwable $e) {
$coordinator->eventError($run, 'Dispatch unpaired: krok selhal.', [
'step' => 'dispatch_unpaired',
'round_id' => $run->round_id,
'error' => $e->getMessage(),
]);
throw $e;
}
}
}