diff --git a/heroUnion.mjs b/heroUnion.mjs index 6b2293c..cd51c5e 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -253,7 +253,8 @@ class HeroUnion { } //保存处理中任务结果 - saveTaskById(bot_name, id, data) { + //增加失败状态设置 + saveTaskById(bot_name, id, data, status) { let done = false; let taskIndex = this.tasks.findIndex((item) => item.id == id && item.status == 'running'); @@ -276,9 +277,16 @@ class HeroUnion { //更新统计数据 this.taskStatus.running --; - this.taskStatus.done ++; - - this.tasks[taskIndex].status = 'done'; + if (typeof(status) == 'undefined' || status == 'done') { + this.taskStatus.done ++; + this.tasks[taskIndex].status = 'done'; + }else if (typeof(status) != 'undefined' && status == 'failed') { + this.taskStatus.failed ++; + this.tasks[taskIndex].status = 'failed'; + this.tasks[taskIndex].error = typeof(data.error) != 'undefined' && data.error ? + data.error : 'HeroBot says it failed.'; + common.error('Task %s is failed, save by bot %s', id, bot_name); + } common.log('Task %s save data done by bot %s.', id, bot_name); done = true; diff --git a/public/index.html b/public/index.html index 032b44c..38c32ab 100644 --- a/public/index.html +++ b/public/index.html @@ -163,26 +163,38 @@ $('#herobots').html(html); }; - var api = '/api/stats/'; - $.ajax({ - url: api, - method: 'GET', - dataType: 'json' - }).done(function(data) { - $('#herounion_stats').text(JSON.stringify(data, null, 4)); - $('.run_time').text( formatSeconds(data.run_seconds) ); - renderStats('taskStatus', data.taskStatus); - renderStats('heroStatus', data.heroStatus); - }); - - api = '/api/heros/'; - $.ajax({ - url: api, - method: 'GET', - dataType: 'json' - }).done(function(data) { - renderHeros(data); - }); + var loadUnionStats = function() { + $('.run_time').text('...'); + var api = '/api/stats/'; + $.ajax({ + url: api, + method: 'GET', + dataType: 'json' + }).done(function(data) { + $('#herounion_stats').text(JSON.stringify(data, null, 4)); + $('.run_time').text( formatSeconds(data.run_seconds) ); + renderStats('taskStatus', data.taskStatus); + renderStats('heroStatus', data.heroStatus); + }); + }; + + var loadHeros = function() { + $('#herobots').text('...'); + var api = '/api/heros/'; + $.ajax({ + url: api, + method: 'GET', + dataType: 'json' + }).done(function(data) { + renderHeros(data); + }); + }; + + //init + loadUnionStats(); + loadHeros(); + + setInterval(loadUnionStats, 5000); \ No newline at end of file diff --git a/router_api.mjs b/router_api.mjs index f66d67d..5315b54 100644 --- a/router_api.mjs +++ b/router_api.mjs @@ -230,6 +230,7 @@ router.post('/savetask/', async (req, res) => { let name = req.body.name, task_id = req.body.task_id, task_result = req.body.task_result, + task_status = req.body.status, sign = req.body.sign; let data = {code: 0, message: ''}; @@ -261,11 +262,15 @@ router.post('/savetask/', async (req, res) => { task_result: task_result }; + if (typeof(task_status) != 'undefined' && task_status == 'failed') { + paramsCheck.status = task_status; + } + let mySign = common.sign(paramsCheck, task.token); if (mySign.toLowerCase() != sign.toLowerCase()) { data.message = `签名 ${sign} 不匹配,请确保token正确及签名方法跟文档一致`; }else { - let saved = heroUnion.saveTaskById(name, task_id, task_result); + let saved = heroUnion.saveTaskById(name, task_id, task_result, task_status); if (saved) { data.code = 1; data.message = '保存任务数据完成';