Browse Source

修改下载更新服务实现类,下载更新后自动执行一次重启服务操作

zhaojinyu 2 weeks ago
parent
commit
d7ad1aba84

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

@@ -4,9 +4,7 @@ import com.ruoyi.file.domain.FileUpdateInfo;
 import com.ruoyi.file.service.FileUpdateInfoService;
 import com.usky.common.core.bean.ApiResult;
 import com.usky.common.core.bean.CommonPage;
-import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
@@ -15,16 +13,6 @@ import java.util.List;
 @RequestMapping("/api/update")
 public class FileUpdateInfoController {
 
-    // 从配置文件中读取脚本名称
-    @Value("${file.linuxrestart}")
-    private String linuxContralScript;
-
-    @Value("${file.windowsstop}")
-    private String windowsStopScript;
-
-    @Value("${file.windowsstart}")
-    private String windowsStartScript;
-
     @Autowired
     private FileUpdateInfoService fileUpdateInfoService;
 
@@ -107,42 +95,10 @@ public class FileUpdateInfoController {
                 return ApiResult.error("未找到对应的文件信息,ID: " + id);
             }
             String fileName = fileUpdateInfo.getFileName();
-            controlApplication(fileName, action);
+            fileUpdateInfoService.controlApplication(fileName, action);
             return ApiResult.success("服务控制命令已发送: " + action);
         } catch (Exception e) {
             return ApiResult.error("控制服务失败: " + e.getMessage());
         }
     }
-
-    private void controlApplication(String fileName, String action) throws Exception {
-        String osName = System.getProperty("os.name").toLowerCase();
-
-        if (osName.contains("linux") || osName.contains("unix")) {
-            // Linux 系统
-            String command = "sh " + linuxContralScript + " " + action + " " + fileName;
-            Runtime.getRuntime().exec(command);
-        } else if (osName.contains("windows")) {
-            // Windows 系统
-            String command;
-            switch (action.toLowerCase()) {
-                case "start":
-                    command = windowsStartScript + " " + fileName;
-                    break;
-                case "stop":
-                    command = windowsStopScript + " " + fileName;
-                    break;
-                case "restart":
-                    // Windows 系统没有单独的重启脚本,需要先停止再启动
-                    Runtime.getRuntime().exec(windowsStopScript + " " + fileName);
-                    Thread.sleep(5000); // 等待 5 秒,确保服务停止
-                    Runtime.getRuntime().exec(windowsStartScript + " " + fileName);
-                    return;
-                default:
-                    throw new IllegalArgumentException("不支持的操作类型: " + action);
-            }
-            Runtime.getRuntime().exec(command);
-        } else {
-            throw new Exception("不支持的操作系统: " + osName);
-        }
-    }
 }

+ 4 - 1
base-modules/service-file/src/main/java/com/ruoyi/file/domain/FileUpdateInfo.java

@@ -3,6 +3,7 @@ package com.ruoyi.file.domain;
 import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
 import lombok.Data;
 
 import java.io.File;
@@ -12,9 +13,11 @@ import java.net.URL;
 @TableName("file_update_info")
 public class FileUpdateInfo {
     private Long id;
-    private String fileName; // 文件名(不包含路径)
+    private String fileName; // 文件名
     private String fileMd5;
     private String remoteMd5;
+
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private java.util.Date updateTime;
     private String updateStatus;
 

+ 11 - 1
base-modules/service-file/src/main/java/com/ruoyi/file/service/FileUpdateInfoService.java

@@ -1,21 +1,31 @@
 package com.ruoyi.file.service;
 
 import com.ruoyi.file.domain.FileUpdateInfo;
-
 import java.util.List;
 
 public interface FileUpdateInfoService {
+
+    //添加服务信息
     void addFileToDatabase(FileUpdateInfo fileUpdateInfo) throws Exception;
 
+    //检查服务更新
     boolean checkFileUpdate(Long id) throws Exception;
 
+    //执行服务更新
     void performFileUpdate(Long id) throws Exception;
 
+    //获取所有服务信息
     List<FileUpdateInfo> getAllFiles() throws Exception;
 
+    //根据文件名模糊查询服务信息
     List<FileUpdateInfo> getFilesByFileNameContaining(String fileName) throws Exception;
 
+    //根据id查询服务信息
     FileUpdateInfo getFileById(Long id) throws Exception;
 
+    //删除服务信息
     void deleteFileById(Long id) throws Exception;
+
+    //控制服务运行状态
+    void controlApplication(String fileName, String action) throws Exception;
 }

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

@@ -28,6 +28,16 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
     @Value("${file.remote-url}")
     private String remoteUrlBase;
 
+    // 从配置文件中读取脚本名称
+    @Value("${file.linuxrestart}")
+    private String linuxContralScript;
+
+    @Value("${file.windowsstop}")
+    private String windowsStopScript;
+
+    @Value("${file.windowsstart}")
+    private String windowsStartScript;
+
     @Override
     public void addFileToDatabase(FileUpdateInfo fileUpdateInfo) throws Exception {
         String filePath = fileUpdateInfo.getFilePath(); // 使用新的 getFilePath 方法
@@ -127,6 +137,10 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
             fileUpdateInfo.setVersion("v" + major + "." + minor + "." + patch);
 
             fileUpdateInfoMapper.updateById(fileUpdateInfo);
+
+            // 下载完成后重启服务
+            controlApplication(fileName, "restart");
+
         } catch (FileNotFoundException e) {
             throw new Exception("文件未找到: " + localFilePath + ",原因: " + e.getMessage(), e);
         } catch (IOException e) {
@@ -181,4 +195,37 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
     public void deleteFileById(Long id) {
         fileUpdateInfoMapper.deleteById(id);
     }
+
+    @Override
+    public void controlApplication(String fileName, String action) throws Exception {
+        String osName = System.getProperty("os.name").toLowerCase();
+
+        if (osName.contains("linux") || osName.contains("unix")) {
+            // Linux 系统
+            String command = "sh " + linuxContralScript + " " + action + " " + fileName;
+            Runtime.getRuntime().exec(command);
+        } else if (osName.contains("windows")) {
+            // Windows 系统
+            String command;
+            switch (action.toLowerCase()) {
+                case "start":
+                    command = windowsStartScript + " " + fileName;
+                    break;
+                case "stop":
+                    command = windowsStopScript + " " + fileName;
+                    break;
+                case "restart":
+                    // Windows 系统没有单独的重启脚本,需要先停止再启动
+                    Runtime.getRuntime().exec(windowsStopScript + " " + fileName);
+                    Thread.sleep(5000); // 等待 5 秒,确保服务停止
+                    Runtime.getRuntime().exec(windowsStartScript + " " + fileName);
+                    return;
+                default:
+                    throw new IllegalArgumentException("不支持的操作类型: " + action);
+            }
+            Runtime.getRuntime().exec(command);
+        } else {
+            throw new Exception("不支持的操作系统: " + osName);
+        }
+    }
 }