update 20.10.2025
This commit is contained in:
parent
8c11130b5d
commit
a939cd51ef
616 changed files with 84821 additions and 4121 deletions
148
dev/app-bak/Jobs/CreateReturnLabelJob.php
Normal file
148
dev/app-bak/Jobs/CreateReturnLabelJob.php
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\DhlShipment;
|
||||
use App\Services\DhlApiService;
|
||||
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 return labels asynchronously
|
||||
*
|
||||
* This job handles the creation of DHL return labels in the background,
|
||||
* preventing API timeouts and improving user experience.
|
||||
*/
|
||||
class CreateReturnLabelJob implements ShouldQueue
|
||||
{
|
||||
use BusQueueable, Dispatchable, InteractsWithQueue, SerializesModels;
|
||||
|
||||
/**
|
||||
* @var DhlShipment
|
||||
*/
|
||||
public $originalShipment;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $options;
|
||||
|
||||
/**
|
||||
* 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 DhlShipment $originalShipment
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(DhlShipment $originalShipment, array $options = [])
|
||||
{
|
||||
$this->originalShipment = $originalShipment;
|
||||
$this->options = $options;
|
||||
|
||||
// Set queue name based on priority
|
||||
if (isset($options['priority']) && $options['priority'] === 'high') {
|
||||
$this->onQueue('high-priority');
|
||||
} else {
|
||||
$this->onQueue('dhl-returns');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
try {
|
||||
Log::info('[DHL Queue] Starting return label creation job', [
|
||||
'original_shipment_id' => $this->originalShipment->id,
|
||||
'original_shipment_number' => $this->originalShipment->shipment_number,
|
||||
'attempt' => $this->attempts(),
|
||||
]);
|
||||
|
||||
$dhlService = new DhlApiService();
|
||||
|
||||
// Create the return label
|
||||
$returnShipment = $dhlService->createReturnLabel(
|
||||
$this->originalShipment,
|
||||
$this->options
|
||||
);
|
||||
|
||||
Log::info('[DHL Queue] Return label created successfully', [
|
||||
'original_shipment_id' => $this->originalShipment->id,
|
||||
'return_shipment_id' => $returnShipment->id,
|
||||
'return_shipment_number' => $returnShipment->shipment_number,
|
||||
]);
|
||||
|
||||
// Trigger follow-up actions if specified
|
||||
if (isset($this->options['auto_track']) && $this->options['auto_track']) {
|
||||
\App\Jobs\TrackShipmentJob::dispatch($returnShipment)->delay(now()->addMinutes(5));
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
Log::error('[DHL Queue] Return label creation failed', [
|
||||
'original_shipment_id' => $this->originalShipment->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] Return label creation permanently failed', [
|
||||
'original_shipment_id' => $this->originalShipment->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] CreateReturnLabelJob permanently failed', [
|
||||
'original_shipment_id' => $this->originalShipment->id,
|
||||
'original_shipment_number' => $this->originalShipment->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 return label creation
|
||||
// - Update original shipment to mark return label creation failed
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the time at which the job should timeout.
|
||||
*
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function retryUntil()
|
||||
{
|
||||
return now()->addHours(2);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue