Files
pricebuddy/app/Jobs/RetryUrlPriceJob.php
Jeremy dc88f7b037 test: fix settings save isolation and updatePrices return assertions
- Forget the AppSettings singleton in TestCase::setUp so each test loads
  settings fresh from the migrated DB; a singleton loaded before the new
  settings rows exist marks them default-loaded, making AppSettings::save()
  throw MissingSettings (exposed by the new required scrape_retry settings).
- Align the retry-settings page test with the existing convention (assert no
  form errors on the fields; add a min-value validation test) instead of a
  full-form persistence assertion the page's unrelated required fields block.
- Update UrlTest for the new Collection return type of Product::updatePrices().

harden: RetryUrlPriceJob tries/missing-models/notification throttle

Address final code review findings:
- Set $tries=1 so queue-level retries don't duplicate the explicit retry chain.
- Set $deleteWhenMissingModels=true so a URL/product deleted before the delayed
  job runs is discarded silently rather than failing the job.
- Throttle the exhaustion notification to one per product per retry window, so a
  multi-URL product doesn't send duplicate product-level scrape-fail notices.
- Assert the retry delay is applied in the job tests.
2026-06-10 21:58:32 +10:00

85 lines
2.6 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();
$product->updatePriceCache();
// A non-null result means the scrape recovered (a price was recorded,
// or the URL is legitimately out of stock). Nothing more to do.
if (! is_null($result)) {
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));
}
}
}