Browse Source

api save task add parameter status

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

16
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 --;
this.taskStatus.done ++; if (typeof(status) == 'undefined' || status == '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;

52
public/index.html

@ -163,26 +163,38 @@
$('#herobots').html(html); $('#herobots').html(html);
}; };
var api = '/api/stats/'; var loadUnionStats = function() {
$.ajax({ $('.run_time').text('...');
url: api, var api = '/api/stats/';
method: 'GET', $.ajax({
dataType: 'json' url: api,
}).done(function(data) { method: 'GET',
$('#herounion_stats').text(JSON.stringify(data, null, 4)); dataType: 'json'
$('.run_time').text( formatSeconds(data.run_seconds) ); }).done(function(data) {
renderStats('taskStatus', data.taskStatus); $('#herounion_stats').text(JSON.stringify(data, null, 4));
renderStats('heroStatus', data.heroStatus); $('.run_time').text( formatSeconds(data.run_seconds) );
}); renderStats('taskStatus', data.taskStatus);
renderStats('heroStatus', data.heroStatus);
api = '/api/heros/'; });
$.ajax({ };
url: api,
method: 'GET', var loadHeros = function() {
dataType: 'json' $('#herobots').text('...');
}).done(function(data) { var api = '/api/heros/';
renderHeros(data); $.ajax({
}); url: api,
method: 'GET',
dataType: 'json'
}).done(function(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