From 55d43d6d44e24932a401db91258c8cb66952fa9b Mon Sep 17 00:00:00 2001 From: filesite Date: Sun, 7 Apr 2024 20:12:14 +0800 Subject: [PATCH] function beroHeartCheck ready --- common.mjs | 8 ++++++++ heroUnion.mjs | 36 +++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/common.mjs b/common.mjs index 4b7e3c0..2d5963e 100644 --- a/common.mjs +++ b/common.mjs @@ -14,6 +14,14 @@ class Common { this.configDir = resolve('conf/'); } + getTimestamp() { + return Math.floor(Date.now()); + } + + getTimestampInSeconds() { + return Math.floor(Date.now() / 1000); + } + sortDict(obj) { //dict按key排序 return Object.keys(obj).sort().reduce(function (result, key) { result[key] = obj[key]; diff --git a/heroUnion.mjs b/heroUnion.mjs index 5e10da7..d9e1ddc 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -73,15 +73,6 @@ class HeroUnion { }; } - //公用方法 - getTimestamp() { - return Math.floor(Date.now()); - } - - getTimestampInSeconds() { - return Math.floor(Date.now() / 1000); - } - isDataTooLarge(data) { return JSON.stringify(data).length > this.task_data_max_size * 1024; } @@ -98,7 +89,7 @@ class HeroUnion { //根据任务提交者ID和时间戳生成任务ID编号 generateTaskId(uuid) { - let timestamp = this.getTimestamp(); + let timestamp = common.getTimestamp(); return `${uuid}_${timestamp}`; } @@ -125,7 +116,7 @@ class HeroUnion { * } **/ createTask(uuid, url, platform, data_mode, notify_url, country, lang) { - let timestamp = this.getTimestampInSeconds(); + let timestamp = common.getTimestampInSeconds(); let task = { id: this.generateTaskId(uuid), @@ -220,7 +211,7 @@ class HeroUnion { this.tasks[taskIndex].results[resIndex] = data; } - this.tasks[taskIndex].updated = this.getTimestampInSeconds(); + this.tasks[taskIndex].updated = common.getTimestampInSeconds(); this.tasks[taskIndex].status = 'done'; done = true; @@ -252,7 +243,7 @@ class HeroUnion { let params = { "task_id": task.id, "task_result": task.results, - "timestamp": this.getTimestamp(), + "timestamp": common.getTimestamp(), }; let token = await this.getUserToken(task.uuid); params.sign = common.sign(params, token); @@ -314,7 +305,26 @@ class HeroUnion { //定期检查爬虫是否在线 //如果上一次上报状态时间在10分钟前,则设置该爬虫已下线 beroHeartCheck() { + 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] --; + + _self.heros[index].status = 'offline'; + _self.heroStatus.offline ++; + console.log('Hero %s is offline, its last heart beat is %s', item.name, item.timestamp); + } + }); + }, { + scheduled: false + }); + cronjob.start(); } //获取联盟状态