Преглед на файлове

增加package 配置识别主线程

gemercheung преди 2 години
родител
ревизия
4a5e7ad7a5
променени са 4 файла, в които са добавени 532 реда и са изтрити 31 реда
  1. 2 0
      package.json
  2. 518 26
      pnpm-lock.yaml
  3. 1 1
      src/room-manager/room-manager.controller.ts
  4. 11 4
      src/room/delay/delay.service.ts

+ 2 - 0
package.json

@@ -42,6 +42,8 @@
     "nest-nacos": "^0.4.2",
     "nest-winston": "^1.8.0",
     "nestjs-throttler-storage-redis": "^0.3.0",
+    "pm2-master-process": "^2.0.0",
+    "promise-based-task": "^2.2.0",
     "redis": "^4.6.4",
     "reflect-metadata": "^0.1.13",
     "rxjs": "^7.2.0",

Файловите разлики са ограничени, защото са твърде много
+ 518 - 26
pnpm-lock.yaml


+ 1 - 1
src/room-manager/room-manager.controller.ts

@@ -20,7 +20,7 @@ export class RoomManagerController {
     private readonly roomManagerService: RoomManagerService,
     private readonly roomService: RoomService,
     private readonly usersService: UsersService,
-  ) { }
+  ) {}
 
   @Get('/danmaku/:id')
   @ApiOperation({ summary: '获取房间弹幕列表' })

+ 11 - 4
src/room/delay/delay.service.ts

@@ -8,9 +8,11 @@ import { ConfigService } from '@nestjs/config';
 import { UsersService } from '../users/users.service';
 import { RoomService } from '../room.service';
 import { TempService } from '../temp/temp.service';
+import { isMasterProcess } from 'pm2-master-process';
 dayjs.extend(duration);
 @Injectable()
 export class DelayService {
+  private isMasterProcess = false;
   constructor(
     @InjectRedis() private readonly redis: Redis,
     @InjectRedis('sub') private readonly subRedis: Redis,
@@ -21,19 +23,22 @@ export class DelayService {
     public readonly tempService: TempService,
   ) {}
 
-  init(): void {
+  async init(): Promise<void> {
     const redisDB = this.configService.get<string>('REDIS_DB');
     // console.log('redisDB', redisDB);
     this.redis.config('SET', 'notify-keyspace-events', 'Ex');
     const subscribeEvent = `__keyevent@${redisDB}__:expired`;
     // console.log('subRedis', this.subRedis);
     this.subRedis.subscribe(subscribeEvent);
-    this.subRedis.on('message', (channel, key) => {
+    this.isMasterProcess = await isMasterProcess();
+
+    this.subRedis.on('message', async (channel, key) => {
       if (channel === subscribeEvent) {
         this.handleExpiredKeys(key);
       }
     });
   }
+
   async handleExpiredKeys(key: string) {
     switch (true) {
       //过期下线的
@@ -59,7 +64,8 @@ export class DelayService {
     // const params = tKey.split(':');
     const RoomId = tKey;
     // const UserId = params[1];
-    this.roomService.logger.warn(`解散临时房间 :${RoomId}`, 'temp-room');
+    this.isMasterProcess &&
+      this.roomService.logger.warn(`解散临时房间 :${RoomId}`, 'temp-room');
     await this.roomService.handleRoomDismiss(RoomId);
     this.roomService.socketGateway.server.in(RoomId).disconnectSockets();
   }
@@ -72,7 +78,8 @@ export class DelayService {
     if (delayUser && !delayUser.IsOnline) {
       const roomUsers = await this.userService.getRoomUsers(RoomId);
       const filterRoomUser = roomUsers.filter((i) => i.UserId !== UserId);
-      console.log('delayUser', delayUser.UserId);
+      this.isMasterProcess &&
+        this.roomService.logger.warn(delayUser.UserId, 'delay-user');
       const res = await this.userService.deleteRoomUser(RoomId, UserId);
       if (res) {
         if (delayUser.Role === 'leader') {