64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
||
|
||
namespace Modules\Core\Shipping\Jobs;
|
||
|
||
use Illuminate\Bus\Batchable;
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Foundation\Bus\Dispatchable;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Modules\Core\Shipping\Models\Shipment;
|
||
use Modules\Core\Shipping\Services\ShipmentTrackingRecorder;
|
||
|
||
/**
|
||
* Refreshes one shipment's tracking from its carrier — one job per
|
||
* shipment in PollShipmentTrackingJob's batch, so a slow or failing
|
||
* carrier call only costs its own job, not the rest of the run.
|
||
*
|
||
* Unique per shipment: while one is queued or running, another for the
|
||
* same shipment isn't queued, so two refreshes can't both record the same
|
||
* checkpoint (the recorder's dedup reads, then writes).
|
||
*/
|
||
class RefreshShipmentTrackingJob implements ShouldBeUnique, ShouldQueue
|
||
{
|
||
use Batchable;
|
||
use Dispatchable;
|
||
use InteractsWithQueue;
|
||
use Queueable;
|
||
|
||
/** A few carrier calls at a 10–15s HTTP timeout each; under the queue's 90s retry_after. */
|
||
public int $timeout = 45;
|
||
|
||
public bool $failOnTimeout = true;
|
||
|
||
/** The next poll, five minutes later, is the retry. */
|
||
public int $tries = 1;
|
||
|
||
/** Releases the lock if a worker dies mid-job and never releases it. */
|
||
public int $uniqueFor = 300;
|
||
|
||
public function __construct(public readonly int $shipmentId) {}
|
||
|
||
public function uniqueId(): string
|
||
{
|
||
return (string) $this->shipmentId;
|
||
}
|
||
|
||
public function handle(ShipmentTrackingRecorder $recorder): void
|
||
{
|
||
if ($this->batch()?->cancelled()) {
|
||
return;
|
||
}
|
||
|
||
$shipment = Shipment::find($this->shipmentId);
|
||
|
||
// Cancelled (or deleted) since the batch was built.
|
||
if (! $shipment || $shipment->isCancelled()) {
|
||
return;
|
||
}
|
||
|
||
$recorder->refresh($shipment);
|
||
}
|
||
}
|