Przeglądaj źródła

一台机器支持不同端口启动同样策略

hl 1 rok temu
rodzic
commit
db6180745c
4 zmienionych plików z 37 dodań i 58 usunięć
  1. 21 17
      src/robot.js
  2. 0 14
      src/utils/config2.json
  3. 2 5
      src/utils/node.json
  4. 14 22
      src/web.js

+ 21 - 17
src/robot.js

@@ -35,6 +35,7 @@ function getAppMap() {
 function getApp(key) {
     let app = {
         id: -1,
+        port: -1,
         childProcess: undefined,
         nowBalance: 0,
         messlist: [],
@@ -73,10 +74,10 @@ async function run(param) {
         }
         app.status = 1
 
-
         // 初始化机器人状态
         app.threadStatus = RobotStatus.START_PENDING
         app.id = key
+        app.port = param.callPort
         robotStatus(app)
 
         /****
@@ -89,11 +90,15 @@ async function run(param) {
         // let configName = "config.toml";        //配置文件
         let configName = "config.json";        //配置文件
         let appPath = "";
-        if (platform === 'win32') {//直接运行相对路径
-            appPath = config.winPath + "/" + app.id
-        } else {//打包为docker 镜像
-            appPath = config.linuxPath + "/" + app.id
-        }
+        //相对路径存放
+        appPath = config.filePath + "/" + app.id
+        //1. 检查目录
+        file.checkPathSync(appPath);
+        // if (platform === 'win32') {//直接运行相对路径
+        //     appPath = config.winPath + "/" + app.id
+        // } else {//打包为docker 镜像
+        //     appPath = config.linuxPath + "/" + app.id
+        // }
 
         /****
          *** 第三步:rust 启动程序检查(下载更新)
@@ -102,8 +107,6 @@ async function run(param) {
         var isDow = false
         var scheduleDow = 0
         var scheduleConfig = 0
-        //1. 检查目录
-        file.checkPathSync(appPath);
         //2、 检查执行程序
         var destination = appPath + "/" + exeName
         if (!file.checkFilePath(destination)) {
@@ -232,7 +235,7 @@ async function run(param) {
         const configPath = appPath + "/" + configName
         logger.info(`文件地址:${exePath}-----${configPath}`);
         const command = exePath
-        const args = ['--config=' + configPath]
+        const args = ['--config=' + configPath, '--port=' + app.port]
         app.childProcess = spawn(command, args)
 
 
@@ -453,7 +456,7 @@ async function closeAppAll() {
                         logger.error(`进程${pid} 杀死失败: ${error}`);
                     }
                     logger.info(`进程${pid} 杀死成功`);
-                    logger.info(`当前app:`, app);
+                    // logger.info(`当前app:`, app);
                     app.threadStatus = RobotStatus.STOP_PENDING
                     robotStatus(app)
                 });
@@ -465,16 +468,16 @@ async function closeAppAll() {
             await delay(1000)
             var str = "";
             appMap.forEach((value, key) => {
-                str += "机器人:"+key+",当前状态:"+value.threadStatus+"\t"
-                if(value.threadStatus !== RobotStatus.STOPPED){
-                    z+=1
+                str += "机器人:" + key + ",当前状态:" + value.threadStatus + "\t"
+                if (value.threadStatus !== RobotStatus.STOPPED) {
+                    z += 1
                 }
             });
             logger.info(str)
-            if(z===0){
+            if (z === 0) {
                 logger.info(`策略全部关闭~~开始关闭node`);
                 break
-            }else{
+            } else {
                 logger.info(`等待关闭~~策略!`);
             }
         }
@@ -535,10 +538,11 @@ function robotStatus(app) {
 
 // 上报-余额
 function robotAmount(app) {
+    var accUrl = "http://127.0.0.1:" + app.port
     //拿到策略余额
     try {
         var config = getRustConfig()
-        http.request_get(`${config.rustUrl}/account`, {...config.headers})
+        http.request_get(`${accUrl}/account`, {...config.headers})
             .then((data) => {
                 var d = JSON.parse(data)
                 //余额有变动上报一次
@@ -574,5 +578,5 @@ function delRobot(app) {
 
 
 module.exports = {
-    run, closeApp, restartApp, delRobot, robotStatus, robotAmount, closeAppAll, appMap, getAppMap, RobotStatus
+    run, closeApp, restartApp, delRobot, robotStatus, robotAmount, closeAppAll, appMap, getApp, getAppMap, RobotStatus
 };

+ 0 - 14
src/utils/config2.json

@@ -1,14 +0,0 @@
-{
-  "rustConfig": {
-    "reportUrl": "http://192.168.1.5:81",
-    "rustUrl": "http://127.0.0.1:5555",
-    "dowAppURL": "http://cc.skyfffire.com/api/report/downloadFile",
-    "dowConfigURL": "http://192.168.1.5:81/report/getRobotConfig",
-    "winPath": "C:/Users/hl/Desktop/node_config",
-    "linuxPath": "/4l/rust",
-    "headers": {
-      "auth": "4L",
-      "token": "r7T$8gBV!f&L@E2+"
-    }
-  }
-}

+ 2 - 5
src/utils/node.json

@@ -1,15 +1,12 @@
 {
   "rustConfig": {
     "baseUrl": "http://cc.skyfffire.com/api",
-    "rustUrl": "http://127.0.0.1:5555",
     "dowAppURL": "/report/downloadFile",
     "dowConfigURL": "/report/getRobotConfig",
-    "winPath": "C:/Users/hl/Desktop/node_config",
-    "linuxPath": "/4l/rust",
+    "filePath":"./rust",
     "headers": {
       "auth": "4L",
       "token": "r7T$8gBV!f&L@E2+"
-    },
-    "logPath": "./logs5555"
+    }
   }
 }

+ 14 - 22
src/web.js

@@ -6,6 +6,7 @@ const file = require('./utils/file')
 const {logger, fileLogger} = require("./utils/logger");
 const {spawn, exec, fork, execSync} = require("child_process");
 const path = require("path");
+const {ka} = require("date-fns/locale");
 
 /*******
  * web
@@ -30,31 +31,22 @@ function init() {
         const param = req.query;
         const n = param.n
         const id = param.id
-
-        //时间
-        // const today = new Date();
-        // const year = today.getFullYear(); // 获取年份
-        // const month = (today.getMonth() + 1).toString().padStart(2, '0');
-        // const day = today.getDate().toString().padStart(2, '0');
-        // // 拼接字符串得到 YYYY-MM-DD 格式
-        // var logName = `${year}-${month}-${day}`;
-        //
-        // var f = getRustConfig().logPath + "/" + logName + ".log"
-        // var array = [];
-        // if (file.checkFilePath(f)) {
-        //     const directoryPath = path.resolve(f);
-        //     array = file.readLastNLines(directoryPath, n)
-        // } else {
-        //     logger.info('日志文件不存在:' + f);
-        // }
+        var app = robot.getApp(id)
 
         let result = [];
-        file.getLastFile(getRustConfig().logPath, 10, (fileNameList) => {
-            logger.info('??:' ,fileNameList);
-            result = file.readLastNLines(getRustConfig().logPath, fileNameList, n);
-            logger.info('?????:' ,result);
+        if (app.port !== -1) {
+            var logPath = "./logs" + app.port
+            logger.info(`日志相对地址:${logPath}`)
+
+            file.getLastFile(logPath, 10, (fileNameList) => {
+                // logger.info('??:', fileNameList);
+                result = file.readLastNLines(logPath, fileNameList, n);
+                // logger.info('?????:', result);
+                res.send({'code': 200, 'data': result, "message": "SUCCESS"});
+            });
+        } else {
             res.send({'code': 200, 'data': result, "message": "SUCCESS"});
-        });
+        }
     });
     app.post('/execute', (req, res) => {
         // 获取发送过来的信息(请求体的数据)