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.
This commit is contained in:
2026-08-25 07:58:47 +02:00
parent f52879edbb
commit 8a80aae21e
114 changed files with 9293 additions and 284 deletions
+71 -16
View File
@@ -11,6 +11,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\InteractsWithQueue;
use App\Jobs\Concerns\RestoresAfterArtworkIdCursor;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@@ -24,24 +25,42 @@ use Illuminate\Support\Facades\Log;
*/
final class RecComputeSimilarHybridJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable, InteractsWithQueue, Queueable, RestoresAfterArtworkIdCursor;
use SerializesModels {
__unserialize as private unserializeQueuedModels;
}
// This recompute is idempotent and already guards per-artwork execution.
// Keep retries to a minimum so transient failures do not turn into
// Horizon's max-attempt exception noise.
public int $tries = 1;
public int $timeout = 900;
public int $timeout = 180;
private ?int $afterArtworkId = null;
public function __construct(
private readonly ?int $artworkId = null,
private readonly int $batchSize = 200,
?int $afterArtworkId = null,
) {
$this->afterArtworkId = $afterArtworkId;
$queue = (string) config('recommendations.queue', 'default');
if ($queue !== '') {
$this->onQueue($queue);
}
}
public function __unserialize(array $values): void
{
$this->unserializeQueuedModels($values);
$this->restoreAfterArtworkIdCursor();
}
public function cursorAfterArtworkId(): ?int
{
return $this->afterArtworkId();
}
/**
* @return array<int, object>
*/
@@ -72,6 +91,8 @@ final class RecComputeSimilarHybridJob implements ShouldQueue
? (array) config('recommendations.similarity.weights_with_vector')
: (array) config('recommendations.similarity.weights_without_vector');
$startedAt = microtime(true);
if ($this->artworkId !== null) {
$artwork = Artwork::query()
->public()
@@ -80,6 +101,8 @@ final class RecComputeSimilarHybridJob implements ShouldQueue
->find($this->artworkId);
if (! $artwork instanceof Artwork) {
$this->logBatchComplete($startedAt, 0, false);
return;
}
@@ -93,26 +116,42 @@ final class RecComputeSimilarHybridJob implements ShouldQueue
$weights,
);
$this->logBatchComplete($startedAt, 1, false);
return;
}
Artwork::query()
$artworks = Artwork::query()
->public()
->published()
->select('id', 'user_id')
->chunkById($this->batchSize, function ($artworks) use (
$modelVersion, $vectorEnabled, $resultLimit, $maxPerAuthor, $minCatsTop12, $weights
) {
$this->processArtworkSafely(
$artworks,
$modelVersion,
$vectorEnabled,
$resultLimit,
$maxPerAuthor,
$minCatsTop12,
$weights,
);
});
->when($this->afterArtworkId() !== null, fn ($query) => $query->where('id', '>', $this->afterArtworkId()))
->orderBy('id')
->limit($this->batchSize)
->get();
if ($artworks->isEmpty()) {
$this->logBatchComplete($startedAt, 0, false);
return;
}
$this->processArtworkSafely(
$artworks,
$modelVersion,
$vectorEnabled,
$resultLimit,
$maxPerAuthor,
$minCatsTop12,
$weights,
);
$hasMore = $artworks->count() === $this->batchSize;
if ($hasMore) {
static::dispatch(null, $this->batchSize, (int) $artworks->last()->id);
}
$this->logBatchComplete($startedAt, $artworks->count(), $hasMore);
}
public function failed(\Throwable $exception): void
@@ -120,12 +159,28 @@ final class RecComputeSimilarHybridJob implements ShouldQueue
Log::error('[RecComputeSimilarHybrid] Job failed permanently.', [
'artwork_id' => $this->artworkId,
'batch_size' => $this->batchSize,
'after_artwork_id' => $this->afterArtworkId(),
'attempts' => $this->attempts(),
'exception_class' => $exception::class,
'exception_message' => $exception->getMessage(),
]);
}
/**
* @param positive-int|0 $processed
*/
private function logBatchComplete(float $startedAt, int $processed, bool $hasMore): void
{
Log::info('[RecComputeSimilarHybrid] Batch complete.', [
'artwork_id' => $this->artworkId,
'after_artwork_id' => $this->afterArtworkId(),
'processed' => $processed,
'has_more' => $hasMore,
'duration_ms' => (int) round((microtime(true) - $startedAt) * 1000),
'memory_mb' => round(memory_get_peak_usage(true) / 1048576, 1),
]);
}
/**
* @param iterable<Artwork> $artworks
*/