Browse Source

api function update

master
filesite 8 months ago
parent
commit
01a22425f7
  1. 2
      conf/config.json
  2. 29
      heroUnion.mjs
  3. 22
      router_api.mjs

2
conf/config.json

@ -1,6 +1,6 @@
{ {
"name": "Hero Union", "name": "Hero Union",
"version": "0.0.1", "version": "0.1",
"tokens": { "tokens": {
"demo": "hello#world!" "demo": "hello#world!"
} }

29
heroUnion.mjs

@ -306,13 +306,12 @@ class HeroUnion {
//定期检查爬虫是否在线 //定期检查爬虫是否在线
//如果上一次上报状态时间在10分钟前,则设置该爬虫已下线 //如果上一次上报状态时间在10分钟前,则设置该爬虫已下线
beroHeartCheck() { heroHeartCheck() {
let _self = this; let _self = this;
const frequence = 60; //1 分钟检查一次 const frequence = 60; //1 分钟检查一次
const cronjob = cron.schedule(`*/${frequence} * * * * *`, () => { const cronjob = cron.schedule(`*/${frequence} * * * * *`, () => {
let timestamp = common.getTimestampInSeconds(); let timestamp = common.getTimestampInSeconds();
_self.heros.forEach(function(item, index) { _self.heros.forEach(function(item, index) {
if (item.status != 'offline' && timestamp - item.timestamp > _self.heroHeartTimeout) { if (item.status != 'offline' && timestamp - item.timestamp > _self.heroHeartTimeout) {
_self.heroStatus[item.status] --; _self.heroStatus[item.status] --;
@ -327,10 +326,27 @@ class HeroUnion {
}); });
cronjob.start(); cronjob.start();
console.log('Cronjob of hero heart check started.');
}
//自动重新加载配置文件
autoReloadConfigs() {
let _self = this;
const frequence = 300; //5 分钟重新加载一次
const cronjob = cron.schedule(`*/${frequence} * * * * *`, () => {
const forceReload = true;
_self.getConfig(forceReload);
}, {
scheduled: false
});
cronjob.start();
console.log('Cronjob of config auto reload started.');
} }
//获取联盟状态 //获取联盟状态
stats() { getStats() {
this.stats.taskStatus = this.taskStatus; this.stats.taskStatus = this.taskStatus;
this.stats.heroStatus = this.heroStatus; this.stats.heroStatus = this.heroStatus;
this.stats.run_seconds = common.getTimestampInSeconds() - this.stats.start_time; this.stats.run_seconds = common.getTimestampInSeconds() - this.stats.start_time;
@ -338,6 +354,13 @@ class HeroUnion {
return this.stats; return this.stats;
} }
//初始化
init() {
this.getConfig();
this.autoReloadConfigs();
this.heroHeartCheck();
}
} }

22
router_api.mjs

@ -3,13 +3,28 @@
*/ */
import express from 'express'; import express from 'express';
import common from './common.mjs';
import HeroUnion from './heroUnion.mjs';
//初始化爬虫联盟
const heroUnion = new HeroUnion();
heroUnion.init();
const router = express.Router(); const router = express.Router();
router.get('/', async (req, res) => { router.get('/', async (req, res) => {
const apiList = {
return res.send('api/'); "/api/": "查看所有API",
"/api/stats/": "查看联盟状态",
};
const data = {
name: heroUnion.config.name,
version: heroUnion.config.version,
apis: apiList
};
return res.status(200).json(data);
}); });
router.post('/newtask/', async (req, res) => { router.post('/newtask/', async (req, res) => {
@ -38,8 +53,7 @@ router.post('/onboard/', async (req, res) => {
}); });
router.get('/stats/', async (req, res) => { router.get('/stats/', async (req, res) => {
return res.status(200).json(heroUnion.getStats());
return res.send('api/stats/');
}); });
export default router; export default router;

Loading…
Cancel
Save