Skip to content

Commit

Permalink
Merge pull request #75 from Sooamazing/develop
Browse files Browse the repository at this point in the history
refactor: runserver submit 시 CORRECT 불가로 log 추가
  • Loading branch information
devkeon committed Dec 27, 2023
2 parents 6f7924b + d1c2ff5 commit e0d9a44
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 222 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.goojeans.runserver.controller;

import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -22,13 +25,20 @@ public class ExecuteController {

private final ExecuteService executeService;

@ExceptionHandler(Exception.class)
public ApiResponse<Void> exceptionHandler(Exception e) {
return ApiResponse.serverErrorFrom(e.getMessage());
}

@PostMapping
public ApiResponse<ExecuteResponseDto> execute(@RequestBody @Validated ExecuteRequestDto executeRequestDto) {

// Error 포함 Response
ApiResponse<ExecuteResponseDto> executeResult = executeService.codeJudge(executeRequestDto);

return executeResult;

try {
List<ExecuteResponseDto> executeResult = executeService.codeJudge(executeRequestDto);
return ApiResponse.okFrom(executeResult);
} catch (Exception e) {
log.error("[runserver][controller][execute] execute 시 error 발생 ={}", e.getMessage());
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.goojeans.runserver.controller;

import java.util.List;

import com.goojeans.runserver.dto.request.SubmitRequestDto;
import com.goojeans.runserver.dto.response.ApiResponse;
import com.goojeans.runserver.dto.response.SubmitResponseDto;
Expand All @@ -9,6 +11,7 @@
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -22,13 +25,22 @@ public class SubmitController {

private final SubmitService submitService;

@ExceptionHandler(Exception.class)
public ApiResponse<Void> exceptionHandler(Exception e) {
return ApiResponse.serverErrorFrom(e.getMessage());
}

@PostMapping
public ApiResponse<SubmitResponseDto> submit(@Validated @RequestBody SubmitRequestDto submitRequestDto) {

// Error 포함 Response
ApiResponse<SubmitResponseDto> submitResponseDto = submitService.codeJudge(submitRequestDto);
try {
List<SubmitResponseDto> submitResponseDto = submitService.codeJudge(submitRequestDto);
return ApiResponse.okFrom(submitResponseDto);
} catch (Exception e) {
log.error("[runserver][controller] submit 시 error 발생 ={}", e.getMessage());
throw new RuntimeException(e);

return submitResponseDto;
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -55,9 +56,13 @@ public SubmitAllFilesSet downloadSubmitAllFilesFromS3(Extension fileExtension, l
String dirAbsolutePath,
String s3Key) {

File sourceCodeFile, excuteFile, errorFile, outputFile;
List<File> testcases, answers;
try {

File sourceCodeFile, excuteFile, errorFile, outputFile;
List<File> testcases, answers;
log.info("[runserver][repository][submit] downloadSubmitAllFilesFromS3 dirAbsolutePath ={}",
dirAbsolutePath);

// 절대 경로 지정 및 디렉토리 생성
String directoryPath = Files.createDirectories(Paths.get(dirAbsolutePath)).toAbsolutePath() + "/";

Expand Down Expand Up @@ -91,21 +96,23 @@ public SubmitAllFilesSet downloadSubmitAllFilesFromS3(Extension fileExtension, l
} else {
excuteFile = getBlankFile(directoryPath, "Main.py");
}

return SubmitAllFilesSet.of(sourceCodeFile, excuteFile, errorFile, outputFile, testcases, answers);
} catch (IOException e) {
log.error("{}", e.getMessage());
log.error("[runserver][repository] downloadSubmitAllFilesFromS3 시 error 발생 = {}", e.getMessage());
throw new RuntimeException(e);
}

return SubmitAllFilesSet.of(sourceCodeFile, excuteFile, errorFile, outputFile, testcases, answers);
}

public ExecuteAllFileSet downloadExecuteAllFilesFromS3(Extension fileExtension,
String dirAbsolutePath, String s3Key, String testCase) {

File sourceCodeFile, excuteFile, errorFile, outputFile, testCaseFile;
try {

log.info("[runserver][repository][execute] downloadExecuteAllFilesFromS3 dirAbsolutePath ={}",
dirAbsolutePath);

File sourceCodeFile, excuteFile, errorFile, outputFile, testCaseFile;
// 절대 경로 지정 및 디렉토리 생성
String directoryPath = Files.createDirectories(Paths.get(dirAbsolutePath)).toAbsolutePath() + "/";

Expand Down Expand Up @@ -136,13 +143,12 @@ public ExecuteAllFileSet downloadExecuteAllFilesFromS3(Extension fileExtension,
} else {
excuteFile = getBlankFile(directoryPath, "Main.py");
}

return ExecuteAllFileSet.of(sourceCodeFile, excuteFile, errorFile, outputFile, testCaseFile);
} catch (IOException e) {
log.error("{}", e.getMessage());
log.error("[runserver][repository] downloadExecuteAllFilesFromS3 시 error 발생 = {}", e.getMessage());
throw new RuntimeException(e);
}

return ExecuteAllFileSet.of(sourceCodeFile, excuteFile, errorFile, outputFile, testCaseFile);
}

/*
Expand All @@ -151,23 +157,19 @@ public ExecuteAllFileSet downloadExecuteAllFilesFromS3(Extension fileExtension,
* @param testCase
* @return File
*/
private File testStringToFile(String directoryPath, String testCase) throws IOException {
private File testStringToFile(String directoryPath, String testCase) {

File newFile = new File(directoryPath + "testCase.txt");
newFile.createNewFile();

OutputStream os = new FileOutputStream(newFile);
// null인 경우
if(testCase == null) {
os.write("".getBytes());
} else {
os.write(testCase.getBytes());
try (OutputStream os = new FileOutputStream(newFile)) {
newFile.createNewFile();
// null인 경우
os.write(Objects.requireNonNullElse(testCase, "").getBytes());
os.flush();
return newFile;
} catch (IOException e) {
log.error("[runserver][repository] testStringToFile 시 error 발생 = {}", e.getMessage());
throw new RuntimeException(e);
}
os.flush();
os.close();

return newFile;

}

/*
Expand All @@ -176,10 +178,15 @@ private File testStringToFile(String directoryPath, String testCase) throws IOEx
* @param nameExtension
* @return File
*/
private static File getBlankFile(String directoryPath, String nameExtension) throws IOException {
File newFile = new File(directoryPath + nameExtension);
newFile.createNewFile();
return newFile;
private static File getBlankFile(String directoryPath, String nameExtension) {
try {
File newFile = new File(directoryPath + nameExtension);
newFile.createNewFile();
return newFile;
} catch (IOException e) {
log.error("[runserver][repository] getBlankFile 시 error 발생 = {}", e.getMessage());
throw new RuntimeException(e);
}
}

/*
Expand All @@ -189,7 +196,7 @@ private static File getBlankFile(String directoryPath, String nameExtension) thr
* @param folerName
* @return List<File>
*/
public List<File> getFileListFromS3(String directoriesPath, long algorithmId, String folerName) throws IOException {
public List<File> getFileListFromS3(String directoriesPath, long algorithmId, String folerName) {

String folderNamePath = algorithmId + "/" + folerName;
List<File> fileList = new ArrayList<>();
Expand Down Expand Up @@ -221,6 +228,7 @@ public List<File> getFileListFromS3(String directoriesPath, long algorithmId, St
Collections.sort(fileList);

return fileList;

}

/*
Expand All @@ -229,42 +237,49 @@ public List<File> getFileListFromS3(String directoriesPath, long algorithmId, St
* @param s3Key
* @return File
*/
private File downloadFileFromS3(String directoriesPath, String s3Key) throws
SdkClientException,
S3Exception, IOException, NullPointerException {
private File downloadFileFromS3(String directoriesPath, String s3Key) {

// S3에서 파일이 있는지 확인
s3Client.headObject(HeadObjectRequest.builder()
.bucket(bucket)
.key(s3Key)
.build());
// S3에서 파일 가져와 내용 Byte로 저장
GetObjectRequest objectRequest = GetObjectRequest
.builder()
.key(s3Key)
.bucket(bucket)
.build();
try {
// S3에서 파일이 있는지 확인
s3Client.headObject(HeadObjectRequest.builder()
.bucket(bucket)
.key(s3Key)
.build());
// S3에서 파일 가져와 내용 Byte로 저장
GetObjectRequest objectRequest = GetObjectRequest
.builder()
.key(s3Key)
.bucket(bucket)
.build();

ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();

// 경로에 파일 생성
String[] split = s3Key.split("/");
String downloadPathFile = directoriesPath + split[split.length - 1];
File file = new File(downloadPathFile);

// 생성한 파일에 내용 쓰기, 내용을 쓰면 자동으로 실제 생성됨.
// try-with-resources 대신 close() 사용
OutputStream os = new FileOutputStream(file);
if (data == null) {
os.write("".getBytes());
} else {
os.write(data);
}
os.flush();
os.close();
return file;

ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();

// 경로에 파일 생성
String[] split = s3Key.split("/");
String downloadPathFile = directoriesPath + split[split.length - 1];
File file = new File(downloadPathFile);

// 생성한 파일에 내용 쓰기, 내용을 쓰면 자동으로 실제 생성됨.
// try-with-resources 대신 close() 사용
OutputStream os = new FileOutputStream(file);
if(data == null) {
os.write("".getBytes());
} else {
os.write(data);
} catch (SdkClientException | S3Exception e) {
log.error("[runserver][repository] downloadFileFromS3 시 SdkClientException | S3Exception error 발생 = {}",
e.getMessage());
throw new RuntimeException(e);
} catch (IOException e) {
log.error("[runserver][repository] downloadFileFromS3 시 error 발생 = {}", e.getMessage());
throw new RuntimeException(e);
}
os.flush();
os.close();

return file;

}

Expand Down
Loading

0 comments on commit e0d9a44

Please sign in to comment.