Skip to content

Commit

Permalink
feat: 서버 모드 별 컨테이너 생성 로직 수정 및 getContainer 시 컨테이너 검사 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
flydog98 authored and LuizyHub committed Dec 6, 2023
1 parent 89c7f7b commit 373639a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/backend/src/containers/containers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export class ContainersService {
@Inject('winston') private readonly logger: Logger,
private commandService: CommandService,
) {
this.initializeContainers();
if (this.configService.get<string>('SERVER_MODE') !== 'dev') {
this.initializeContainers();
}
}

async initializeContainers() {
Expand Down Expand Up @@ -131,10 +133,15 @@ export class ContainersService {

async getContainer(
quizIdParam: number | string,
maxRetries = MAX_RETRY,
retry = MAX_RETRY,
): Promise<string> {
const quizId =
typeof quizIdParam === 'string' ? parseInt(quizIdParam, 10) : quizIdParam;

if (this.configService.get<string>('SERVER_MODE') === 'dev') {
return this.createContainer(quizId);
}

if (this.availableContainers.get(quizId).length > 0) {
const containerId = this.availableContainers.get(quizId).shift();

Expand All @@ -146,18 +153,22 @@ export class ContainersService {
this.availableContainers.get(quizId).push(containerId);
});

if (!(await this.isValidateContainerId(containerId))) {
return await this.createContainer(quizId);
}

return containerId;
}

if (maxRetries <= 0) {
if (retry <= 0) {
throw new Error('No available containers after maximum retries');
}

// 재시도 로직
return new Promise((resolve, reject) => {
setTimeout(async () => {
try {
const containerId = await this.getContainer(quizId, maxRetries - 1);
const containerId = await this.getContainer(quizId, retry - 1);
resolve(containerId);
} catch (error) {
reject(error);
Expand Down

0 comments on commit 373639a

Please sign in to comment.