|
@@ -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') {
|