|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Models\Server; |
| 6 | +use App\Models\User; |
| 7 | +use App\Services\NodeSyncService; |
| 8 | +use Illuminate\Console\Command; |
| 9 | +use Illuminate\Support\Facades\Redis; |
| 10 | + |
| 11 | +class CheckTrafficExceeded extends Command |
| 12 | +{ |
| 13 | + protected $signature = 'check:traffic-exceeded'; |
| 14 | + protected $description = '检查流量超标用户并通知节点'; |
| 15 | + |
| 16 | + public function handle() |
| 17 | + { |
| 18 | + $count = Redis::scard('traffic:pending_check'); |
| 19 | + if ($count <= 0) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + $pendingUserIds = array_map('intval', Redis::spop('traffic:pending_check', $count)); |
| 24 | + |
| 25 | + $exceededUsers = User::toBase() |
| 26 | + ->whereIn('id', $pendingUserIds) |
| 27 | + ->whereRaw('u + d >= transfer_enable') |
| 28 | + ->where('transfer_enable', '>', 0) |
| 29 | + ->where('banned', 0) |
| 30 | + ->select(['id', 'group_id']) |
| 31 | + ->get(); |
| 32 | + |
| 33 | + if ($exceededUsers->isEmpty()) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $groupedUsers = $exceededUsers->groupBy('group_id'); |
| 38 | + $notifiedCount = 0; |
| 39 | + |
| 40 | + foreach ($groupedUsers as $groupId => $users) { |
| 41 | + if (!$groupId) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + $userIdsInGroup = $users->pluck('id')->toArray(); |
| 46 | + $servers = Server::whereJsonContains('group_ids', (string) $groupId)->get(); |
| 47 | + |
| 48 | + foreach ($servers as $server) { |
| 49 | + if (!NodeSyncService::isNodeOnline($server->id)) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + NodeSyncService::push($server->id, 'sync.user.delta', [ |
| 54 | + 'action' => 'remove', |
| 55 | + 'users' => array_map(fn($id) => ['id' => $id], $userIdsInGroup), |
| 56 | + ]); |
| 57 | + $notifiedCount++; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + $this->info("Checked " . count($pendingUserIds) . " users, notified {$notifiedCount} nodes for " . $exceededUsers->count() . " exceeded users."); |
| 62 | + } |
| 63 | +} |
0 commit comments