96 lines
2.8 KiB
PHP
96 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Modules\RestaurantDelivery\Jobs;
|
||
|
|
|
||
|
|
use Illuminate\Bus\Queueable;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Modules\RestaurantDelivery\Enums\DeliveryStatus;
|
||
|
|
use Modules\RestaurantDelivery\Models\Delivery;
|
||
|
|
|
||
|
|
class CheckAssignmentTimeoutJob implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The number of times the job may be attempted.
|
||
|
|
*/
|
||
|
|
public int $tries = 1;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new job instance.
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public readonly Delivery $delivery
|
||
|
|
) {
|
||
|
|
$this->onQueue(config('restaurant-delivery.queue.queues.assignment', 'restaurant-delivery-assignment'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the job.
|
||
|
|
*/
|
||
|
|
public function handle(): void
|
||
|
|
{
|
||
|
|
// Refresh delivery from database
|
||
|
|
$delivery = $this->delivery->fresh();
|
||
|
|
|
||
|
|
// Skip if already assigned
|
||
|
|
if ($delivery->rider_id && $delivery->status === DeliveryStatus::RIDER_ASSIGNED) {
|
||
|
|
Log::info('Delivery already assigned, skipping timeout check', [
|
||
|
|
'delivery_id' => $delivery->id,
|
||
|
|
'rider_id' => $delivery->rider_id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Skip if cancelled or completed
|
||
|
|
if (in_array($delivery->status, [DeliveryStatus::CANCELLED, DeliveryStatus::DELIVERED, DeliveryStatus::FAILED])) {
|
||
|
|
Log::info('Delivery is no longer active, skipping assignment', [
|
||
|
|
'delivery_id' => $delivery->id,
|
||
|
|
'status' => $delivery->status->value,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check if max reassignments reached
|
||
|
|
$maxReassignments = config('restaurant-delivery.assignment.max_reassignments', 3);
|
||
|
|
if ($delivery->reassignment_count >= $maxReassignments) {
|
||
|
|
Log::warning('Max reassignments reached for delivery', [
|
||
|
|
'delivery_id' => $delivery->id,
|
||
|
|
'reassignment_count' => $delivery->reassignment_count,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Increment reassignment count
|
||
|
|
$delivery->increment('reassignment_count');
|
||
|
|
|
||
|
|
// Retry assignment
|
||
|
|
Log::info('Assignment timed out, retrying', [
|
||
|
|
'delivery_id' => $delivery->id,
|
||
|
|
'attempt' => $delivery->reassignment_count,
|
||
|
|
]);
|
||
|
|
|
||
|
|
dispatch(new AssignRiderJob($delivery));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle a job failure.
|
||
|
|
*/
|
||
|
|
public function failed(\Throwable $exception): void
|
||
|
|
{
|
||
|
|
Log::error('Assignment timeout check failed', [
|
||
|
|
'delivery_id' => $this->delivery->id,
|
||
|
|
'error' => $exception->getMessage(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|