Quellcode durchsuchen

添加 MachineProcController 接口展示机器监听的进程

reghao vor 3 Monaten
Ursprung
Commit
c3a1f3bf2a

+ 41 - 0
web/src/main/java/cn/reghao/bnt/web/devops/machine/controller/MachineProcController.java

@@ -0,0 +1,41 @@
+package cn.reghao.bnt.web.devops.machine.controller;
+
+import cn.reghao.bnt.web.devops.machine.model.po.MachineProc;
+import cn.reghao.bnt.web.devops.machine.service.MachineProcService;
+import cn.reghao.jutil.web.WebResult;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-12-17 15:49:01
+ */
+@Slf4j
+@Tag(name = "机器进程接口")
+@RestController
+@RequestMapping("/api/devops/machine/proc")
+public class MachineProcController {
+    private final MachineProcService machineProcService;
+
+    public MachineProcController(MachineProcService machineProcService) {
+        this.machineProcService = machineProcService;
+    }
+
+    @Operation(summary = "机器进程列表", description = "N")
+    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String machineListPage(@RequestParam(value = "machineId") String machineId) {
+        List<MachineProc> machineProcList = machineProcService.getByMachineId(machineId);
+        return WebResult.success(machineProcList);
+    }
+    
+    @Operation(summary = "", description = "N")
+    @PostMapping(value = "/deprecate", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String setMachineDeprecate(String machineId) {
+        return WebResult.success();
+    }
+}

+ 16 - 0
web/src/main/java/cn/reghao/bnt/web/devops/machine/db/repository/MachineProcRepository.java

@@ -0,0 +1,16 @@
+package cn.reghao.bnt.web.devops.machine.db.repository;
+
+import cn.reghao.bnt.web.devops.machine.model.po.MachineProc;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-12-17 15:47:12
+ */
+public interface MachineProcRepository extends JpaRepository<MachineProc, Integer>, JpaSpecificationExecutor<MachineProc> {
+    List<MachineProc> findByMachineId(String machineId);
+    MachineProc findByMachineIdAndBindAddress(String machineId, String bindAddress);
+}

+ 61 - 0
web/src/main/java/cn/reghao/bnt/web/devops/machine/model/po/MachineProc.java

@@ -0,0 +1,61 @@
+package cn.reghao.bnt.web.devops.machine.model.po;
+
+import cn.reghao.bnt.common.machine.model.SysProcess;
+import cn.reghao.bnt.web.util.BaseEntity;
+import jakarta.persistence.CollectionTable;
+import jakarta.persistence.ElementCollection;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Table;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import org.hibernate.annotations.LazyCollection;
+import org.hibernate.annotations.LazyCollectionOption;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-12-17 15:58:08
+ */
+@NoArgsConstructor
+@Getter
+@Setter
+@Entity
+@Table(name = "devops_machine_proc")
+public class MachineProc extends BaseEntity {
+    private String machineId;
+    private Integer pid;
+    private String name;
+    private Integer ppid;
+    private String cmdLine;
+    private String bindAddress;
+    private Long startTime;
+    private String user;
+    private String containerId;
+    private String appId;
+
+    public MachineProc(String machineId, SysProcess sysProcess, String bindAddress) {
+        this.machineId = machineId;
+        this.pid = sysProcess.getPid();
+        this.name = sysProcess.getName();
+        this.ppid = sysProcess.getPpid();
+        this.cmdLine = sysProcess.getCmdLine();
+        this.bindAddress = bindAddress;
+        this.startTime = sysProcess.getStartTime();
+        this.user = sysProcess.getUser();
+        this.containerId = sysProcess.getContainerId();
+        this.appId = sysProcess.getAppId();
+    }
+
+    public void update(SysProcess sysProcess) {
+        this.pid = sysProcess.getPid();
+        this.name = sysProcess.getName();
+        this.ppid = sysProcess.getPpid();
+        this.cmdLine = sysProcess.getCmdLine();
+        this.startTime = sysProcess.getStartTime();
+        this.user = sysProcess.getUser();
+        this.containerId = sysProcess.getContainerId();
+        this.appId = sysProcess.getAppId();
+    }
+}

+ 49 - 0
web/src/main/java/cn/reghao/bnt/web/devops/machine/service/MachineProcService.java

@@ -0,0 +1,49 @@
+package cn.reghao.bnt.web.devops.machine.service;
+
+import cn.reghao.bnt.common.machine.model.SysProcess;
+import cn.reghao.bnt.web.devops.machine.db.repository.MachineProcRepository;
+import cn.reghao.bnt.web.devops.machine.model.po.MachineProc;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2025-12-17 15:47:57
+ */
+@Service
+public class MachineProcService {
+    private final MachineProcRepository machineProcRepository;
+
+    public MachineProcService(MachineProcRepository machineProcRepository) {
+        this.machineProcRepository = machineProcRepository;
+    }
+
+    public void saveAll(String machineId, List<SysProcess> listenProcessList) {
+        List<MachineProc> machineProcList1 = machineProcRepository.findByMachineId(machineId);
+        List<MachineProc> machineProcList = listenProcessList.stream()
+                .map(sysProcess -> {
+                    StringBuilder sb = new StringBuilder();
+                    for (String line : sysProcess.getHostPorts()) {
+                        sb.append(line).append(",");
+                    }
+                    sb.deleteCharAt(sb.length() - 1);
+                    String bindAddress = sb.toString();
+
+                    MachineProc machineProcEntity = machineProcRepository.findByMachineIdAndBindAddress(machineId, bindAddress);
+                    if (machineProcEntity != null) {
+                        machineProcEntity.update(sysProcess);
+                        return machineProcEntity;
+                    } else {
+                        return new MachineProc(machineId, sysProcess, bindAddress);
+                    }
+                })
+                .collect(Collectors.toList());
+        machineProcRepository.saveAll(machineProcList);
+    }
+
+    public List<MachineProc> getByMachineId(String machineId) {
+        return machineProcRepository.findByMachineId(machineId);
+    }
+}

+ 3 - 2
web/src/main/java/cn/reghao/bnt/web/ws/EventDispatcherConfig.java

@@ -2,6 +2,7 @@ package cn.reghao.bnt.web.ws;
 
 import cn.reghao.bnt.common.msg.event.*;
 import cn.reghao.bnt.web.devops.app.service.AppDeployService;
+import cn.reghao.bnt.web.devops.machine.service.MachineProcService;
 import cn.reghao.bnt.web.devops.machine.service.MachineService;
 import cn.reghao.bnt.web.devops.machine.service.MachineTaskService;
 import cn.reghao.bnt.web.ws.event.*;
@@ -17,14 +18,14 @@ import org.springframework.context.annotation.Configuration;
 public class EventDispatcherConfig {
     @Bean
     public EventDispatcher eventDispatcher(MachineService machineService, AppDeployService appDeployService,
-                                           MachineTaskService machineTaskService,
+                                           MachineTaskService machineTaskService, MachineProcService machineProcService,
                                            SessionManagerFront sessionManagerFront) {
         EventDispatcher dispatcher = new EventDispatcher();
         dispatcher.register(EvtAgentStart.class, new EvtAgentStartHandler(machineService));
         dispatcher.register(EvtAgentHeartbeat.class, new EvtAgentHeartbeatHandler(machineService));
 
         dispatcher.register(EvtAppStatResult.class, new EvtAppStatResultHandler(appDeployService));
-        dispatcher.register(EvtTaskResult.class, new EvtTaskResultHandler(machineTaskService));
+        dispatcher.register(EvtTaskResult.class, new EvtTaskResultHandler(machineTaskService, machineProcService));
         dispatcher.register(EvtDockerOpsResult.class, new EvtDockerOpsResultHandler(sessionManagerFront));
         return dispatcher;
     }

+ 16 - 2
web/src/main/java/cn/reghao/bnt/web/ws/event/EvtTaskResultHandler.java

@@ -1,24 +1,38 @@
 package cn.reghao.bnt.web.ws.event;
 
+import cn.reghao.bnt.common.machine.model.SysProcess;
 import cn.reghao.bnt.common.msg.event.EvtTaskResult;
+import cn.reghao.bnt.web.devops.machine.model.po.MachineProc;
+import cn.reghao.bnt.web.devops.machine.service.MachineProcService;
 import cn.reghao.bnt.web.devops.machine.service.MachineTaskService;
 import cn.reghao.jutil.jdk.event.handler.Handler;
 import cn.reghao.jutil.jdk.event.message.Event;
 
+import java.util.List;
+import java.util.stream.Collectors;
+
 /**
  * @author reghao
  * @date 2025-12-16 10:53:25
  */
 public class EvtTaskResultHandler extends Handler {
     private final MachineTaskService machineTaskService;
+    private final MachineProcService machineProcService;
 
-    public EvtTaskResultHandler(MachineTaskService machineTaskService) {
+    public EvtTaskResultHandler(MachineTaskService machineTaskService, MachineProcService machineProcService) {
         this.machineTaskService = machineTaskService;
+        this.machineProcService = machineProcService;
     }
 
     @Override
     public void handle(Event evt) {
         EvtTaskResult evtTaskResult = (EvtTaskResult) evt;
-        machineTaskService.handleTaskResult(evtTaskResult);
+        int resultType = evtTaskResult.getResultType();
+        if (resultType == 1) {
+            machineTaskService.handleTaskResult(evtTaskResult);
+        } else if (resultType == 2) {
+            String machineId = evtTaskResult.getMachineId();
+            machineProcService.saveAll(machineId, evtTaskResult.getListenProcessList());
+        }
     }
 }