optimizations
This commit is contained in:
@@ -8,11 +8,14 @@ use App\Models\Artwork;
|
||||
use App\Models\ArtworkEmbedding;
|
||||
use App\Services\Vision\ArtworkEmbeddingClient;
|
||||
use App\Services\Vision\ArtworkVisionImageUrl;
|
||||
use App\Services\Vision\ArtworkVectorIndexService;
|
||||
use App\Services\Vision\VectorService;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
|
||||
final class GenerateArtworkEmbeddingJob implements ShouldQueue
|
||||
@@ -42,13 +45,19 @@ final class GenerateArtworkEmbeddingJob implements ShouldQueue
|
||||
return [2, 10, 30];
|
||||
}
|
||||
|
||||
public function handle(ArtworkEmbeddingClient $client, ArtworkVisionImageUrl $imageUrlBuilder): void
|
||||
public function handle(
|
||||
ArtworkEmbeddingClient $client,
|
||||
ArtworkVisionImageUrl $imageUrlBuilder,
|
||||
VectorService|ArtworkVectorIndexService $vectors,
|
||||
): void
|
||||
{
|
||||
if (! (bool) config('recommendations.embedding.enabled', true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$artwork = Artwork::query()->find($this->artworkId);
|
||||
$artwork = Artwork::query()
|
||||
->with(['categories' => fn ($categories) => $categories->with('contentType')->orderBy('sort_order')->orderBy('name')])
|
||||
->find($this->artworkId);
|
||||
if (! $artwork) {
|
||||
return;
|
||||
}
|
||||
@@ -111,11 +120,32 @@ final class GenerateArtworkEmbeddingJob implements ShouldQueue
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$this->upsertVectorIndex($vectors, $artwork);
|
||||
} finally {
|
||||
$this->releaseLock($lockKey);
|
||||
}
|
||||
}
|
||||
|
||||
private function upsertVectorIndex(
|
||||
VectorService|ArtworkVectorIndexService $vectors,
|
||||
Artwork $artwork
|
||||
): void
|
||||
{
|
||||
if (! $vectors->isConfigured()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$vectors->upsertArtwork($artwork);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('GenerateArtworkEmbeddingJob vector upsert failed', [
|
||||
'artwork_id' => (int) $artwork->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, float> $vector
|
||||
* @return array<int, float>
|
||||
|
||||
Reference in New Issue
Block a user