Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 10x 7x 10x 10x 1x 3x 1x 3x 3x 1x 7x 7x 7x 7x 7x 1x | import { BotDatabaseService } from '@baneverywhere/db'; import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; import { Cron, CronExpression } from '@nestjs/schedule'; import { BOT_CONNECTION } from '@baneverywhere/namespaces'; import { ClientProxy } from '@nestjs/microservices'; import { BotPatterns } from '@baneverywhere/bot-interfaces'; import { v4 as uuidv4 } from 'uuid'; import { logError } from '@baneverywhere/error-handler'; import { TwitchClientService } from '@baneverywhere/twitch-client'; import { ConfigService } from '@nestjs/config'; import { InjectQueue } from '@nestjs/bull'; import { Queue } from 'bull'; import { lastValueFrom } from 'rxjs'; @Injectable() export class AppService implements OnModuleInit { private MAX_USERS_PER_BOT: number; constructor( public readonly dbService: BotDatabaseService, @Inject(BOT_CONNECTION) public botClient: ClientProxy, public readonly twitchClientService: TwitchClientService, public readonly configService: ConfigService, @InjectQueue('queue') private readonly queue: Queue, ) { this.MAX_USERS_PER_BOT = parseInt( this.configService.get<string>('MAX_USERS_PER_BOT', '1000') ); } onModuleInit() { this.checkIfUserIsOnline(); } @Cron(CronExpression.EVERY_MINUTE) @logError() async checkOnline() { this.checkIfUserIsOnline(); } @Cron(CronExpression.EVERY_MINUTE) @logError() synchronizeBotStatus() { this.botClient.emit<void, unknown>(BotPatterns.BOT_GET_STATUS, uuidv4()); } @Cron(CronExpression.EVERY_MINUTE) @logError() async removeMachinesInLimbo() { const machines = await this.dbService.machine.findMany({ where: { lastSeen: { lt: new Date(Date.now() - 1000 * 120), }, }, select: { uuid: true, }, }); await this.dbService.user.updateMany({ where: { machineUUID: { in: machines.map((m) => m.uuid), }, }, data: { machineUUID: null, }, }); await this.dbService.machine.deleteMany({ where: { uuid: { in: machines.map((m) => m.uuid), }, }, }); } @logError() async preassignMachineToUser(username: string): Promise<string> { const machinesWithCount = await this.dbService.machine.findMany({ select: { uuid: true, _count: { select: { users: true, }, }, }, orderBy: { users: { _count: 'asc', }, }, take: 1, }); const machine = machinesWithCount.find( (m) => m._count.users < this.MAX_USERS_PER_BOT ); Iif (!machine) throw new Error('No machine available'); await this.dbService.user.update({ where: { login: username, }, data: { machineUUID: machine.uuid, }, }); return machine.uuid; } @logError() async checkIfUserIsOnline(cursor?: number) { const users = await this.dbService.user.findMany({ select: { id: true, login: true, machineUUID: true, }, take: 20, skip: cursor ? 1 : 0, ...(cursor ? { cursor: { id: cursor } } : {}), }); Iif (users.length === 0) return; const { data: { data: channels }, } = await this.twitchClientService.checkUsersStatus( users.map((u) => u.login) ); const usernames = channels.map((channel) => channel.user_login); const online = users.filter((user) => usernames.includes(user.login)); const offline = users.filter((user) => !usernames.includes(user.login)); await this.dbService.user.updateMany({ where: { login: { in: offline.map((u) => u.login), }, }, data: { machineUUID: null, }, }); await Promise.all( offline.map(async (u) => { this.botClient.emit(BotPatterns.USER_OFFLINE, { channelName: u.login, botId: u.machineUUID, }); }) ); await Promise.all( online.map(async (user) => { Iif (user.machineUUID) return; const machineID = await this.preassignMachineToUser(user.login); await lastValueFrom(this.botClient.send(BotPatterns.USER_ONLINE, { channelName: user.login, botId: machineID, })); this.queue.add('queue', { username: user.login }); }) ); Iif (users.length === 20) { await this.checkIfUserIsOnline(users[19].id); } } } |