Files
pricebuddy/app/Jobs/RetryUrlPriceJob.php
Jeremy a353b35a72 fix: address insights review — per-product correctness, quiet cache write, lazy retry rebuild
- Remove once() from ProductInsights::for(): the static call-site memoized
  globally, so resolving insights for a second product in one request returned
  the first product's data. The persistent insights_cache already prevents
  recompute, so once() only added the bug.
- ProductTransformer delegates to ProductInsights::for() instead of duplicating
  the cache-vs-compute branch.
- RetryUrlPriceJob rebuilds price/insights caches only on a non-null scrape
  result; a failed retry changes no data, so skip the heavy rebuild.
- NotifyPriceChangeListener: add $tries/$timeout for the queued rebuild.
- Product::updateInsightsCache() uses forceFill()->saveQuietly() so a pure
  cache write doesn't re-trigger the saving hook (next_check_at) or updated hook.
- Add regression test: for() returns distinct insights per product in one request.
2026-06-21 01:23:09 +10:00

89 lines
2.8 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Product;
use App\Models\Url;
use App\Notifications\ScrapeFailNotification;
use App\Services\PriceFetcherService;
use App\Settings\AppSettings;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Cache;
class RetryUrlPriceJob implements ShouldQueue
{
use Dispatchable;
use Queueable;
public $timeout = PriceFetcherService::JOB_TIMEOUT;
/**
* The retry chain is driven explicitly (this job re-dispatches itself), so a
* single attempt is enough — queue-level retries would duplicate it.
*/
public $tries = 1;
/**
* If the URL (or its product) is deleted before this delayed job runs, just
* discard the job instead of failing it.
*/
public bool $deleteWhenMissingModels = true;
/**
* @param Url $url the URL to re-scrape
* @param int $attempt the attempt number this job represents (the original scheduled scrape is attempt 1)
*/
public function __construct(public Url $url, public int $attempt) {}
public function handle(): void
{
$product = $this->url->product;
if (! $product || $product->paused) {
return;
}
$result = $this->url->updatePrice();
// A non-null result means the scrape recovered (a price was recorded,
// or the URL is legitimately out of stock). Refresh the caches to reflect
// the new data, then stop. A null result changed nothing, so skip the
// (heavy) cache rebuild and fall through to the retry/notify logic.
if (! is_null($result)) {
$product->updatePriceCache();
$product->updateInsightsCache();
return;
}
$settings = AppSettings::new();
if ($this->attempt < $settings->scrape_retry_max_attempts) {
self::dispatch($this->url, $this->attempt + 1)
->delay(now()->addMinutes($settings->scrape_retry_delay_minutes));
return;
}
$this->notifyExhausted($product, $settings);
}
/**
* Notify the product owner that the scrape failed after all retries. The
* notification is product-level, so it is throttled to one per product per
* retry window to avoid flooding when several of a product's URLs exhaust
* their retries at around the same time.
*/
protected function notifyExhausted(Product $product, AppSettings $settings): void
{
$cacheKey = "scrape-fail-notified:{$product->getKey()}";
$window = now()->addMinutes(max(1, $settings->scrape_retry_delay_minutes));
if (Cache::add($cacheKey, true, $window)) {
$product->user?->notify(new ScrapeFailNotification($product));
}
}
}