Browse Source

每6小时重启一次项目。

tom 1 month ago
parent
commit
bec1cae445

+ 23 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/CmdController.java

@@ -0,0 +1,23 @@
+package com.ruoyi.sim.controller;
+
+import com.ruoyi.sim.service.impl.CmdService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/sim/cmd/")
+public class CmdController {
+
+    @Autowired
+    private CmdService cmdService;
+
+    /**
+     *
+     */
+    @GetMapping(value = "/restart")
+    public void restart() {
+        cmdService.restart();
+    }
+}

+ 41 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CmdService.java

@@ -0,0 +1,41 @@
+package com.ruoyi.sim.service.impl;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+@Service
+public class CmdService {
+
+    private static final Logger l = LoggerFactory.getLogger(CmdService.class);
+
+    /**
+     * java-service [项目名称] [start|stop|restart]
+     * java-service ruoyi-admin restart
+     */
+    public void restart() {
+        // 定义要执行的命令
+        String command = "java-service ruoyi-admin restart";
+        // 使用 ProcessBuilder 执行命令
+        ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
+        try {
+            // 启动进程
+            Process process = processBuilder.start();
+            // 获取命令的输出
+            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+            String line;
+            while ((line = reader.readLine()) != null) {
+                l.info("line = {}", line);
+            }
+            // 等待命令执行完成
+            int exitCode = process.waitFor();
+            l.info("命令执行完成,退出码:" + exitCode);
+        } catch (InterruptedException | IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 10 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommSendService.java

@@ -62,6 +62,8 @@ public class CommSendService {
     private SimConfig config;
     @Autowired
     private CommStrategy commStrategy;
+    @Autowired
+    private CmdService cmdService;
 
     /**
      * 定时任务。
@@ -311,6 +313,14 @@ public class CommSendService {
     }
 
     /**
+     * 每6hour运行一次。
+     */
+    public void scheduledProjectRestart() {
+        l.info("scheduledProjectRestart");
+        cmdService.restart();
+    }
+
+    /**
      * 主动更新模拟器状态。
      * <p>
      * <p>