From bc56cc2ff737150aace78f261a350226ce11a0d2 Mon Sep 17 00:00:00 2001 From: coco3x Date: Wed, 11 Dec 2024 23:01:11 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20ContinuousProcess=EB=A5=BC=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8A=94=20log=20writer=EB=93=A4(mdns,=20mon?= =?UTF-8?q?tior)=EC=97=90=20=ED=94=84=EB=A1=9C=EC=84=B8=EC=8A=A4=20?= =?UTF-8?q?=EB=B3=B5=EA=B5=AC=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../managed/mdns/MdnsLogWriter.java | 31 ++++++++++++------- .../log_writer/monitor/MonitorLogWriter.java | 24 ++++++++++---- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java index ef03b43d..9509b190 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogWriter.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; @@ -17,7 +18,7 @@ @Component public class MdnsLogWriter { private final Map processes; - private final Map wasDead; + private final Map dead; private final MdnsLogParser parser; private final ManagedLogDAO dao; private final String sudoPassword; @@ -27,13 +28,8 @@ public MdnsLogWriter(ManagedLogDAO dao, MdnsLogParser parser, NetworkConfig conf this.parser = parser; this.sudoPassword = sudoPassword; this.processes = new HashMap<>(); - this.wasDead = new HashMap<>(); - config.getMdnsList() - .forEach(managedInfo -> { - this.processes.put(managedInfo, new MdnsLogProcess(managedInfo, sudoPassword)); - log.info("[managed - mdns({})] started", managedInfo.ssid()); - this.wasDead.put(managedInfo, false); - }); + this.dead = new HashMap<>(); + config.getMdnsList().forEach(this::startProcess); } @Scheduled(initialDelay = 10000, fixedDelay = 10000) @@ -43,9 +39,13 @@ private void writeLogs() { ManagedInfo managedInfo = entry.getKey(); MdnsLogProcess process = entry.getValue(); boolean alive = process.isAlive(); - if (!alive && wasDead.get(managedInfo).equals(Boolean.FALSE)) { - wasDead.put(managedInfo, true); - log.error("[managed - mdns({})] dead :\n{}\n{}", managedInfo.ssid(), "에러 스트림 내용:", process.readErrorLines()); + //프로세스가 죽었다는 것을 인지했을때만 아래 로직을 실행 + if (!alive && dead.get(managedInfo).equals(Boolean.FALSE)) { + dead.put(managedInfo, true); + log.error("[managed - mdns({})] 프로세스가 종료됨 :\n{}\n{}", managedInfo.ssid(), "프로세스의 에러 스트림 내용:", process.readErrorLines()); + log.error("[managed - mdns({})] 프로세스를 재실행합니다.", managedInfo.ssid()); + //프로세스 실행은 논블로킹 + new Thread(()->startProcess(managedInfo)).start(); } return alive;}) .flatMap(entry -> { @@ -71,4 +71,13 @@ private void writeLogs() { dao.upsertAll(totalLogs); } + private void startProcess(ManagedInfo info){ + Optional.ofNullable(this.processes.get(info)).ifPresent(MdnsLogProcess::terminate); + MdnsLogProcess process = new MdnsLogProcess(info, sudoPassword); + this.processes.put(info, process); + log.info("[managed - mdns({})] 프로세스 실행 완료", info.ssid()); + this.dead.put(info, false); + } + + } diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java index 314d64fe..9ec8417a 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogWriter.java @@ -1,7 +1,10 @@ package com.whoz_in.log_writer.monitor; import com.whoz_in.log_writer.config.NetworkConfig; +import com.whoz_in.log_writer.managed.ManagedInfo; +import com.whoz_in.log_writer.managed.mdns.MdnsLogProcess; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -12,7 +15,7 @@ @Component public class MonitorLogWriter { private MonitorLogProcess process; //교체될 수 있으므로 final X - private Boolean wasDead = false; + private Boolean dead = false; private final MonitorLogParser parser; private final MonitorLogDAO repo; private final String sudoPassword; @@ -23,15 +26,16 @@ public MonitorLogWriter(MonitorLogParser parser, MonitorLogDAO repo, NetworkConf this.repo = repo; this.monitorInfo = config.getMonitorInfo(); this.sudoPassword = sudoPassword; - this.process = new MonitorLogProcess(monitorInfo, sudoPassword); - log.info("[monitor] started"); + startProcess(); } @Scheduled(initialDelay = 10000, fixedDelay = 10000) private void saveLogs(){ if (!process.isAlive()) { - if (wasDead.equals(Boolean.FALSE)) { - log.error("[monitor] dead :\n{}\n{}", "에러 스트림 내용:", process.readErrorLines()); - wasDead = true; + if (dead.equals(Boolean.FALSE)) { + dead = true; + log.error("[monitor] 프로세스가 종료됨 :\n{}\n{}", "프로세스의 에러 스트림 내용:", process.readErrorLines()); + log.error("[monitor] 프로세스를 재실행합니다."); + startProcess(); } return; } @@ -48,4 +52,12 @@ private void saveLogs(){ repo.upsertAll(macs); } + private void startProcess(){ + Optional.ofNullable(this.process).ifPresent(MonitorLogProcess::terminate); + MonitorLogProcess p = new MonitorLogProcess(monitorInfo, sudoPassword); + this.process = p; + log.info("[monitor] 프로세스 실행 완료"); + this.dead = false; + } + } \ No newline at end of file