From 01a22425f7c08eaf9ad5d388120f92dc5d7285fd Mon Sep 17 00:00:00 2001 From: filesite Date: Sun, 7 Apr 2024 22:05:47 +0800 Subject: [PATCH] api function update --- conf/config.json | 2 +- heroUnion.mjs | 29 ++++++++++++++++++++++++++--- router_api.mjs | 22 ++++++++++++++++++---- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/conf/config.json b/conf/config.json index 1cf4750..e8c8623 100644 --- a/conf/config.json +++ b/conf/config.json @@ -1,6 +1,6 @@ { "name": "Hero Union", - "version": "0.0.1", + "version": "0.1", "tokens": { "demo": "hello#world!" } diff --git a/heroUnion.mjs b/heroUnion.mjs index ea8c315..81c2c82 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -306,13 +306,12 @@ class HeroUnion { //定期检查爬虫是否在线 //如果上一次上报状态时间在10分钟前,则设置该爬虫已下线 - beroHeartCheck() { + heroHeartCheck() { let _self = this; const frequence = 60; //1 分钟检查一次 const cronjob = cron.schedule(`*/${frequence} * * * * *`, () => { let timestamp = common.getTimestampInSeconds(); - _self.heros.forEach(function(item, index) { if (item.status != 'offline' && timestamp - item.timestamp > _self.heroHeartTimeout) { _self.heroStatus[item.status] --; @@ -327,10 +326,27 @@ class HeroUnion { }); 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.heroStatus = this.heroStatus; this.stats.run_seconds = common.getTimestampInSeconds() - this.stats.start_time; @@ -338,6 +354,13 @@ class HeroUnion { return this.stats; } + //初始化 + init() { + this.getConfig(); + this.autoReloadConfigs(); + this.heroHeartCheck(); + } + } diff --git a/router_api.mjs b/router_api.mjs index 027e32c..ed0f4c2 100644 --- a/router_api.mjs +++ b/router_api.mjs @@ -3,13 +3,28 @@ */ import express from 'express'; +import common from './common.mjs'; +import HeroUnion from './heroUnion.mjs'; + +//初始化爬虫联盟 +const heroUnion = new HeroUnion(); +heroUnion.init(); const router = express.Router(); router.get('/', async (req, res) => { - - return res.send('api/'); + const apiList = { + "/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) => { @@ -38,8 +53,7 @@ router.post('/onboard/', async (req, res) => { }); router.get('/stats/', async (req, res) => { - - return res.send('api/stats/'); + return res.status(200).json(heroUnion.getStats()); }); export default router;