Browse Source

api save task add parameter status

master
filesite 7 months ago
parent
commit
b58d54f0c8
  1. 12
      heroUnion.mjs
  2. 14
      public/index.html
  3. 7
      router_api.mjs

12
heroUnion.mjs

@ -253,7 +253,8 @@ class HeroUnion {
} }
//保存处理中任务结果 //保存处理中任务结果
saveTaskById(bot_name, id, data) { //增加失败状态设置
saveTaskById(bot_name, id, data, status) {
let done = false; let done = false;
let taskIndex = this.tasks.findIndex((item) => item.id == id && item.status == 'running'); let taskIndex = this.tasks.findIndex((item) => item.id == id && item.status == 'running');
@ -276,9 +277,16 @@ class HeroUnion {
//更新统计数据 //更新统计数据
this.taskStatus.running --; this.taskStatus.running --;
if (typeof(status) == 'undefined' || status == 'done') {
this.taskStatus.done ++; this.taskStatus.done ++;
this.tasks[taskIndex].status = '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); common.log('Task %s save data done by bot %s.', id, bot_name);
done = true; done = true;

14
public/index.html

@ -163,6 +163,8 @@
$('#herobots').html(html); $('#herobots').html(html);
}; };
var loadUnionStats = function() {
$('.run_time').text('...');
var api = '/api/stats/'; var api = '/api/stats/';
$.ajax({ $.ajax({
url: api, url: api,
@ -174,8 +176,11 @@
renderStats('taskStatus', data.taskStatus); renderStats('taskStatus', data.taskStatus);
renderStats('heroStatus', data.heroStatus); renderStats('heroStatus', data.heroStatus);
}); });
};
api = '/api/heros/'; var loadHeros = function() {
$('#herobots').text('...');
var api = '/api/heros/';
$.ajax({ $.ajax({
url: api, url: api,
method: 'GET', method: 'GET',
@ -183,6 +188,13 @@
}).done(function(data) { }).done(function(data) {
renderHeros(data); renderHeros(data);
}); });
};
//init
loadUnionStats();
loadHeros();
setInterval(loadUnionStats, 5000);
</script> </script>
</body> </body>
</html> </html>

7
router_api.mjs

@ -230,6 +230,7 @@ router.post('/savetask/', async (req, res) => {
let name = req.body.name, let name = req.body.name,
task_id = req.body.task_id, task_id = req.body.task_id,
task_result = req.body.task_result, task_result = req.body.task_result,
task_status = req.body.status,
sign = req.body.sign; sign = req.body.sign;
let data = {code: 0, message: ''}; let data = {code: 0, message: ''};
@ -261,11 +262,15 @@ router.post('/savetask/', async (req, res) => {
task_result: task_result task_result: task_result
}; };
if (typeof(task_status) != 'undefined' && task_status == 'failed') {
paramsCheck.status = task_status;
}
let mySign = common.sign(paramsCheck, task.token); let mySign = common.sign(paramsCheck, task.token);
if (mySign.toLowerCase() != sign.toLowerCase()) { if (mySign.toLowerCase() != sign.toLowerCase()) {
data.message = `签名 ${sign} 不匹配,请确保token正确及签名方法跟文档一致`; data.message = `签名 ${sign} 不匹配,请确保token正确及签名方法跟文档一致`;
}else { }else {
let saved = heroUnion.saveTaskById(name, task_id, task_result); let saved = heroUnion.saveTaskById(name, task_id, task_result, task_status);
if (saved) { if (saved) {
data.code = 1; data.code = 1;
data.message = '保存任务数据完成'; data.message = '保存任务数据完成';

Loading…
Cancel
Save