61 lines
1.8 KiB
PHP
61 lines
1.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Modules\RestaurantDelivery\Console\Commands;
|
||
|
|
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use Modules\RestaurantDelivery\Jobs\CleanupStaleLocationsJob;
|
||
|
|
use Modules\RestaurantDelivery\Models\LocationLog;
|
||
|
|
use Modules\RestaurantDelivery\Models\Rider;
|
||
|
|
|
||
|
|
class CleanupStaleData extends Command
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The name and signature of the console command.
|
||
|
|
*/
|
||
|
|
protected $signature = 'delivery:cleanup
|
||
|
|
{--days=7 : Number of days to keep location logs}
|
||
|
|
{--queue : Dispatch as a background job}';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The console command description.
|
||
|
|
*/
|
||
|
|
protected $description = 'Clean up stale location logs and mark offline riders';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the console command.
|
||
|
|
*/
|
||
|
|
public function handle(): int
|
||
|
|
{
|
||
|
|
$days = (int) $this->option('days');
|
||
|
|
$queue = $this->option('queue');
|
||
|
|
|
||
|
|
if ($queue) {
|
||
|
|
dispatch(new CleanupStaleLocationsJob($days));
|
||
|
|
$this->info('Cleanup job dispatched to queue.');
|
||
|
|
|
||
|
|
return self::SUCCESS;
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->info("Cleaning up data older than {$days} days...");
|
||
|
|
|
||
|
|
// Delete old location logs
|
||
|
|
$deletedLogs = LocationLog::where('recorded_at', '<', now()->subDays($days))
|
||
|
|
->delete();
|
||
|
|
|
||
|
|
$this->info("Deleted {$deletedLogs} old location log entries.");
|
||
|
|
|
||
|
|
// Mark stale riders as offline
|
||
|
|
$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]);
|
||
|
|
|
||
|
|
$this->info("Marked {$ridersMarkedOffline} riders as offline (no update for {$offlineThreshold}s).");
|
||
|
|
|
||
|
|
return self::SUCCESS;
|
||
|
|
}
|
||
|
|
}
|