| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace App\Services\AdminRealtime;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 会员代理申请由 melbet_sport-api 直接插入 agent_applications,不经 bot-28 模型钩子。
- * relay 每轮按主键游标找出新申请并登记 created 事件;审核等修改在 bot-28 内经模型钩子登记。
- *
- * 游标存缓存:首次运行(或缓存丢失)从当前最大 id 开始,不补推历史申请;
- * 列表与待处理角标不受影响,前端重连时会重新拉列表。
- */
- final class AdminRealtimeAgentApplications
- {
- private const CURSOR = 'admin_realtime:agent_applications_cursor';
- private const BATCH = 200;
- public static function scan(): void
- {
- if (!config('admin_realtime.enabled')) return;
- try {
- $cursor = Cache::get(self::CURSOR);
- if ($cursor === null) {
- Cache::forever(self::CURSOR, (int) DB::table('agent_applications')->max('id'));
- return;
- }
- $ids = DB::table('agent_applications')->where('id', '>', (int) $cursor)
- ->orderBy('id')->limit(self::BATCH)->pluck('id')->map(static fn($id): int => (int) $id)->all();
- if (!$ids) return;
- // 先登记再推进游标;登记失败抛出,下轮重试(最多重复,不会漏)。
- $now = now('Asia/Shanghai')->format('Y-m-d H:i:s');
- DB::table('admin_realtime_events')->insert(array_map(static fn(int $id): array => [
- 'resource' => 'agent_applications', 'resource_id' => (string) $id, 'action' => 'created',
- 'source' => 'melbet_sport-api', 'created_at' => $now,
- ], $ids));
- Cache::forever(self::CURSOR, max($ids));
- } catch (\Throwable $e) {
- Log::channel('realtime')->error('admin_realtime_agent_application_scan_failed', ['error' => $e->getMessage()]);
- }
- }
- }
|