Browse Source

调用更新服务运行状态接口时等待查询是否成功

zhaojinyu 5 days ago
parent
commit
bc63e33e22

+ 18 - 1
base-modules/service-file/src/main/java/com/ruoyi/file/controller/FileUpdateInfoController.java

@@ -88,8 +88,25 @@ public class FileUpdateInfoController {
                 return ApiResult.error("未找到对应的文件信息,ID: " + id);
             }
             String fileName = fileUpdateInfo.getFileName();
+
+            // 在控制服务之前,先检测当前服务状态
+            int currentStatus = fileUpdateInfoService.getFileStatus(fileName);
+            if ((action.equals("start") && currentStatus == 1) || (action.equals("stop") && currentStatus == 2)) {
+                return ApiResult.error("服务已经是目标状态,无需重复操作");
+            }
+
             fileUpdateInfoService.controlApplication(fileName, action);
-            return ApiResult.success(action + "命令已发送");
+
+            // 根据 action 返回不同的成功消息
+            if ("start".equalsIgnoreCase(action)) {
+                return ApiResult.success("启动成功");
+            } else if ("stop".equalsIgnoreCase(action)) {
+                return ApiResult.success("停止成功");
+            } else if ("restart".equalsIgnoreCase(action)) {
+                return ApiResult.success("重启成功");
+            } else {
+                return ApiResult.success(action + "命令已发送");
+            }
         } catch (Exception e) {
             return ApiResult.error("控制服务失败: " + e.getMessage());
         }

+ 7 - 0
base-modules/service-file/src/main/java/com/ruoyi/file/service/FileUpdateInfoService.java

@@ -7,6 +7,13 @@ import org.springframework.web.bind.annotation.RequestParam;
 
 public interface FileUpdateInfoService {
 
+    /**
+     * 获取文件的运行状态
+     * @param fileName 文件名
+     * @return 状态码(1: 运行中,2: 已停止,0: 未知)
+     */
+    int getFileStatus(String fileName);
+
     //添加服务信息
     void addFileToDatabase(FileUpdateInfo fileUpdateInfo) throws Exception;
 

+ 28 - 11
base-modules/service-file/src/main/java/com/ruoyi/file/service/impl/FileUpdateInfoServiceImpl.java

@@ -267,7 +267,7 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
                 case "restart":
                     // Windows 系统没有单独的重启脚本,需要先停止再启动
                     Runtime.getRuntime().exec(windowsStopScript + " " + fileName);
-                    Thread.sleep(5000); // 等待 5 秒,确保服务停止
+                    Thread.sleep(3000); // 等待 3 秒,确保服务停止
                     Runtime.getRuntime().exec(windowsStartScript + " " + fileName);
                     return;
                 default:
@@ -277,6 +277,31 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
         } else {
             throw new Exception("不支持的操作系统: " + osName);
         }
+
+        // 等待操作完成
+        long startTime = System.currentTimeMillis();
+        long timeout = 8000; // 最长等待时间 8 秒
+        boolean isActionCompleted = false;
+
+        while (System.currentTimeMillis() - startTime < timeout) {
+            int currentStatus = getFileStatus(fileName); // 获取当前服务状态
+            if (action.equals("start") && currentStatus == 1) {
+                isActionCompleted = true;
+                break; // 服务已启动
+            } else if (action.equals("stop") && currentStatus == 2) {
+                isActionCompleted = true;
+                break; // 服务已停止
+            } else if (action.equals("restart") && currentStatus == 1) {
+                isActionCompleted = true;
+                break; // 服务已重启
+            }
+
+            Thread.sleep(500); // 每隔 500 毫秒检测一次状态
+        }
+
+        if (!isActionCompleted) {
+            throw new Exception("操作超时,服务状态未达到预期");
+        }
     }
 
     //扫描服务所在目录下的文件
@@ -333,7 +358,6 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
 
 
     // 检测服务运行状态并更新到数据库
-// 检测服务运行状态并更新到数据库
     private void checkServiceStatus(FileUpdateInfo fileUpdateInfo) {
         String fileName = fileUpdateInfo.getFileName();
         int fileStatus = getFileStatus(fileName); // 调用方法获取文件运行状态
@@ -342,7 +366,8 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
     }
 
     // 获取文件运行状态
-    private int getFileStatus(String fileName) {
+    @Override
+    public int getFileStatus(String fileName) {
         String osName = System.getProperty("os.name").toLowerCase();
 
         if (osName.contains("linux") || osName.contains("unix")) {
@@ -354,16 +379,11 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
                 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);
@@ -377,13 +397,10 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
                 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;
                     }
                 }