179 lines
5.3 KiB
PHP
179 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\ShoppingOrder;
|
|
use App\Services\DhlDataHelper;
|
|
use Exception;
|
|
use Illuminate\Bus\Queueable as BusQueueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Job to create DHL shipments asynchronously
|
|
*
|
|
* This job handles the creation of DHL shipments in the background,
|
|
* preventing API timeouts and improving user experience.
|
|
*/
|
|
class CreateShipmentJob implements ShouldQueue
|
|
{
|
|
use BusQueueable, Dispatchable, InteractsWithQueue, SerializesModels;
|
|
|
|
/**
|
|
* @var ShoppingOrder
|
|
*/
|
|
public $shoppingOrder;
|
|
|
|
/**
|
|
* @var float
|
|
*/
|
|
public $weight;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
public $options;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
public $dhlConfig;
|
|
|
|
/**
|
|
* The number of times the job may be attempted.
|
|
*
|
|
* @var int
|
|
*/
|
|
public $tries = 3;
|
|
|
|
/**
|
|
* The maximum number of seconds the job can run before timing out.
|
|
*
|
|
* @var int
|
|
*/
|
|
public $timeout = 120;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param ShoppingOrder $shoppingOrder
|
|
* @param float $weight
|
|
* @param array $options
|
|
* @param array|null $dhlConfig
|
|
*/
|
|
public function __construct(ShoppingOrder $shoppingOrder, float $weight = 1.0, array $options = [], array $dhlConfig = [])
|
|
{
|
|
$this->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);
|
|
}
|
|
}
|