Browse Source

function saveTaskById ready

master
master 11 months ago
parent
commit
314efe87d8
  1. 43
      heroUnion.mjs
  2. 2
      index.mjs

43
heroUnion.mjs

@ -26,6 +26,10 @@ class HeroUnion {
//构造函数,设置默认配置 //构造函数,设置默认配置
constructor() { constructor() {
//this.task_data_dir = path.resolve('./tmp/data/'); //任务数据保存目录
this.task_cache_time = 86400; //任务数据最长缓存时间,单位:秒
this.task_data_max_size = 1024; //任务数据最大字节数,单位:KB
this.stats = {}; this.stats = {};
this.tasks = []; this.tasks = [];
this.taskStatus = { this.taskStatus = {
@ -59,6 +63,10 @@ class HeroUnion {
return Math.floor(Date.now() / 1000); return Math.floor(Date.now() / 1000);
} }
isDataTooLarge(data) {
return JSON.stringify(data).length > this.task_data_max_size * 1024;
}
//--任务相关功能-- //--任务相关功能--
//根据任务提交者ID和时间戳生成任务ID编号 //根据任务提交者ID和时间戳生成任务ID编号
@ -83,9 +91,14 @@ class HeroUnion {
* platform: '', * platform: '',
* data_mode: '', * data_mode: '',
* notify_url: '', * notify_url: '',
* results: [],
* created: 0, //timestamp in seconds
* updated: 0 //timestamp in seconds
* } * }
**/ **/
createTask(uuid, url, platform, data_mode, notify_url, country, lang) { createTask(uuid, url, platform, data_mode, notify_url, country, lang) {
let timestamp = this.getTimestampInSeconds();
let task = { let task = {
id: this.generateTaskId(uuid), id: this.generateTaskId(uuid),
status: 'waiting', status: 'waiting',
@ -99,7 +112,11 @@ class HeroUnion {
data_mode: 'default', data_mode: 'default',
country: 'china', country: 'china',
lang: 'zh-CN', lang: 'zh-CN',
notify_url: '' notify_url: '',
results: [],
created: timestamp,
updated: timestamp
}; };
if (typeof(data_mode) != 'undefined' && data_mode) { if (typeof(data_mode) != 'undefined' && data_mode) {
@ -166,8 +183,30 @@ class HeroUnion {
} }
//保存任务处理结果 //保存任务处理结果
async saveTaskById() { saveTaskById(uuid, id, data) {
let done = false;
let taskIndex = this.tasks.findIndex((item) => item.id == id);
if (taskIndex > -1) {
if (this.isDataTooLarge(data)) {
return false;
}
data.uuid = uuid;
let resIndex = this.tasks[taskIndex].results.findeIndex((dataItem) => dataItem.uuid == uuid);
if (resIndex == -1) {
this.tasks[taskIndex].results.push(data);
}else {
this.tasks[taskIndex].results[resIndex] = data;
}
this.tasks[taskIndex].updated = this.getTimestampInSeconds();
done = true;
}
return done;
} }
//查询某个任务的状态及其数据 //查询某个任务的状态及其数据

2
index.mjs

@ -32,7 +32,7 @@ app.use((err, req, res, next) => {
return next(err); return next(err);
} }
console.error('Request error in tg bot: %s', err.stack); console.error('Request error in hero union: %s', err.stack);
var statusCode = 500; var statusCode = 500;
if (typeof(err.statusCode) != 'undefined' && err.statusCode) { if (typeof(err.statusCode) != 'undefined' && err.statusCode) {

Loading…
Cancel
Save