shoppingOrder = $shoppingOrder; $this->weight = $weight; $this->options = $options; // Set queue name based on priority if (isset($options['priority']) && $options['priority'] === 'high') { $this->onQueue('high-priority'); } else { $this->onQueue('dhl-shipments'); } } /** * Execute the job. */ public function handle(): void { try { Log::info('[DHL Queue] Starting shipment creation job', [ 'order_id' => $this->shoppingOrder->id, 'weight' => $this->weight, 'attempt' => $this->attempts(), ]); // Load DHL configuration here, never from the serialized job payload. $dhlConfig = (new \App\Http\Controllers\SettingController)->getDhlConfig(); $dhlClient = new \Acme\Dhl\Support\DhlClient( $dhlConfig['base_url'], $dhlConfig['api_key'], $dhlConfig['username'], $dhlConfig['password'] ); $shippingService = new \Acme\Dhl\Services\ShippingService($dhlClient); // Prepare order data using helper $orderData = DhlDataHelper::prepareOrderData($this->shoppingOrder, $this->weight, $this->options, $dhlConfig); // Create the shipment using new package $result = $shippingService->createLabel($orderData); Log::info('[DHL Queue] Shipment created successfully', [ 'order_id' => $this->shoppingOrder->id, 'shipment_number' => $result['shipmentNumber'] ?? 'N/A', 'label_path' => $result['labelPath'] ?? 'N/A', ]); // Trigger follow-up actions if specified (if tracking number available) if (isset($this->options['auto_track']) && $this->options['auto_track'] && ! empty($result['trackingNumber'])) { Log::info('[DHL Queue] Scheduling tracking update', [ 'tracking_number' => $result['trackingNumber'], ]); // Note: TrackShipmentJob would need to be updated to work with tracking numbers } } catch (Exception $e) { Log::error('[DHL Queue] Shipment creation failed', [ 'order_id' => $this->shoppingOrder->id, '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 creation permanently failed', [ 'order_id' => $this->shoppingOrder->id, 'error' => $e->getMessage(), ]); } throw $e; // Re-throw to trigger retry mechanism } } /** * Handle a job failure. */ public function failed(Exception $exception): void { Log::error('[DHL Queue] CreateShipmentJob permanently failed', [ 'order_id' => $this->shoppingOrder->id, 'error' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ]); // You could implement additional failure handling here: // - Send notification to admin // - Update order status // - Create manual task for staff } /** * Determine the time at which the job should timeout. * * @return \DateTime */ public function retryUntil() { return now()->addHours(2); } }