Skip to content

Commit 025adc9

Browse files
committed
fix: user-db,session-eventhandler 모듈화를 통한 책임 분리
1 parent f8f16ad commit 025adc9

File tree

8 files changed

+70
-41
lines changed

8 files changed

+70
-41
lines changed

BE/src/query/query.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { ResponseDto } from '../common/response/response.dto';
1313
import { ApiExtraModels, ApiTags } from '@nestjs/swagger';
1414
import { ResQueryDto } from './dto/res-query.dto';
1515
import { ExecuteQuerySwagger } from '../config/swagger/query-swagger.decorator';
16-
import { Request } from 'express';
1716
import { Serialize } from '../interceptors/serialize.interceptor';
1817
import { ShellGuard } from '../guard/shell.guard';
1918
import { UserDBConnectionInterceptor } from '../interceptors/user-db-connection.interceptor';

BE/src/query/query.module.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import { QueryService } from './query.service';
33
import { QueryController } from './query.controller';
44
import { TypeOrmModule } from '@nestjs/typeorm';
55
import { Shell } from '../shell/shell.entity';
6-
import { ShellService } from '../shell/shell.service';
7-
import { UserDBManager } from '../config/query-database/user-db-manager.service';
86
import { UsageModule } from '../usage/usage.module';
97
import { RedisModule } from '../config/redis/redis.module';
8+
import { UserDBModule } from '../config/query-database/user-db.moudle';
9+
import { ShellModule } from '../shell/shell.module';
1010

1111
@Module({
12-
imports: [TypeOrmModule.forFeature([Shell]), UsageModule, RedisModule],
12+
imports: [
13+
TypeOrmModule.forFeature([Shell]),
14+
UsageModule,
15+
RedisModule,
16+
UserDBModule,
17+
ShellModule,
18+
],
1319
controllers: [QueryController],
14-
providers: [QueryService, ShellService, UserDBManager],
20+
providers: [QueryService],
1521
})
1622
export class QueryModule {}

DBManager/src/active-user/active-user.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Cron, CronExpression } from '@nestjs/schedule';
55
@Injectable()
66
export class ActiveUserService implements OnModuleInit {
77
private activeUserVariation = new Map<string, number>();
8-
private redisService: RedisService;
8+
constructor(private readonly redisService: RedisService) {}
99

1010
onModuleInit() {
1111
this.countUp();
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { RedisService } from '../config/redis/redis.service';
2-
import { OnModuleInit } from '@nestjs/common';
3-
import { UserDBConnectionService } from './user-db-connection.service';
2+
import { Injectable, OnModuleInit } from '@nestjs/common';
3+
import { UserDBService } from '../user-db/user-db.service';
44

5-
export class UserSessionDBService implements OnModuleInit {
6-
private redisService: RedisService;
7-
private dbService: UserDBConnectionService;
5+
@Injectable()
6+
export class SessionEventHandler implements OnModuleInit {
7+
constructor(
8+
private readonly redisService: RedisService,
9+
private readonly userDBService: UserDBService,
10+
) {}
811

912
onModuleInit() {
1013
this.createDB();
@@ -15,15 +18,15 @@ export class UserSessionDBService implements OnModuleInit {
1518
const channel = '__keyspace@0__:APPEND';
1619
this.redisService.subscribeSession(channel, async (sessionId) => {
1720
const pod = await this.redisService.hgetSession(sessionId, 'pod');
18-
this.dbService.initUserDatabase(pod, sessionId);
21+
this.userDBService.initUserDatabase(pod, sessionId);
1922
});
2023
}
2124

2225
private removeDB() {
2326
const channel = '__keyspace@0__:EXPIRE';
2427
this.redisService.subscribeSession(channel, async (sessionId) => {
2528
const pod = await this.redisService.hgetSession(sessionId, 'pod');
26-
this.dbService.removeDatabase(pod, sessionId);
29+
this.userDBService.removeDatabase(pod, sessionId);
2730
});
2831
}
2932
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Module } from '@nestjs/common';
2+
import { RedisModule } from '../config/redis/redis.module';
3+
import { UserDBModule } from '../user-db/user-db.module';
4+
import { SessionEventHandler } from './session-event.handler';
5+
6+
@Module({
7+
imports: [RedisModule, UserDBModule],
8+
providers: [SessionEventHandler],
9+
})
10+
export class SessionEventModule {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
3+
import { Connection, createConnection } from 'mysql2/promise';
4+
5+
@Injectable()
6+
export class UserDBConnector {
7+
private connectionMap = new Map<string, Connection>();
8+
9+
constructor(private readonly configService: ConfigService) {}
10+
11+
public async getConnection(pod: string) {
12+
if (!this.connectionMap.has(pod)) {
13+
const connection = await this.createConnectionByPod(pod);
14+
this.connectionMap.set(pod, connection);
15+
}
16+
return this.connectionMap.get(pod);
17+
}
18+
19+
private async createConnectionByPod(pod: string) {
20+
return createConnection({
21+
host: pod,
22+
user: this.configService.get<string>('QUERY_DB_USER'),
23+
password: this.configService.get<string>('QUERY_DB_PASSWORD'),
24+
port: this.configService.get<number>('QUERY_DB_PORT'),
25+
});
26+
}
27+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Module } from '@nestjs/common';
2-
import { UserSessionDBService } from './user-session-db.service';
2+
import { RedisModule } from '../config/redis/redis.module';
3+
import { UserDBService } from './user-db.service';
4+
import { UserDBConnector } from './user-db.connector';
35

46
@Module({
5-
providers: [UserSessionDBService],
7+
imports: [RedisModule],
8+
providers: [UserDBService, UserDBConnector],
69
})
710
export class UserDBModule {}
Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1+
import { UserDBConnector } from './user-db.connector';
12
import { Injectable } from '@nestjs/common';
2-
import { ConfigService } from '@nestjs/config';
3-
import { Connection, createConnection } from 'mysql2/promise';
3+
import * as console from 'node:console';
44

55
@Injectable()
6-
export class UserDBConnectionService {
7-
private connectionMap = new Map<string, Connection>();
8-
9-
constructor(private readonly configService: ConfigService) {}
10-
11-
private async getConnection(pod: string) {
12-
if (!this.connectionMap.has(pod)) {
13-
const connection = await this.createConnectionByPod(pod);
14-
this.connectionMap.set(pod, connection);
15-
}
16-
return this.connectionMap.get(pod);
17-
}
18-
19-
private async createConnectionByPod(pod: string) {
20-
return createConnection({
21-
host: pod,
22-
user: this.configService.get<string>('QUERY_DB_USER'),
23-
password: this.configService.get<string>('QUERY_DB_PASSWORD'),
24-
port: this.configService.get<number>('QUERY_DB_PORT'),
25-
});
26-
}
6+
export class UserDBService {
7+
constructor(private readonly userDBConnector: UserDBConnector) {}
278

289
public async initUserDatabase(pod: string, sessionId: string) {
2910
try {
@@ -35,7 +16,7 @@ export class UserDBConnectionService {
3516
database: identify,
3617
};
3718

38-
const connection = await this.getConnection(pod);
19+
const connection = await this.userDBConnector.getConnection(pod);
3920

4021
await connection.query(`create database ${connectInfo.database};`);
4122
await connection.query(
@@ -51,7 +32,7 @@ export class UserDBConnectionService {
5132

5233
public async removeDatabase(pod: string, sessionId: string) {
5334
try {
54-
const connection = await this.getConnection(pod);
35+
const connection = await this.userDBConnector.getConnection(pod);
5536

5637
const identify = sessionId.substring(0, 10);
5738

@@ -63,4 +44,4 @@ export class UserDBConnectionService {
6344
console.error(e);
6445
}
6546
}
66-
}
47+
}

0 commit comments

Comments
 (0)