|
|
|
@ -38,6 +38,7 @@ class HeroUnion {
@@ -38,6 +38,7 @@ class HeroUnion {
|
|
|
|
|
this.notify_max_try = 5; //回调通知最多尝试次数
|
|
|
|
|
this.heroHeartTimeout = 600; //爬虫心跳超时时长,单位:秒
|
|
|
|
|
this.max_list_hero_num = 1000; //在接口getHeros()里最多返回的爬虫数量
|
|
|
|
|
this.axios_proxy = false; //axios库发送请求时是否使用系统代理
|
|
|
|
|
|
|
|
|
|
this.stats = { |
|
|
|
|
start_time: common.getTimestampInSeconds() |
|
|
|
@ -110,6 +111,10 @@ class HeroUnion {
@@ -110,6 +111,10 @@ class HeroUnion {
|
|
|
|
|
if (typeof(this.config.max_list_hero_num) != 'undefined' && this.config.max_list_hero_num) { |
|
|
|
|
this.max_list_hero_num = this.config.max_list_hero_num; //最大返回爬虫数量
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (typeof(this.config.axios_proxy) != 'undefined' && this.config.axios_proxy) { |
|
|
|
|
this.axios_proxy = this.config.axios_proxy; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return this.config; |
|
|
|
@ -298,21 +303,16 @@ class HeroUnion {
@@ -298,21 +303,16 @@ class HeroUnion {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//任务完成触发回调通知
|
|
|
|
|
async handleTaskDone(id) { |
|
|
|
|
async handleTaskDone(task) { |
|
|
|
|
let notified = false; |
|
|
|
|
|
|
|
|
|
let task = this.getTaskById(id); |
|
|
|
|
let notify_url = task.notify_url; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
if (notify_url && /^http(s)?:\/\/[\w\.]+/i.test(notify_url)) { |
|
|
|
|
//检查任务通知次数是否达到最大尝试次数
|
|
|
|
|
if (task.notify_time > this.notify_max_try) { |
|
|
|
|
common.error('[LIMITED] Task %s notify time has reach the max try number %s', |
|
|
|
|
task.id, this.notify_max_try); |
|
|
|
|
if (!notify_url || common.isUrlOk(notify_url) == false) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
common.log('[%s] Try to notify task %s via %s', task.notify_time, task.id, notify_url); |
|
|
|
|
|
|
|
|
|
let params = { |
|
|
|
|
"task_id": task.id, |
|
|
|
|
"task_result": task.results, |
|
|
|
@ -320,13 +320,21 @@ class HeroUnion {
@@ -320,13 +320,21 @@ class HeroUnion {
|
|
|
|
|
}; |
|
|
|
|
let token = await this.getUserToken(task.uuid); |
|
|
|
|
params.sign = common.sign(params, token); |
|
|
|
|
const response = await axios.post(notify_url, params, {timeout: this.notify_timeout*1000}); |
|
|
|
|
|
|
|
|
|
const response = await axios.post(notify_url, params, { |
|
|
|
|
timeout: this.notify_timeout*1000, |
|
|
|
|
proxy: this.axios_proxy |
|
|
|
|
}); |
|
|
|
|
if (response.status == 200) { |
|
|
|
|
notified = true; |
|
|
|
|
common.log('Task %s notify to %s done, response data:', task.id, notify_url, response.data); |
|
|
|
|
}else { |
|
|
|
|
common.error('[FAILED] Notify to %s failed, response status: %s, status text: %s, result: %s', |
|
|
|
|
notify_url, response.status, response.statusText, response.data); |
|
|
|
|
} |
|
|
|
|
}catch(err) { |
|
|
|
|
common.error('[ERROR] Notify to %s failed: %s', notify_url, err); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//更新任务notified状态以及notify_time通知次数
|
|
|
|
|
let taskIndex = this.tasks.findIndex((item) => item.id == task.id); |
|
|
|
@ -334,10 +342,6 @@ class HeroUnion {
@@ -334,10 +342,6 @@ class HeroUnion {
|
|
|
|
|
this.tasks[taskIndex].notified = notified; |
|
|
|
|
this.tasks[taskIndex].notify_time ++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}catch(err) { |
|
|
|
|
common.error('[ERROR] Notify to %s failed: %s', notify_url, err); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return notified; |
|
|
|
|
} |
|
|
|
@ -464,6 +468,25 @@ class HeroUnion {
@@ -464,6 +468,25 @@ class HeroUnion {
|
|
|
|
|
common.log('Cronjob of auto clean expired tasks started.'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//定期尝试给已完成状态的任务notify_url发送通知回调
|
|
|
|
|
autoNotifyTasks() { |
|
|
|
|
let _self = this; |
|
|
|
|
|
|
|
|
|
const frequence = typeof(this.config.autoNotifyTaskFrequence) != 'undefined' |
|
|
|
|
&& this.config.autoNotifyTaskFrequence ? this.config.autoNotifyTaskFrequence : 120; //2 分钟检查一次
|
|
|
|
|
const cronjob = cron.schedule(`*/${frequence} * * * * *`, () => { |
|
|
|
|
let task = _self.tasks.find((item) => item.status == 'done' && item.notified == false && item.notify_time < _self.notify_max_try); |
|
|
|
|
if (task) { |
|
|
|
|
_self.handleTaskDone(task); |
|
|
|
|
} |
|
|
|
|
}, { |
|
|
|
|
scheduled: false |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
cronjob.start(); |
|
|
|
|
common.log('Cronjob of auto notify done tasks started.'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//获取联盟状态
|
|
|
|
|
getStats() { |
|
|
|
|
this.stats.taskStatus = this.taskStatus; |
|
|
|
@ -523,6 +546,7 @@ class HeroUnion {
@@ -523,6 +546,7 @@ class HeroUnion {
|
|
|
|
|
this.autoReloadConfigs(); |
|
|
|
|
this.heroHeartCheck(); |
|
|
|
|
this.autoCleanExpiredTasks(); |
|
|
|
|
this.autoNotifyTasks(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|