dhlShipment = $dhlShipment; $this->options = $options; // Set queue name based on priority if (isset($options['priority']) && $options['priority'] === 'high') { $this->onQueue('high-priority'); } else { $this->onQueue('dhl-cancellations'); } } /** * Execute the job. */ public function handle(): void { try { Log::info('[DHL Queue] Starting shipment cancellation job', [ 'shipment_id' => $this->dhlShipment->id, 'shipment_number' => $this->dhlShipment->shipment_number, 'attempt' => $this->attempts(), ]); $dhlService = new DhlApiService(); // Cancel the shipment $success = $dhlService->cancelShipment($this->dhlShipment); if ($success) { Log::info('[DHL Queue] Shipment cancelled successfully', [ 'shipment_id' => $this->dhlShipment->id, 'shipment_number' => $this->dhlShipment->shipment_number, ]); } else { throw new Exception('Cancellation returned false'); } } catch (Exception $e) { Log::error('[DHL Queue] Shipment cancellation failed', [ 'shipment_id' => $this->dhlShipment->id, 'shipment_number' => $this->dhlShipment->shipment_number, 'error' => $e->getMessage(), 'attempt' => $this->attempts(), 'max_tries' => $this->tries, ]); // If this is the final attempt, mark as permanently failed if ($this->attempts() >= $this->tries) { Log::error('[DHL Queue] Shipment cancellation permanently failed', [ 'shipment_id' => $this->dhlShipment->id, 'error' => $e->getMessage(), ]); } throw $e; // Re-throw to trigger retry mechanism } } /** * Handle a job failure. * * @param Exception $exception */ public function failed(Exception $exception): void { Log::error('[DHL Queue] CancelShipmentJob permanently failed', [ 'shipment_id' => $this->dhlShipment->id, 'shipment_number' => $this->dhlShipment->shipment_number, 'error' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ]); // You could implement additional failure handling here: // - Send notification to admin // - Create manual task for staff to handle cancellation // - Update shipment status to indicate cancellation failed } /** * Determine the time at which the job should timeout. * * @return \DateTime */ public function retryUntil() { return now()->addHour(); // Shorter timeout for cancellations } }