Skip to content

Commit

Permalink
Merge pull request #63 from JNU-econovation/log_process_interface
Browse files Browse the repository at this point in the history
Log process interface
  • Loading branch information
inferior3x authored Nov 29, 2024
2 parents 5d228f3 + 65ba2a5 commit 3f6b3d0
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.whoz_in.log_writer.common.process;

//실행 후 종료되지 않는 프로세스
//꾸준히 출력을 읽을 수 있어야 한다.
public abstract class ContinuousProcess {

//출력이 없을 땐 블로킹되면 안된다.
public abstract String readLine();
public abstract boolean isAlive();
public abstract void terminate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.whoz_in.log_writer.common.process;

import java.util.List;

//실행 후 종료되어 모든 출력을 얻는 프로세스
//출력 스트림을 통한 프로세스와의 상호작용은 없다.
public abstract class TransientProcess {

//종료되었을 때 출력을 얻는다.
//종료되지 않았다면 블로킹된다.
public abstract List<String> results();
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public NetworkConfig(@Value("${spring.profiles.active:default}") String profile,
//monitor
Map<String, String> monitorMap = (Map<String, String>) map.get("monitor");
this.monitorInfo = new MonitorInfo(
monitorMap.get("interface"),
generateCommand(monitorMap.get("command"), monitorMap.get("interface"))
);
generateCommand(monitorMap.get("command"), monitorMap.get("interface")));
// managed
Map<String, Object> managedMap = (Map<String, Object>) map.get("managed");
// mdns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class ManagedLogDAO {

private final JdbcTemplate jdbcTemplate;

public void insertAll(Collection<ManagedLog> logs) {
public void upsertAll(Collection<ManagedLog> logs) {
if (logs.isEmpty()) return;
List<ManagedLog> logList = logs.stream().toList();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whoz_in.log_writer.managed.arp;

import com.whoz_in.log_writer.common.process.TransientProcess;
import com.whoz_in.log_writer.managed.ManagedInfo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand All @@ -10,8 +11,7 @@
import java.util.ArrayList;
import java.util.List;

//단발성 프로세스
public class ArpLogProcess {
public class ArpLogProcess extends TransientProcess {
private final BufferedReader br;
private final Process process;
public ArpLogProcess(ManagedInfo info, String password) {
Expand All @@ -31,8 +31,9 @@ public ArpLogProcess(ManagedInfo info, String password) {
}
}

//단발성 프로세스로, 프로세스가 종료될 때까지 run은 블로킹된다.
public List<String> readLines(){

@Override
public List<String> results(){
List<String> logs = new ArrayList<>();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public ArpLogWriter(ManagedLogDAO dao,
this.sudoPassword = sudoPassword;
}

@Scheduled(initialDelay = 10000, fixedRate = 5000)
@Scheduled(initialDelay = 10000, fixedDelay = 5000)
private void scan() {
List<ManagedLog> logs= arpList.stream()
.flatMap(arpInfo-> {
ArpLogProcess proc = new ArpLogProcess(arpInfo, sudoPassword); //프로세스 실행
List<String> lines = proc.readLines(); //프로세스의 모든 출력 가져오기
List<String> lines = proc.results(); //프로세스의 모든 출력 가져오기
Set<ManagedLog> procLogs = lines.stream() //출력 라인들을 ManagedLog 변환하며 ssid도 넣어줌
.filter(parser::validate)
.map(line->{
Expand All @@ -60,6 +60,6 @@ private void scan() {
})
.toList();

dao.insertAll(logs);
dao.upsertAll(logs);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whoz_in.log_writer.managed.mdns;

import com.whoz_in.log_writer.common.process.ContinuousProcess;
import com.whoz_in.log_writer.common.util.NonBlockingBufferedReader;
import com.whoz_in.log_writer.managed.ManagedInfo;
import java.io.BufferedReader;
Expand All @@ -9,11 +10,10 @@
import java.io.OutputStreamWriter;
import java.io.Writer;

public class MdnsLogProcess {
public class MdnsLogProcess extends ContinuousProcess {
private final Process process;
private final NonBlockingBufferedReader cbr;

//TODO: Info로 변경
public MdnsLogProcess(ManagedInfo info, String sudoPassword) {
try {
new File("../error").mkdir(); //에러 처리 수정하면 이거 없앨게요..
Expand All @@ -33,19 +33,21 @@ public MdnsLogProcess(ManagedInfo info, String sudoPassword) {
* @return 프로세스의 출력에서 한 줄을 읽어들인다.
* 읽을 줄이 없을경우 null을 출력한다.
*/
@Override
public String readLine(){
//여기서 try catch
try {
return this.cbr.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public boolean isAlive(){
return this.process.isAlive();
}

@Override
public void terminate(){
this.process.destroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MdnsLogWriter(ManagedLogDAO dao, MdnsLogParser parser, NetworkConfig conf
));
}

@Scheduled(initialDelay = 10000, fixedRate = 10000)
@Scheduled(initialDelay = 10000, fixedDelay = 10000)
private void writeLogs() {
List<ManagedLog> totalLogs = this.processes.entrySet().parallelStream()
.filter(entry-> {
Expand Down Expand Up @@ -60,7 +60,7 @@ private void writeLogs() {
})
.toList();

dao.insertAll(totalLogs);
dao.upsertAll(totalLogs);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.whoz_in.log_writer.monitor;

//TODO: interfaceName 제거
//Monitor 프로세스를 생성하고 처리하는 과정에서 필요한 정보를 담는다.
public record MonitorInfo(String interfaceName, String command) {}
public record MonitorInfo(String command) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whoz_in.log_writer.monitor;

import com.whoz_in.log_writer.common.process.ContinuousProcess;
import com.whoz_in.log_writer.common.util.NonBlockingBufferedReader;
import java.io.BufferedReader;
import java.io.File;
Expand All @@ -8,7 +9,7 @@
import java.io.OutputStreamWriter;
import java.io.Writer;

public final class MonitorLogProcess {
public final class MonitorLogProcess extends ContinuousProcess {
private final Process process;
private final NonBlockingBufferedReader cbr;

Expand All @@ -28,6 +29,7 @@ public MonitorLogProcess(MonitorInfo info, String sudoPassword) {
}
}

@Override
public String readLine(){
try {
return cbr.readLine();
Expand All @@ -36,11 +38,13 @@ public String readLine(){
}
}

@Override
public boolean isAlive(){
return this.process.isAlive();
}

public void destory(){
@Override
public void terminate(){
this.process.destroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MonitorLogWriter(MonitorLogParser parser, MonitorLogDAO repo, NetworkConf
this.sudoPassword = sudoPassword;
this.process = new MonitorLogProcess(monitorInfo, sudoPassword);
}
@Scheduled(initialDelay = 10000, fixedRate = 10000)
@Scheduled(initialDelay = 10000, fixedDelay = 10000)
private void saveLogs(){
if (!process.isAlive()) {
System.err.println("[monitor] 종료됨 : ERROR");
Expand All @@ -41,7 +41,6 @@ private void saveLogs(){

System.out.println("[monitor] 저장할 mac 개수: " + macs.size());
repo.upsertAll(macs);
macs.clear();
}

}

0 comments on commit 3f6b3d0

Please sign in to comment.