Files
SkinbaseNova/app/Jobs/RankBuildScopeListsJob.php
T
klevze 8a80aae21e Ship production optimization M1-M12.5A: queues, metrics, HTTP observability, and vector search reliability.
Keep similar-ai from tripping the global circuit on a lone URL 502, clamp Qdrant search to 100, and add Server-Timing plus slow-request logging. Studio shared props, Academy S3 exists caching, heat chunking, and Redis/scheduler hygiene stay in this rollout.
2026-08-25 07:58:47 +02:00

164 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Services\RankingService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class RankBuildScopeListsJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $timeout = 300;
public int $tries = 2;
/**
* Unique until the job finishes or uniqueFor elapses.
* Must outlast the queue wait (hours) plus timeout (300s), not just 360s.
*/
public int $uniqueFor;
private const LIST_TYPES = ['trending', 'new_hot', 'best'];
public function __construct(
public readonly string $scopeType,
public readonly int $scopeId,
) {
$this->uniqueFor = max(3600, (int) config('ranking.scope_job_unique_for', 21600));
}
public function uniqueId(): string
{
return $this->scopeType . ':' . $this->scopeId;
}
public function handle(RankingService $ranking): void
{
$modelVersion = config('ranking.model_version', 'rank_v1');
$maxPerAuthor = (int) config('ranking.diversity.max_per_author', 3);
$listSize = (int) config('ranking.diversity.list_size', 50);
$candidatePool = (int) config('ranking.diversity.candidate_pool', 200);
foreach (self::LIST_TYPES as $listType) {
$this->buildAndStore(
$ranking,
$listType,
$this->scopeType,
$this->scopeId,
$modelVersion,
$maxPerAuthor,
$listSize,
$candidatePool
);
}
Log::info('RankBuildScopeListsJob: finished', [
'scope_type' => $this->scopeType,
'scope_id' => $this->scopeId,
'model_version' => $modelVersion,
]);
}
private function buildAndStore(
RankingService $ranking,
string $listType,
string $scopeType,
int $scopeId,
string $modelVersion,
int $maxPerAuthor,
int $listSize,
int $candidatePool
): void {
$scoreCol = $this->scoreColumn($listType);
$candidates = $this->fetchCandidates(
$scopeType, $scopeId, $scoreCol, $candidatePool, $modelVersion
);
$diverse = $ranking->applyDiversity(
$candidates->all(), $maxPerAuthor, $listSize
);
$ids = array_map(
fn ($item) => (int) ($item->artwork_id ?? $item['artwork_id']),
$diverse
);
DB::table('rank_lists')->upsert(
[[
'scope_type' => $scopeType,
'scope_id' => $scopeId,
'list_type' => $listType,
'model_version' => $modelVersion,
'artwork_ids' => json_encode($ids),
'computed_at' => now()->toDateTimeString(),
]],
['scope_type', 'scope_id', 'list_type', 'model_version'],
['artwork_ids', 'computed_at']
);
$ranking->bustCache($scopeType, $scopeId === 0 ? null : $scopeId, $listType);
}
/**
* @return \Illuminate\Support\Collection<int, object>
*/
private function fetchCandidates(
string $scopeType,
int $scopeId,
string $scoreCol,
int $limit,
string $modelVersion
): \Illuminate\Support\Collection {
$query = DB::table('rank_artwork_scores as ras')
->select(['ras.artwork_id', 'a.user_id', "ras.{$scoreCol}"])
->join('artworks as a', function ($join): void {
$join->on('a.id', '=', 'ras.artwork_id')
->where('a.is_public', 1)
->where('a.is_approved', 1)
->whereNull('a.deleted_at');
})
->where('ras.model_version', $modelVersion)
->orderByDesc("ras.{$scoreCol}")
->limit($limit);
if ($scopeType === 'category' && $scopeId > 0) {
$query->join(
'artwork_category as ac',
fn ($j) => $j->on('ac.artwork_id', '=', 'a.id')
->where('ac.category_id', $scopeId)
);
}
if ($scopeType === 'content_type' && $scopeId > 0) {
$query->join(
'artwork_category as ac',
'ac.artwork_id', '=', 'a.id'
)->join(
'categories as cat',
fn ($j) => $j->on('cat.id', '=', 'ac.category_id')
->where('cat.content_type_id', $scopeId)
->whereNull('cat.deleted_at')
);
}
return $query->get();
}
private function scoreColumn(string $listType): string
{
return match ($listType) {
'new_hot' => 'score_new_hot',
'best' => 'score_best',
default => 'score_trending',
};
}
}