mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-28 06:47:24 +08:00
fix: dedup device IPs, reset stale online_count on disconnect and scheduled cleanup (#886)
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class CleanupOnlineStatus extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'cleanup:online-status';
|
||||||
|
|
||||||
|
protected $description = 'Reset stale online_count for users whose devices have expired from Redis';
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$affected = User::where('online_count', '>', 0)
|
||||||
|
->where(function ($query) {
|
||||||
|
$query->where('last_online_at', '<', now()->subMinutes(10))
|
||||||
|
->orWhereNull('last_online_at');
|
||||||
|
})
|
||||||
|
->update(['online_count' => 0]);
|
||||||
|
|
||||||
|
if ($affected > 0) {
|
||||||
|
$this->info("Reset online_count for {$affected} stale users.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,6 +42,8 @@ class Kernel extends ConsoleKernel
|
|||||||
$schedule->command('send:remindMail', ['--force'])->dailyAt('11:30')->onOneServer();
|
$schedule->command('send:remindMail', ['--force'])->dailyAt('11:30')->onOneServer();
|
||||||
// horizon metrics
|
// horizon metrics
|
||||||
$schedule->command('horizon:snapshot')->everyFiveMinutes()->onOneServer();
|
$schedule->command('horizon:snapshot')->everyFiveMinutes()->onOneServer();
|
||||||
|
// cleanup stale online_count (GC for Redis TTL expiration)
|
||||||
|
$schedule->command('cleanup:online-status')->everyFiveMinutes()->onOneServer();
|
||||||
// backup Timing
|
// backup Timing
|
||||||
// if (env('ENABLE_AUTO_BACKUP_AND_UPDATE', false)) {
|
// if (env('ENABLE_AUTO_BACKUP_AND_UPDATE', false)) {
|
||||||
// $schedule->command('backup:database', ['true'])->daily()->onOneServer();
|
// $schedule->command('backup:database', ['true'])->daily()->onOneServer();
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ class DeviceStateService
|
|||||||
|
|
||||||
$this->removeNodeDevices($nodeId, $userId);
|
$this->removeNodeDevices($nodeId, $userId);
|
||||||
|
|
||||||
|
// Normalize: strip port suffix and deduplicate
|
||||||
|
$ips = array_values(array_unique(array_map([self::class, 'normalizeIP'], $ips)));
|
||||||
|
|
||||||
if (!empty($ips)) {
|
if (!empty($ips)) {
|
||||||
$fields = [];
|
$fields = [];
|
||||||
foreach ($ips as $ip) {
|
foreach ($ips as $ip) {
|
||||||
@@ -98,6 +101,7 @@ class DeviceStateService
|
|||||||
Redis::hdel($key, $field);
|
Redis::hdel($key, $field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->notifyUpdate($userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_keys($oldDevices);
|
return array_keys($oldDevices);
|
||||||
@@ -166,6 +170,22 @@ class DeviceStateService
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strip port from IP address: "1.2.3.4:12345" → "1.2.3.4", "[::1]:443" → "::1"
|
||||||
|
*/
|
||||||
|
private static function normalizeIP(string $ip): string
|
||||||
|
{
|
||||||
|
// [IPv6]:port
|
||||||
|
if (preg_match('/^\[(.+)\]:\d+$/', $ip, $m)) {
|
||||||
|
return $m[1];
|
||||||
|
}
|
||||||
|
// IPv4:port
|
||||||
|
if (preg_match('/^(\d+\.\d+\.\d+\.\d+):\d+$/', $ip, $m)) {
|
||||||
|
return $m[1];
|
||||||
|
}
|
||||||
|
return $ip;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* notify update (throttle control)
|
* notify update (throttle control)
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user