optimizations

This commit is contained in:
2026-03-28 19:15:39 +01:00
parent 0b25d9570a
commit cab4fbd83e
509 changed files with 1016804 additions and 1605 deletions

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Models\Artwork;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
final class BackfillArtworkVectorIndexJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public int $tries = 1;
public int $timeout = 120;
public function __construct(
private readonly int $afterId = 0,
private readonly int $batchSize = 200,
private readonly bool $publicOnly = false,
private readonly int $staleHours = 0,
) {
$queue = (string) config('recommendations.queue', config('vision.queue', 'default'));
if ($queue !== '') {
$this->onQueue($queue);
}
}
public function handle(): void
{
$batch = max(1, min($this->batchSize, 1000));
$staleHours = max(0, $this->staleHours);
$staleBefore = $staleHours > 0 ? now()->subHours($staleHours) : null;
$query = Artwork::query()
->where('id', '>', $this->afterId)
->whereNotNull('hash')
->whereHas('embeddings')
->when($this->publicOnly, static fn ($query) => $query->public()->published())
->orderBy('id')
->limit($batch);
if ($staleBefore !== null) {
$query->where(static function ($innerQuery) use ($staleBefore): void {
$innerQuery->whereNull('last_vector_indexed_at')
->orWhere('last_vector_indexed_at', '<=', $staleBefore);
});
}
$artworks = $query->get(['id']);
if ($artworks->isEmpty()) {
return;
}
foreach ($artworks as $artwork) {
SyncArtworkVectorIndexJob::dispatch((int) $artwork->id);
}
if ($artworks->count() === $batch) {
$lastId = (int) $artworks->last()->id;
self::dispatch($lastId, $batch, $this->publicOnly, $staleHours);
}
}
}