From 314efe87d8ec608d37093037db335cce8327933c Mon Sep 17 00:00:00 2001 From: master Date: Sun, 24 Dec 2023 18:43:25 +0800 Subject: [PATCH] function saveTaskById ready --- heroUnion.mjs | 43 +++++++++++++++++++++++++++++++++++++++++-- index.mjs | 2 +- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/heroUnion.mjs b/heroUnion.mjs index 2cc14c0..28a63bb 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -26,6 +26,10 @@ class HeroUnion { //构造函数,设置默认配置 constructor() { + //this.task_data_dir = path.resolve('./tmp/data/'); //任务数据保存目录 + this.task_cache_time = 86400; //任务数据最长缓存时间,单位:秒 + this.task_data_max_size = 1024; //任务数据最大字节数,单位:KB + this.stats = {}; this.tasks = []; this.taskStatus = { @@ -59,6 +63,10 @@ class HeroUnion { return Math.floor(Date.now() / 1000); } + isDataTooLarge(data) { + return JSON.stringify(data).length > this.task_data_max_size * 1024; + } + //--任务相关功能-- //根据任务提交者ID和时间戳生成任务ID编号 @@ -83,9 +91,14 @@ class HeroUnion { * platform: '', * data_mode: '', * notify_url: '', + * results: [], + * created: 0, //timestamp in seconds + * updated: 0 //timestamp in seconds * } **/ createTask(uuid, url, platform, data_mode, notify_url, country, lang) { + let timestamp = this.getTimestampInSeconds(); + let task = { id: this.generateTaskId(uuid), status: 'waiting', @@ -99,7 +112,11 @@ class HeroUnion { data_mode: 'default', country: 'china', lang: 'zh-CN', - notify_url: '' + notify_url: '', + results: [], + + created: timestamp, + updated: timestamp }; 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; } //查询某个任务的状态及其数据 diff --git a/index.mjs b/index.mjs index b4af2d3..6ebd764 100644 --- a/index.mjs +++ b/index.mjs @@ -32,7 +32,7 @@ app.use((err, req, res, next) => { 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; if (typeof(err.statusCode) != 'undefined' && err.statusCode) {