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:
@@ -10,8 +10,10 @@ use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use App\Jobs\Concerns\RestoresAfterArtworkIdCursor;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Compute behavior-based (co-like) similarity from precomputed item pairs.
|
||||
@@ -21,38 +23,129 @@ use Illuminate\Support\Facades\DB;
|
||||
*/
|
||||
final class RecComputeSimilarByBehaviorJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
use Dispatchable, InteractsWithQueue, Queueable, RestoresAfterArtworkIdCursor;
|
||||
use SerializesModels {
|
||||
__unserialize as private unserializeQueuedModels;
|
||||
}
|
||||
|
||||
public int $tries = 2;
|
||||
public int $timeout = 600;
|
||||
public int $timeout = 120;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$startedAt = microtime(true);
|
||||
$modelVersion = (string) config('recommendations.similarity.model_version', 'sim_v1');
|
||||
$resultLimit = (int) config('recommendations.similarity.result_limit', 30);
|
||||
$maxPerAuthor = (int) config('recommendations.similarity.max_per_author', 2);
|
||||
|
||||
$query = Artwork::query()->public()->published()->select('id', 'user_id');
|
||||
|
||||
if ($this->artworkId !== null) {
|
||||
$query->where('id', $this->artworkId);
|
||||
$artwork = Artwork::query()->public()->published()->select('id', 'user_id')->find($this->artworkId);
|
||||
|
||||
if ($artwork instanceof Artwork) {
|
||||
$this->processArtworkSafely($artwork, $modelVersion, $resultLimit, $maxPerAuthor);
|
||||
$this->logBatchComplete($startedAt, 1, false);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logBatchComplete($startedAt, 0, false);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->chunkById($this->batchSize, function ($artworks) use ($modelVersion, $resultLimit, $maxPerAuthor) {
|
||||
foreach ($artworks as $artwork) {
|
||||
$this->processArtwork($artwork, $modelVersion, $resultLimit, $maxPerAuthor);
|
||||
}
|
||||
});
|
||||
$artworks = Artwork::query()
|
||||
->public()
|
||||
->published()
|
||||
->select('id', 'user_id')
|
||||
->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;
|
||||
}
|
||||
|
||||
foreach ($artworks as $artwork) {
|
||||
$this->processArtworkSafely($artwork, $modelVersion, $resultLimit, $maxPerAuthor);
|
||||
}
|
||||
|
||||
$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
|
||||
{
|
||||
Log::error('[RecComputeSimilarByBehavior] 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(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function processArtworkSafely(
|
||||
Artwork $artwork,
|
||||
string $modelVersion,
|
||||
int $resultLimit,
|
||||
int $maxPerAuthor,
|
||||
): void {
|
||||
try {
|
||||
$this->processArtwork($artwork, $modelVersion, $resultLimit, $maxPerAuthor);
|
||||
} catch (\Throwable $exception) {
|
||||
Log::warning("[RecComputeSimilarByBehavior] Failed for artwork {$artwork->id}: {$exception->getMessage()}", [
|
||||
'artwork_id' => $artwork->id,
|
||||
'exception_class' => $exception::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param positive-int|0 $processed
|
||||
*/
|
||||
private function logBatchComplete(float $startedAt, int $processed, bool $hasMore): void
|
||||
{
|
||||
Log::info('[RecComputeSimilarByBehavior] 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),
|
||||
]);
|
||||
}
|
||||
|
||||
private function processArtwork(
|
||||
|
||||
Reference in New Issue
Block a user