실행 오류 수정

This commit is contained in:
2026-02-12 10:58:12 +09:00
parent 6572e17f00
commit c6e721aa37

View File

@@ -9,6 +9,8 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -57,23 +59,58 @@ public class DockerTrainService {
Process p = pb.start(); Process p = pb.start();
// 로그는 별도 스레드에서 읽기 (메인 스레드가 readLine에 안 걸리게) // 로그는 별도 스레드에서 읽기 (메인 스레드가 readLine에 안 걸리게)
StringBuilder log = new StringBuilder(); StringBuilder logBuilder = new StringBuilder();
Pattern epochPattern = Pattern.compile("(?i)\\bepoch\\s*\\[?(\\d+)\\s*/\\s*(\\d+)\\]?\\b");
Thread logThread = Thread logThread =
new Thread( new Thread(
() -> { () -> {
try (BufferedReader br = try (BufferedReader br =
new BufferedReader( new BufferedReader(
new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) { new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
synchronized (log) {
log.append(line).append('\n'); // 1) 원래 하던 로그 누적
synchronized (logBuilder) {
logBuilder.append(line).append('\n');
}
// 2) 🔥 epoch 감지 + DB 업데이트
Matcher m = epochPattern.matcher(line);
if (m.find()) {
int currentEpoch = Integer.parseInt(m.group(1));
int totalEpoch = Integer.parseInt(m.group(2));
log.info("[EPOCH] container={} {}/{}", containerName, currentEpoch, totalEpoch);
// TODO 실행중인 에폭 저장 필요하면 만들어야함
// modelTrainMngCoreService.updateCurrentEpoch(modelId,
// currentEpoch, totalEpoch);
} }
} }
} catch (Exception ignored) { } catch (Exception e) {
log.warn("logThread error: {}", e.toString());
} }
}, },
"train-log-" + containerName); "train-log-" + containerName);
// new Thread(
// () -> {
// try (BufferedReader br =
// new BufferedReader(
// new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
// String line;
// while ((line = br.readLine()) != null) {
// synchronized (log) {
// log.append(line).append('\n');
// }
// }
// } catch (Exception ignored) {
// }
// },
// "train-log-" + containerName);
logThread.setDaemon(true); logThread.setDaemon(true);
logThread.start(); logThread.start();
@@ -90,8 +127,8 @@ public class DockerTrainService {
killContainer(containerName); killContainer(containerName);
String logs; String logs;
synchronized (log) { synchronized (logBuilder) {
logs = log.toString(); logs = logBuilder.toString();
} }
return new TrainRunResult( return new TrainRunResult(
@@ -108,8 +145,8 @@ public class DockerTrainService {
logThread.join(500); logThread.join(500);
String logs; String logs;
synchronized (log) { synchronized (logBuilder) {
logs = log.toString(); logs = logBuilder.toString();
} }
return new TrainRunResult(null, containerName, exit, exit == 0 ? "SUCCESS" : "FAILED", logs); return new TrainRunResult(null, containerName, exit, exit == 0 ? "SUCCESS" : "FAILED", logs);