27-05-2026 Update DHL Modul v2.0
This commit is contained in:
parent
53bdba33cd
commit
036595be94
41 changed files with 3346 additions and 310 deletions
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\DhlShipment;
|
||||
use App\Services\DhlApiService;
|
||||
use Acme\Dhl\Models\DhlShipment;
|
||||
use App\Services\DhlTrackingService;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Illuminate\Bus\Queueable as BusQueueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
|
@ -14,7 +15,7 @@ use Illuminate\Support\Facades\Log;
|
|||
|
||||
/**
|
||||
* Job to track DHL shipments asynchronously
|
||||
*
|
||||
*
|
||||
* This job handles the tracking of DHL shipments in the background,
|
||||
* updating tracking status and details automatically.
|
||||
*/
|
||||
|
|
@ -37,7 +38,7 @@ class TrackShipmentJob implements ShouldQueue
|
|||
*
|
||||
* @var int
|
||||
*/
|
||||
public $tries = 2; // Lower tries for tracking as it's less critical
|
||||
public $tries = 2;
|
||||
|
||||
/**
|
||||
* The maximum number of seconds the job can run before timing out.
|
||||
|
|
@ -49,14 +50,13 @@ class TrackShipmentJob implements ShouldQueue
|
|||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param DhlShipment $dhlShipment
|
||||
* @param array $options
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function __construct(DhlShipment $dhlShipment, array $options = [])
|
||||
{
|
||||
$this->dhlShipment = $dhlShipment;
|
||||
$this->options = $options;
|
||||
|
||||
|
||||
// Set queue name - tracking is usually lower priority
|
||||
$this->onQueue('dhl-tracking');
|
||||
}
|
||||
|
|
@ -64,35 +64,34 @@ class TrackShipmentJob implements ShouldQueue
|
|||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
public function handle(DhlTrackingService $trackingService): void
|
||||
{
|
||||
try {
|
||||
Log::info('[DHL Queue] Starting shipment tracking job', [
|
||||
'shipment_id' => $this->dhlShipment->id,
|
||||
'tracking_number' => $this->dhlShipment->tracking_number,
|
||||
'dhl_shipment_no' => $this->dhlShipment->dhl_shipment_no,
|
||||
'attempt' => $this->attempts(),
|
||||
]);
|
||||
|
||||
$dhlService = new DhlApiService();
|
||||
|
||||
// Get tracking details
|
||||
$trackingDetails = $dhlService->getTrackingDetails($this->dhlShipment);
|
||||
$result = $trackingService->updateTrackingNow($this->dhlShipment, $this->options);
|
||||
|
||||
Log::info('[DHL Queue] Shipment tracking updated successfully', [
|
||||
'shipment_id' => $this->dhlShipment->id,
|
||||
'tracking_status' => $trackingDetails['status'] ?? 'unknown',
|
||||
'events_count' => isset($trackingDetails['events']) ? count($trackingDetails['events']) : 0,
|
||||
'success' => $result['success'] ?? false,
|
||||
'tracking_status' => $result['tracking_status'] ?? 'unknown',
|
||||
'tracking_completed' => $result['tracking_completed'] ?? false,
|
||||
]);
|
||||
|
||||
// Schedule next tracking update if shipment is still in transit
|
||||
if (isset($this->options['auto_retrack']) && $this->options['auto_retrack']) {
|
||||
$status = $trackingDetails['status'] ?? '';
|
||||
if (($this->options['auto_retrack'] ?? false) && ! ($result['tracking_completed'] ?? false)) {
|
||||
$this->dhlShipment->refresh();
|
||||
$status = $this->dhlShipment->status ?? '';
|
||||
if ($this->shouldContinueTracking($status)) {
|
||||
// Schedule next tracking in 2-6 hours based on current status
|
||||
$nextTrackingDelay = $this->getNextTrackingDelay($status);
|
||||
TrackShipmentJob::dispatch($this->dhlShipment, $this->options)
|
||||
->delay(now()->addMinutes($nextTrackingDelay));
|
||||
|
||||
|
||||
Log::info('[DHL Queue] Next tracking job scheduled', [
|
||||
'shipment_id' => $this->dhlShipment->id,
|
||||
'delay_minutes' => $nextTrackingDelay,
|
||||
|
|
@ -103,7 +102,7 @@ class TrackShipmentJob implements ShouldQueue
|
|||
} catch (Exception $e) {
|
||||
Log::warning('[DHL Queue] Shipment tracking failed', [
|
||||
'shipment_id' => $this->dhlShipment->id,
|
||||
'tracking_number' => $this->dhlShipment->tracking_number,
|
||||
'dhl_shipment_no' => $this->dhlShipment->dhl_shipment_no,
|
||||
'error' => $e->getMessage(),
|
||||
'attempt' => $this->attempts(),
|
||||
'max_tries' => $this->tries,
|
||||
|
|
@ -115,7 +114,7 @@ class TrackShipmentJob implements ShouldQueue
|
|||
'shipment_id' => $this->dhlShipment->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
|
||||
// Don't re-throw for final attempt - just log and continue
|
||||
return;
|
||||
}
|
||||
|
|
@ -126,14 +125,12 @@ class TrackShipmentJob implements ShouldQueue
|
|||
|
||||
/**
|
||||
* Handle a job failure.
|
||||
*
|
||||
* @param Exception $exception
|
||||
*/
|
||||
public function failed(Exception $exception): void
|
||||
{
|
||||
Log::warning('[DHL Queue] TrackShipmentJob permanently failed', [
|
||||
'shipment_id' => $this->dhlShipment->id,
|
||||
'tracking_number' => $this->dhlShipment->tracking_number,
|
||||
'dhl_shipment_no' => $this->dhlShipment->dhl_shipment_no,
|
||||
'error' => $exception->getMessage(),
|
||||
]);
|
||||
|
||||
|
|
@ -142,29 +139,14 @@ class TrackShipmentJob implements ShouldQueue
|
|||
|
||||
/**
|
||||
* Determine if we should continue tracking this shipment
|
||||
*
|
||||
* @param string $status
|
||||
* @return bool
|
||||
*/
|
||||
private function shouldContinueTracking(string $status): bool
|
||||
{
|
||||
$finalStates = [
|
||||
'delivered',
|
||||
'delivered_to_recipient',
|
||||
'delivered_to_pickup_location',
|
||||
'returned_to_sender',
|
||||
'cancelled',
|
||||
'lost',
|
||||
];
|
||||
|
||||
return !in_array(strtolower($status), $finalStates);
|
||||
return ! in_array(strtolower($status), DhlShipment::TERMINAL_STATUSES, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get delay for next tracking update based on current status
|
||||
*
|
||||
* @param string $status
|
||||
* @return int Minutes until next tracking
|
||||
*/
|
||||
private function getNextTrackingDelay(string $status): int
|
||||
{
|
||||
|
|
@ -184,10 +166,8 @@ class TrackShipmentJob implements ShouldQueue
|
|||
|
||||
/**
|
||||
* Determine the time at which the job should timeout.
|
||||
*
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function retryUntil()
|
||||
public function retryUntil(): DateTime
|
||||
{
|
||||
return now()->addMinutes(30); // Short timeout for tracking
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue