93 lines
2.6 KiB
PHP
93 lines
2.6 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\Cache;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Modules\RestaurantDelivery\Models\LocationLog;
|
||
|
|
use Modules\RestaurantDelivery\Models\Rider;
|
||
|
|
|
||
|
|
class CleanupStaleLocationsJob 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 int $daysToKeep = 7
|
||
|
|
) {
|
||
|
|
$this->onQueue(config('restaurant-delivery.queue.queues.analytics', 'restaurant-delivery-analytics'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the job.
|
||
|
|
*/
|
||
|
|
public function handle(): void
|
||
|
|
{
|
||
|
|
// Delete old location logs
|
||
|
|
$deletedLogs = LocationLog::where('recorded_at', '<', now()->subDays($this->daysToKeep))
|
||
|
|
->delete();
|
||
|
|
|
||
|
|
Log::info('Cleaned up old location logs', [
|
||
|
|
'deleted_count' => $deletedLogs,
|
||
|
|
'days_kept' => $this->daysToKeep,
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Mark riders as offline if no recent location update
|
||
|
|
$offlineThreshold = config('restaurant-delivery.firebase.location.offline_threshold', 120);
|
||
|
|
|
||
|
|
$ridersMarkedOffline = Rider::where('is_online', true)
|
||
|
|
->where('last_location_update', '<', now()->subSeconds($offlineThreshold))
|
||
|
|
->update(['is_online' => false]);
|
||
|
|
|
||
|
|
Log::info('Marked riders as offline', [
|
||
|
|
'count' => $ridersMarkedOffline,
|
||
|
|
'threshold_seconds' => $offlineThreshold,
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Clear stale cache entries
|
||
|
|
$this->clearStaleCacheEntries();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Clear stale cache entries.
|
||
|
|
*/
|
||
|
|
protected function clearStaleCacheEntries(): void
|
||
|
|
{
|
||
|
|
// Get all online riders and clear their old cache entries if needed
|
||
|
|
$riders = Rider::where('is_online', false)->get(['id']);
|
||
|
|
|
||
|
|
foreach ($riders as $rider) {
|
||
|
|
Cache::forget("rider_location_{$rider->id}");
|
||
|
|
Cache::forget("last_animation_point_{$rider->id}");
|
||
|
|
}
|
||
|
|
|
||
|
|
Log::info('Cleared stale cache entries', [
|
||
|
|
'rider_count' => $riders->count(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle a job failure.
|
||
|
|
*/
|
||
|
|
public function failed(\Throwable $exception): void
|
||
|
|
{
|
||
|
|
Log::error('Cleanup stale locations job failed', [
|
||
|
|
'error' => $exception->getMessage(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|