|
|
|
@ -20,7 +20,9 @@ import HeroBot from "./heroBot.mjs";
@@ -20,7 +20,9 @@ import HeroBot from "./heroBot.mjs";
|
|
|
|
|
class TaskMoniter { |
|
|
|
|
constructor(task_list_dir) { |
|
|
|
|
this.check_time_gap = 1; //检测间隔时间,单位:分钟
|
|
|
|
|
this.notify_time_gap = 5; //数据回调间隔时间,单位:分钟
|
|
|
|
|
this.checking = false; |
|
|
|
|
this.notifying = false; |
|
|
|
|
|
|
|
|
|
this.task_dir = task_list_dir; //监控目录:任务列表保存目录
|
|
|
|
|
this.tasks = {}; //内存中的任务列表
|
|
|
|
@ -150,6 +152,17 @@ class TaskMoniter {
@@ -150,6 +152,17 @@ class TaskMoniter {
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateTask(task_id, task) { |
|
|
|
|
if (typeof(this.tasks[task.id]) == 'undefined') { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.tasks[task.id] = task; |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//检查新的数据抓取任务
|
|
|
|
|
async checkTasks() { |
|
|
|
|
if (this.checking == true) { |
|
|
|
|
return; |
|
|
|
@ -191,6 +204,49 @@ class TaskMoniter {
@@ -191,6 +204,49 @@ class TaskMoniter {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//保存数据到HeroUnion联盟
|
|
|
|
|
//检查已经抓取到数据的任务
|
|
|
|
|
async notifyHandle(task) { |
|
|
|
|
if (typeof(task.from) == 'undefined' || task.from != 'HeroUnion' || this.notifying) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//已经完成回传
|
|
|
|
|
if (typeof(task.notified) != 'undefined' && task.notified) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//判断当前任务数据回传次数是否小于最多尝试次数
|
|
|
|
|
if (typeof(task.notify_time) != 'undefined' && task.notify_time >= configs.herounion.notify_max_try) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//尝试回传数据
|
|
|
|
|
this.notifying = true; |
|
|
|
|
let saveRes = await this.heroBot.saveTaskData(task.id, task.token, task.data); |
|
|
|
|
this.notifying = false; |
|
|
|
|
|
|
|
|
|
if (typeof(task.notify_time) != 'undefined') { |
|
|
|
|
task.notify_time ++; |
|
|
|
|
}else { |
|
|
|
|
task.notify_time = 1; //回传次数
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
task.notify_at = common.getTimestampInSeconds(); //回传时间戳
|
|
|
|
|
|
|
|
|
|
//如果返回数据code=1,则认为数据保存成功,否则过几分钟再次尝试
|
|
|
|
|
if (saveRes && saveRes.code == 1) { |
|
|
|
|
task.notified = true; //记录已经完成回传
|
|
|
|
|
console.log("[%s][%s] Task %s's data save to HeroUnion done", |
|
|
|
|
common.getTimeString(), task.notify_time, task.id); |
|
|
|
|
}else { |
|
|
|
|
console.log("[%s][%s] Task %s's data save to HeroUnion failed, it will try again later.", |
|
|
|
|
common.getTimeString(), task.notify_time, task.id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.updateTask(task.id, task); //更新任务数据
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
run() { //开始监控任务目录,把所有任务缓存到内存
|
|
|
|
|
console.log('[%s] TaskMoniter started.', common.getTimeString()); |
|
|
|
|
|
|
|
|
@ -206,6 +262,22 @@ class TaskMoniter {
@@ -206,6 +262,22 @@ class TaskMoniter {
|
|
|
|
|
|
|
|
|
|
task_auto_run.start(); |
|
|
|
|
console.log('[%s] TaskMoniter auto check started.', common.getTimeString()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//定期向HeroUnion回传任务抓取结果
|
|
|
|
|
const task_notify_time = this.notify_time_gap; |
|
|
|
|
const notify_auto_run = cron.schedule(`*/${task_notify_time} * * * *`, async () => { |
|
|
|
|
let task = _self.tasks.find((item) => typeof(item.from) != 'undefined' && item.from == 'HeroUnion' && typeof(item.notified) == 'undefined'); |
|
|
|
|
if (task) { |
|
|
|
|
console.log("[%s] Try to save task %s's data to HeroUnion", common.getTimeString(), task.id); |
|
|
|
|
await _self.notifyHandle(task); |
|
|
|
|
} |
|
|
|
|
}, { |
|
|
|
|
scheduled: false |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
notify_auto_run.start(); |
|
|
|
|
console.log('[%s] TaskMoniter auto notify started.', common.getTimeString()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|