Skip to content

Commit

Permalink
feat: ContinuousProcess를 관리하는 log writer들(mdns, montior)에 프로세스 복구 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
inferior3x committed Dec 11, 2024
1 parent d8418a4 commit bc56cc2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +18,7 @@
@Component
public class MdnsLogWriter {
private final Map<ManagedInfo, MdnsLogProcess> processes;
private final Map<ManagedInfo, Boolean> wasDead;
private final Map<ManagedInfo, Boolean> dead;
private final MdnsLogParser parser;
private final ManagedLogDAO dao;
private final String sudoPassword;
Expand All @@ -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)
Expand All @@ -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 -> {
Expand All @@ -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);
}


}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

}

0 comments on commit bc56cc2

Please sign in to comment.