Browse Source

新增服务运行状态获取及更新至数据库的方法与实现

zhaojinyu 5 days ago
parent
commit
3a9963b857

+ 6 - 0
base-modules/service-file/pom.xml

@@ -89,6 +89,12 @@
             <scope>runtime</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>1.7.36</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 79 - 0
base-modules/service-file/src/main/java/com/ruoyi/file/service/impl/FileUpdateInfoServiceImpl.java

@@ -12,6 +12,8 @@ import org.apache.commons.codec.digest.DigestUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.*;
 import java.net.HttpURLConnection;
@@ -29,6 +31,7 @@ import java.util.stream.Collectors;
 
 @Service
 public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
+    private static final Logger logger = LoggerFactory.getLogger(FileUpdateInfoServiceImpl.class);
 
     @Autowired
     private FileUpdateInfoMapper fileUpdateInfoMapper;
@@ -197,6 +200,11 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
         Page<FileUpdateInfo> page = new Page<>(current, size);
         IPage<FileUpdateInfo> result = fileUpdateInfoMapper.selectPage(page, null);
 
+        // 更新每个文件的运行状态
+        for (FileUpdateInfo fileUpdateInfo : result.getRecords()) {
+            checkServiceStatus(fileUpdateInfo);
+        }
+
         // 将 MyBatis-Plus 的分页结果转换为 CommonPage
         return new CommonPage<>(
                 result.getRecords(),
@@ -214,6 +222,11 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
         queryWrapper.like("file_name", fileName);
         IPage<FileUpdateInfo> result = fileUpdateInfoMapper.selectPage(page, queryWrapper);
 
+        // 更新每个文件的运行状态
+        for (FileUpdateInfo fileUpdateInfo : result.getRecords()) {
+            checkServiceStatus(fileUpdateInfo);
+        }
+
         // 将 MyBatis-Plus 的分页结果转换为 CommonPage
         return new CommonPage<>(
                 result.getRecords(),
@@ -317,4 +330,70 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
         // 返回分页结果
         return new CommonPage<>(fileWithIds, total, size, current);
     }
+
+
+    // 检测服务运行状态并更新到数据库
+// 检测服务运行状态并更新到数据库
+    private void checkServiceStatus(FileUpdateInfo fileUpdateInfo) {
+        String fileName = fileUpdateInfo.getFileName();
+        int fileStatus = getFileStatus(fileName); // 调用方法获取文件运行状态
+        fileUpdateInfo.setFileStatus(fileStatus); // 更新运行状态
+        fileUpdateInfoMapper.updateById(fileUpdateInfo); // 更新数据库
+    }
+
+    // 获取文件运行状态
+    private int getFileStatus(String fileName) {
+        String osName = System.getProperty("os.name").toLowerCase();
+
+        if (osName.contains("linux") || osName.contains("unix")) {
+            // Linux 系统
+            String command = "pgrep -f " + fileName;
+            try {
+                Process process = Runtime.getRuntime().exec(command);
+                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+                String line;
+                boolean isRunning = false;
+                while ((line = reader.readLine()) != null) {
+                    logger.info("Process output: {}", line);
+                    if (line != null && !line.isEmpty()) {
+                        isRunning = true;
+                        logger.info("Service is running: {}", fileName);
+                        break;
+                    }
+                }
+                if (!isRunning) {
+                    logger.info("Service is not running: {}", fileName);
+                }
+                return isRunning ? 1 : 2; // 1: 运行中,2: 已停止
+            } catch (IOException e) {
+                logger.error("Failed to execute command: {}", command, e);
+            }
+        } else if (osName.contains("windows")) {
+            // Windows 系统
+            String command = "sc query " + fileName;
+            try {
+                Process process = Runtime.getRuntime().exec(command);
+                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+                String line;
+                boolean isRunning = false;
+                while ((line = reader.readLine()) != null) {
+                    logger.info("Service query output: {}", line);
+                    if (line.contains("RUNNING")) {
+                        isRunning = true;
+                        logger.info("Service is running: {}", fileName);
+                        break;
+                    } else if (line.contains("STOPPED")) {
+                        logger.info("Service is stopped: {}", fileName);
+                        break;
+                    }
+                }
+                return isRunning ? 1 : 2; // 1: 运行中,2: 已停止
+            } catch (IOException e) {
+                logger.error("Failed to execute command: {}", command, e);
+            }
+        } else {
+            logger.warn("Unsupported operating system: {}", osName);
+        }
+        return 0; // 未知
+    }
 }