Initial commit
This commit is contained in:
95
app/Jobs/DispatchAggregateResultsJobsJob.php
Normal file
95
app/Jobs/DispatchAggregateResultsJobsJob.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\EvaluationRun;
|
||||
use App\Models\LogResult;
|
||||
use App\Services\Evaluation\EvaluationCoordinator;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Job: DispatchAggregateResultsJobsJob
|
||||
*
|
||||
* Účel:
|
||||
* - Rozdělí agregaci výsledků na menší joby podle log_id.
|
||||
* - Spustí batch jobů AggregateLogResultsJob a po dokončení naváže
|
||||
* ApplyLogOverridesJob + RecalculateOfficialRanksJob + PauseEvaluationRunJob.
|
||||
*/
|
||||
class DispatchAggregateResultsJobsJob implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public int $tries = 2;
|
||||
public array $backoff = [60];
|
||||
|
||||
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, 'Aggregate: krok spuštěn.', [
|
||||
'step' => 'aggregate',
|
||||
'round_id' => $run->round_id,
|
||||
]);
|
||||
|
||||
$logIds = LogResult::where('evaluation_run_id', $run->id)
|
||||
->pluck('log_id')
|
||||
->all();
|
||||
|
||||
$run->update([
|
||||
'status' => 'RUNNING',
|
||||
'current_step' => 'aggregate',
|
||||
'progress_total' => count($logIds),
|
||||
'progress_done' => 0,
|
||||
]);
|
||||
|
||||
$jobs = [];
|
||||
foreach ($logIds as $logId) {
|
||||
$jobs[] = new AggregateLogResultsJob($run->id, (int) $logId);
|
||||
}
|
||||
|
||||
$next = function () use ($run) {
|
||||
Bus::chain([
|
||||
new ApplyLogOverridesJob($run->id),
|
||||
new RecalculateOfficialRanksJob($run->id),
|
||||
new PauseEvaluationRunJob(
|
||||
$run->id,
|
||||
'WAITING_REVIEW_SCORE',
|
||||
'waiting_review_score',
|
||||
'Čeká na kontrolu skóre.'
|
||||
),
|
||||
])->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, 'Aggregate: krok selhal.', [
|
||||
'step' => 'aggregate',
|
||||
'round_id' => $run->round_id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user