shoppingOrder = $shoppingOrder; $this->weight = $weight; $this->options = $options; // Load DHL config once when creating the job if (empty($dhlConfig)) { $settingController = new \App\Http\Controllers\SettingController(); $this->dhlConfig = $settingController->getDhlConfig(); } else { $this->dhlConfig = $dhlConfig; } // 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(), ]); // Use DHL configuration loaded in constructor $dhlClient = new \Acme\Dhl\Support\DhlClient( $this->dhlConfig['base_url'], $this->dhlConfig['api_key'], $this->dhlConfig['username'], $this->dhlConfig['password'] ); $shippingService = new \Acme\Dhl\Services\ShippingService($dhlClient); // Prepare order data using helper $orderData = DhlDataHelper::prepareOrderData($this->shoppingOrder, $this->weight, $this->options, $this->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. * * @param Exception $exception */ 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); } }