Skip to content

Commit

Permalink
feat: 컨테이너 복구
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizyHub authored and flydog98 committed Nov 30, 2023
1 parent ca70d2a commit 147c345
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
25 changes: 25 additions & 0 deletions packages/backend/src/containers/containers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CommandResponseDto } from '../quizzes/dto/command-response.dto';
import shellEscape from 'shell-escape';
import { v4 as uuidv4 } from 'uuid';
import { SshService } from '../ssh/ssh.service';
import { ActionType } from '../session/schema/session.schema';

@Injectable()
export class ContainersService {
Expand Down Expand Up @@ -106,4 +107,28 @@ export class ContainersService {
throw new Error(stderrData);
}
}

async restoreContainer(logObject: {
status: string;
logs: {
mode: ActionType;
message: string;
}[];
containerId: string;
}): Promise<void> {
this.logger.log('info', 'restoring container...');
const { logs, containerId } = logObject;

let recentMessage = '';
for (const log of logs) {
if (log.mode === 'command') {
await this.runGitCommand(containerId, log.message);
} else if (log.mode === 'editor') {
await this.runEditorCommand(containerId, recentMessage, log.message);
} else {
throw new Error('Invalid log mode');
}
recentMessage = log.message;
}
}
}
28 changes: 21 additions & 7 deletions packages/backend/src/quizzes/quizzes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export class QuizzesController {
id,
containerId,
);
await this.containerService.restoreContainer(
await this.sessionService.getLogObject(sessionId, id),
);
}

// 리팩토링 필수입니다.
Expand Down Expand Up @@ -274,18 +277,29 @@ export class QuizzesController {
): Promise<SubmitDto> {
if (!sessionId) return new Fail();
try {
const containerId = await this.sessionService.getContainerIdBySessionId(
let containerId = await this.sessionService.getContainerIdBySessionId(
sessionId,
id,
);

if (!containerId) {
return;
}

if (!(await this.containerService.isValidateContainerId(containerId))) {
if (
!containerId ||
!(await this.containerService.isValidateContainerId(containerId))
) {
// 재현해서 컨테이너 발급하기
return;
this.logger.log(
'info',
'no docker container or invalid container Id. creating container..',
);
containerId = await this.containerService.getContainer(id);
await this.sessionService.setContainerBySessionId(
sessionId,
id,
containerId,
);
await this.containerService.restoreContainer(
await this.sessionService.getLogObject(sessionId, id),
);
}

const result: boolean = await this.quizWizardService.submit(
Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/session/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,12 @@ export class SessionService {
session.save();
this.logger.log('info', `session ${session._id as ObjectId} deleted`);
}

async getLogObject(sessionId: string, problemId: number): Promise<any> {
const session = await this.getSessionById(sessionId);
if (!session.problems.get(problemId)) {
throw new Error('problem not found');
}
return session.problems.get(problemId);
}
}

0 comments on commit 147c345

Please sign in to comment.