lrf402788946 4 سال پیش
والد
کامیت
6d6ee00a76
5فایلهای تغییر یافته به همراه18 افزوده شده و 43 حذف شده
  1. 1 1
      app/schedule/creeperxtsqnode.js
  2. 0 23
      app/schedule/noderead.js
  3. 13 11
      app/service/creeperxtsqnode.js
  4. 3 7
      app/service/creeperxtsread.js
  5. 1 1
      config/config.default.js

+ 1 - 1
app/schedule/creeperxtsqnode.js

@@ -9,7 +9,7 @@ class Creeper extends Subscription {
     return {
       // cron: '0 6 30 * * ?', // 每天晚上23点执行任务
       // // cron: '0 20 12 * * ?', // 每天晚上23点执行任务
-      interval: '2m', // 1分钟执行一次
+      interval: '1m', // 1分钟执行一次
       type: 'worker', // 指定所有的 worker 都需要执行
     };
   }

+ 0 - 23
app/schedule/noderead.js

@@ -1,23 +0,0 @@
-'use strict';
-
-const Subscription = require('egg').Subscription;
-
-class Creeper extends Subscription {
-  // 通过 schedule 属性来设置定时任务的执行间隔等配置
-  // 更改执行时间
-  static get schedule() {
-    return {
-      // cron: '0 6 30 * * ?', // 每天晚上23点执行任务
-      // // cron: '0 20 12 * * ?', // 每天晚上23点执行任务
-      interval: '1m', // 1分钟执行一次
-      type: 'worker', // 指定所有的 worker 都需要执行
-    };
-  }
-
-  // subscribe 是真正定时任务执行时被运行的函数
-  async subscribe() {
-    console.log('服务器集群节点读取');
-    await this.ctx.service.creeperxtsread.qnodes(); // qnodes
-  }
-}
-module.exports = Creeper;

+ 13 - 11
app/service/creeperxtsqnode.js

@@ -38,17 +38,19 @@ class CreeperxtsService extends CrudService {
         // console.log(`sessionText=>${sessionText}`);
         // console.log(`sshObj=>${JSON.stringify(sshObj)}`);
         const path_ = `${pathDir}/qnodes.txt`;
-        fs.access(path_, err => {
-          if (err) {
-            fs.writeFile(path_, sessionText, function(err) {
-              if (err) throw err;
-            });
-          } else {
-            fs.appendFile(path_, sessionText, function(err) {
-              if (err) throw err;
-            });
-          }
-        });
+        const writeRes = fs.writeFileSync(path_, sessionText);
+        this.ctx.service.creeperxtsread.qnodes();
+        // fs.access(path_, err => {
+        //   if (err) {
+        //     fs.writeFile(path_, sessionText, function(err) {
+        //       if (err) throw err;
+        //     });
+        //   } else {
+        //     fs.appendFile(path_, sessionText, function(err) {
+        //       if (err) throw err;
+        //     });
+        //   }
+        // });
       },
     };
     const SSH = new SSH2Shell(host);

+ 3 - 7
app/service/creeperxtsread.js

@@ -78,8 +78,8 @@ class CreeperxtsreadService extends CrudService {
         }
       }
     }
-    this.toMq(arr);
-    fs.unlinkSync(`${pathread}`);
+    await this.toMq(arr);
+    // await fs.unlinkSync(`${pathread}`);
   }
 
   async readFileToArr(path_) {
@@ -93,15 +93,11 @@ class CreeperxtsreadService extends CrudService {
   }
 
   async toMq(data) {
-    console.log('in toMq');
-    console.log(`data:${data.lenght}`);
     if (this.ctx.mq) {
       const exchange = 'service_count';
       const routerKey = 'nodes';
       const parm = { durable: true };
-      console.log(`exchange:${exchange}`);
-      console.log(`routerKey:${routerKey}`);
-      await this.mq.fanout(exchange, routerKey, JSON.stringify(data), parm);
+      await this.ctx.mq.fanout(exchange, routerKey, JSON.stringify(data), parm);
     }
   }
 }

+ 1 - 1
config/config.default.js

@@ -38,7 +38,7 @@ module.exports = appInfo => {
       hostname: '127.0.0.1',
       username: 'visit',
       password: 'visit',
-      vhost: 'train',
+      vhost: 'visit',
     },
     app: true,
     agent: true,