@kne/fastify-task 3.0.0-alpha.4 → 3.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +4 -35
- package/libs/controllers/main.js +1 -0
- package/libs/services/main.js +329 -226
- package/libs/services/statistics.js +73 -44
- package/libs/utils/cache-manager.js +162 -0
- package/libs/utils/config.js +326 -0
- package/libs/utils/errors.js +191 -0
- package/libs/utils/modules/task-executor.js +196 -0
- package/libs/utils/modules/task-processor.js +185 -0
- package/libs/utils/security-validator.js +203 -0
- package/package.json +3 -2
|
@@ -1,31 +1,42 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
const dayjs = require('dayjs');
|
|
3
|
+
const utc = require('dayjs/plugin/utc');
|
|
4
|
+
const timezone = require('dayjs/plugin/timezone');
|
|
5
|
+
const {
|
|
6
|
+
CONFIG_CONSTANTS,
|
|
7
|
+
getConfigManager
|
|
8
|
+
} = require('../utils/config');
|
|
9
|
+
|
|
10
|
+
dayjs.extend(utc);
|
|
11
|
+
dayjs.extend(timezone);
|
|
12
|
+
|
|
13
|
+
// 查询用客户端时间:parseRange 使用客户端时区计算日期范围
|
|
14
|
+
const parseRange = (range = '7d', tz) => {
|
|
15
|
+
const config = CONFIG_CONSTANTS.TIME_RANGES[range];
|
|
15
16
|
if (!config) {
|
|
16
|
-
throw new Error(`不支持的时间范围: ${range}, 支持: ${Object.keys(
|
|
17
|
+
throw new Error(`不支持的时间范围: ${range}, 支持: ${Object.keys(CONFIG_CONSTANTS.TIME_RANGES).join(',')}`);
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
// 写入数据全使用服务器时间,查询用客户端时间
|
|
20
|
+
// 使用客户端时区计算"现在"和"起始时间",确保日期边界对齐客户端视角
|
|
21
|
+
const now = tz ? dayjs().tz(tz) : dayjs();
|
|
22
|
+
const unitStartMap = { day: 'day', month: 'month', year: 'year' };
|
|
23
|
+
const startUnit = unitStartMap[config.unit];
|
|
24
|
+
const startTime = startUnit
|
|
25
|
+
? now.subtract(config.value, startUnit).startOf(startUnit)
|
|
26
|
+
: now.subtract(config.value, config.unit);
|
|
27
|
+
return { startTime: startTime.toDate(), endTime: now.toDate(), label: config.label, range };
|
|
24
28
|
};
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
// 查询用客户端时间:formatDate 使用客户端时区格式化日期
|
|
31
|
+
const formatDate = (date, tz) => {
|
|
32
|
+
const d = tz ? dayjs(date).tz(tz) : dayjs(date);
|
|
33
|
+
return d.format('YYYY-MM-DD');
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// 查询用客户端时间:提取客户端时区下的小时
|
|
37
|
+
const getHour = (date, tz) => {
|
|
38
|
+
const d = tz ? dayjs(date).tz(tz) : dayjs(date);
|
|
39
|
+
return d.hour();
|
|
29
40
|
};
|
|
30
41
|
|
|
31
42
|
const safeNum = v => {
|
|
@@ -57,7 +68,7 @@ const buildChannels = async (statisticsServices, type, runnerType) => {
|
|
|
57
68
|
const list = Array.isArray(metaResult) ? metaResult : metaResult.list || [];
|
|
58
69
|
const rootChannels = list.map(meta => meta.channel).filter(Boolean);
|
|
59
70
|
const types = type ? rootChannels.filter(ch => ch === type) : rootChannels;
|
|
60
|
-
const rts = runnerType ? [runnerType] : RUNNER_TYPES;
|
|
71
|
+
const rts = runnerType ? [runnerType] : [CONFIG_CONSTANTS.RUNNER_TYPES.SYSTEM, CONFIG_CONSTANTS.RUNNER_TYPES.MANUAL];
|
|
61
72
|
const channels = [];
|
|
62
73
|
for (const t of types) {
|
|
63
74
|
channels.push(t);
|
|
@@ -81,24 +92,40 @@ const accumDuration = (acc, count, sum) => {
|
|
|
81
92
|
};
|
|
82
93
|
|
|
83
94
|
// 创建空的 dailyMap 条目
|
|
84
|
-
const createDailyMapEntry = () => ({
|
|
95
|
+
const createDailyMapEntry = () => ({
|
|
96
|
+
total: 0,
|
|
97
|
+
byStatus: {
|
|
98
|
+
[CONFIG_CONSTANTS.TASK_STATUSES.SUCCESS]: 0,
|
|
99
|
+
[CONFIG_CONSTANTS.TASK_STATUSES.FAILED]: 0,
|
|
100
|
+
[CONFIG_CONSTANTS.TASK_STATUSES.CANCELED]: 0
|
|
101
|
+
},
|
|
102
|
+
byType: {}
|
|
103
|
+
});
|
|
85
104
|
|
|
86
105
|
// 创建空的 dailyDurationMap 条目
|
|
87
106
|
const createDailyDurationEntry = () => ({
|
|
88
|
-
completedCount: 0,
|
|
89
|
-
|
|
90
|
-
|
|
107
|
+
completedCount: 0,
|
|
108
|
+
successCount: 0,
|
|
109
|
+
failedCount: 0,
|
|
110
|
+
canceledCount: 0,
|
|
111
|
+
sumWaiting: 0,
|
|
112
|
+
sumExecution: 0,
|
|
113
|
+
sumTotal: 0,
|
|
114
|
+
countWithTiming: 0,
|
|
115
|
+
byType: {},
|
|
116
|
+
byRunnerType: {}
|
|
91
117
|
});
|
|
92
118
|
|
|
93
119
|
// 查询 statistics 并解析结果
|
|
94
|
-
|
|
120
|
+
// 写入数据全使用服务器时间(DB 中存储的是服务器时间),查询用客户端时间转换展示
|
|
121
|
+
const queryAndParse = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
|
|
95
122
|
if (channels.length === 0) return null;
|
|
96
123
|
|
|
97
124
|
const statResult = await statisticsServices.query({
|
|
98
125
|
channels, startTime, endTime,
|
|
99
|
-
attributeNames:
|
|
126
|
+
attributeNames: CONFIG_CONSTANTS.STATISTICS_ATTRIBUTES,
|
|
100
127
|
aggregates: ['sum'],
|
|
101
|
-
timezone:
|
|
128
|
+
timezone: tz || undefined
|
|
102
129
|
});
|
|
103
130
|
|
|
104
131
|
const result = {
|
|
@@ -128,8 +155,9 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
128
155
|
const sum = getSumData(item);
|
|
129
156
|
const count = safeNum(sum.total);
|
|
130
157
|
const isHourly = item.period === 'h';
|
|
131
|
-
|
|
132
|
-
const
|
|
158
|
+
// 查询用客户端时间:日期和小时使用客户端时区转换
|
|
159
|
+
const date = formatDate(item.time, tz);
|
|
160
|
+
const hour = isHourly ? getHour(item.time, tz) : null;
|
|
133
161
|
|
|
134
162
|
// ─── 1-segment channel: 全局 + 按日 + 时长 聚合 ───
|
|
135
163
|
if (!runnerTypeName) {
|
|
@@ -144,7 +172,7 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
144
172
|
result.duration.completedCount += count;
|
|
145
173
|
result.duration.successCount += safeNum(sum.success);
|
|
146
174
|
result.duration.failedCount += safeNum(sum.failed);
|
|
147
|
-
result.duration.
|
|
175
|
+
result.duration.canceled += safeNum(sum.canceled);
|
|
148
176
|
result.duration.sumWaiting += safeNum(sum.waitingTime);
|
|
149
177
|
result.duration.sumExecution += safeNum(sum.executionTime);
|
|
150
178
|
result.duration.sumTotal += safeNum(sum.totalTime);
|
|
@@ -348,7 +376,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
348
376
|
|
|
349
377
|
// ─── queryStatistics (history 接口) ───
|
|
350
378
|
const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
|
|
351
|
-
|
|
379
|
+
// 查询用客户端时间:parseRange 传入 timezone
|
|
380
|
+
const { startTime, endTime, label, range: rangeKey } = parseRange(range, timezone);
|
|
352
381
|
const emptyResult = {
|
|
353
382
|
range: rangeKey, rangeLabel: label,
|
|
354
383
|
totalTasks: 0, byStatus: {}, byType: {}, byRunnerType: {},
|
|
@@ -380,11 +409,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
380
409
|
};
|
|
381
410
|
|
|
382
411
|
// ─── buildSseData (SSE 接口) ───
|
|
383
|
-
const buildSseData = async ({ timezone, type, runnerType }) => {
|
|
384
|
-
|
|
385
|
-
const
|
|
386
|
-
const
|
|
387
|
-
const
|
|
412
|
+
const buildSseData = async ({ timezone: tz, type, runnerType }) => {
|
|
413
|
+
// 查询用客户端时间:"今天"的边界使用客户端时区
|
|
414
|
+
const now = tz ? dayjs().tz(tz) : dayjs();
|
|
415
|
+
const todayStart = now.startOf('day').toDate();
|
|
416
|
+
const todayEnd = now.endOf('day').toDate();
|
|
417
|
+
const todayStr = now.format('YYYY-MM-DD');
|
|
388
418
|
|
|
389
419
|
// 统计查询:正在执行和等待执行的数量 + DB 查询
|
|
390
420
|
const [runnerTypeStatusRows, waitingTasks] = await Promise.all([
|
|
@@ -462,7 +492,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
462
492
|
|
|
463
493
|
try {
|
|
464
494
|
const channels = await buildChannels(statisticsServices, type, runnerType);
|
|
465
|
-
const parsed = await queryAndParse(statisticsServices, { channels, startTime: todayStart, endTime: todayEnd, timezone, runnerType });
|
|
495
|
+
const parsed = await queryAndParse(statisticsServices, { channels, startTime: todayStart, endTime: todayEnd, timezone: tz, runnerType });
|
|
466
496
|
if (parsed) {
|
|
467
497
|
totalTasks = parsed.totalTasks + dbStatusCount.running + dbWaitingTotal;
|
|
468
498
|
byStatus = { ...parsed.byStatus, running: dbStatusCount.running, waiting: dbWaitingTotal, pending: 0 };
|
|
@@ -500,9 +530,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
500
530
|
}
|
|
501
531
|
|
|
502
532
|
// 补充当前小时的 running/waiting 瞬时状态(来自 DB 实时查询,不受 statistics 服务影响)
|
|
503
|
-
//
|
|
504
|
-
|
|
505
|
-
const currentHour = now.getHours();
|
|
533
|
+
// 查询用客户端时间:当前小时使用客户端时区
|
|
534
|
+
const currentHour = now.hour();
|
|
506
535
|
if (dbStatusCount.running > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'running', count: dbStatusCount.running });
|
|
507
536
|
if (dbWaitingTotal > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'waiting', count: dbWaitingTotal });
|
|
508
537
|
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内存缓存管理器
|
|
3
|
+
* 提供简单的缓存功能,支持过期时间
|
|
4
|
+
*/
|
|
5
|
+
class CacheManager {
|
|
6
|
+
constructor(defaultTTL = 300000) { // 默认5分钟过期
|
|
7
|
+
this.cache = new Map();
|
|
8
|
+
this.defaultTTL = defaultTTL;
|
|
9
|
+
this.timers = new Map();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 设置缓存
|
|
14
|
+
* @param {string} key - 缓存键
|
|
15
|
+
* @param {*} value - 缓存值
|
|
16
|
+
* @param {number} ttl - 过期时间(毫秒)
|
|
17
|
+
*/
|
|
18
|
+
set(key, value, ttl = this.defaultTTL) {
|
|
19
|
+
// 清除现有的定时器
|
|
20
|
+
if (this.timers.has(key)) {
|
|
21
|
+
clearTimeout(this.timers.get(key));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 设置缓存
|
|
25
|
+
this.cache.set(key, {
|
|
26
|
+
value,
|
|
27
|
+
expiresAt: Date.now() + ttl
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// 设置过期定时器
|
|
31
|
+
const timer = setTimeout(() => {
|
|
32
|
+
this.delete(key);
|
|
33
|
+
}, ttl);
|
|
34
|
+
timer.unref();
|
|
35
|
+
|
|
36
|
+
this.timers.set(key, timer);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 获取缓存
|
|
41
|
+
* @param {string} key - 缓存键
|
|
42
|
+
* @returns {*} 缓存值,如果不存在或已过期返回 null
|
|
43
|
+
*/
|
|
44
|
+
get(key) {
|
|
45
|
+
const item = this.cache.get(key);
|
|
46
|
+
|
|
47
|
+
if (!item) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 检查是否过期
|
|
52
|
+
if (Date.now() > item.expiresAt) {
|
|
53
|
+
this.delete(key);
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return item.value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 删除缓存
|
|
62
|
+
* @param {string} key - 缓存键
|
|
63
|
+
*/
|
|
64
|
+
delete(key) {
|
|
65
|
+
if (this.timers.has(key)) {
|
|
66
|
+
clearTimeout(this.timers.get(key));
|
|
67
|
+
this.timers.delete(key);
|
|
68
|
+
}
|
|
69
|
+
this.cache.delete(key);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 清空所有缓存
|
|
74
|
+
*/
|
|
75
|
+
clear() {
|
|
76
|
+
// 清除所有定时器
|
|
77
|
+
for (const timer of this.timers.values()) {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
}
|
|
80
|
+
this.timers.clear();
|
|
81
|
+
this.cache.clear();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 检查缓存是否存在且未过期
|
|
86
|
+
* @param {string} key - 缓存键
|
|
87
|
+
* @returns {boolean} 是否存在有效缓存
|
|
88
|
+
*/
|
|
89
|
+
has(key) {
|
|
90
|
+
return this.get(key) !== null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 获取或设置缓存
|
|
95
|
+
* @param {string} key - 缓存键
|
|
96
|
+
* @param {Function} factory - 值工厂函数
|
|
97
|
+
* @param {number} ttl - 过期时间(毫秒)
|
|
98
|
+
* @returns {*} 缓存值
|
|
99
|
+
*/
|
|
100
|
+
async getOrSet(key, factory, ttl = this.defaultTTL) {
|
|
101
|
+
const cached = this.get(key);
|
|
102
|
+
if (cached !== null) {
|
|
103
|
+
return cached;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const value = await factory();
|
|
107
|
+
this.set(key, value, ttl);
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 清理过期缓存
|
|
113
|
+
*/
|
|
114
|
+
cleanup() {
|
|
115
|
+
const now = Date.now();
|
|
116
|
+
for (const [key, item] of this.cache.entries()) {
|
|
117
|
+
if (now > item.expiresAt) {
|
|
118
|
+
this.delete(key);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 获取缓存统计信息
|
|
125
|
+
* @returns {Object} 统计信息
|
|
126
|
+
*/
|
|
127
|
+
getStats() {
|
|
128
|
+
return {
|
|
129
|
+
size: this.cache.size,
|
|
130
|
+
timerCount: this.timers.size
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 创建单例实例
|
|
136
|
+
let cacheManagerInstance = null;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 获取缓存管理器实例
|
|
140
|
+
* @param {number} defaultTTL - 默认过期时间
|
|
141
|
+
* @returns {CacheManager} 缓存管理器实例
|
|
142
|
+
*/
|
|
143
|
+
function getCacheManager(defaultTTL = null) {
|
|
144
|
+
if (defaultTTL !== null || !cacheManagerInstance) {
|
|
145
|
+
cacheManagerInstance = new CacheManager(defaultTTL);
|
|
146
|
+
}
|
|
147
|
+
return cacheManagerInstance;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 测试用:重置单例
|
|
151
|
+
function _resetCacheManager() {
|
|
152
|
+
if (cacheManagerInstance) {
|
|
153
|
+
cacheManagerInstance.clear();
|
|
154
|
+
}
|
|
155
|
+
cacheManagerInstance = null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
module.exports = {
|
|
159
|
+
CacheManager,
|
|
160
|
+
getCacheManager,
|
|
161
|
+
_resetCacheManager
|
|
162
|
+
};
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
const path = require('node:path');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 配置常量定义
|
|
5
|
+
*/
|
|
6
|
+
const CONFIG_CONSTANTS = {
|
|
7
|
+
// 日志相关
|
|
8
|
+
LOGS_MAX_SIZE: 100,
|
|
9
|
+
LOGS_RETENTION_DAYS: 30,
|
|
10
|
+
|
|
11
|
+
// 轮询相关
|
|
12
|
+
POLL_MAX_TIMES: 20,
|
|
13
|
+
POLL_INTERVAL_MS: 10000,
|
|
14
|
+
POLL_MIN_INTERVAL_MS: 10,
|
|
15
|
+
POLL_MAX_INTERVAL_MS: 60000,
|
|
16
|
+
|
|
17
|
+
// 任务超时相关
|
|
18
|
+
TASK_TIMEOUT_DEFAULT: 30 * 60 * 1000, // 30分钟
|
|
19
|
+
TASK_TIMEOUT_MIN: 0, // 0表示不超时
|
|
20
|
+
TASK_TIMEOUT_MAX: 24 * 60 * 60 * 1000, // 24小时
|
|
21
|
+
|
|
22
|
+
// 重试相关
|
|
23
|
+
RETRY_BASE_DELAY: 5000, // 5秒
|
|
24
|
+
RETRY_MAX_DELAY: 300000, // 5分钟
|
|
25
|
+
RETRY_MAX_COUNT: 10,
|
|
26
|
+
|
|
27
|
+
// 并发相关
|
|
28
|
+
CONCURRENT_TASKS_DEFAULT: 10,
|
|
29
|
+
CONCURRENT_TASKS_MIN: 1,
|
|
30
|
+
CONCURRENT_TASKS_MAX: 100,
|
|
31
|
+
|
|
32
|
+
// 优先级相关
|
|
33
|
+
PRIORITY_MIN: 0,
|
|
34
|
+
PRIORITY_MAX: 100,
|
|
35
|
+
PRIORITY_DEFAULT: 0,
|
|
36
|
+
|
|
37
|
+
// 延迟相关
|
|
38
|
+
DELAY_MIN: 0,
|
|
39
|
+
DELAY_MAX: 86400, // 24小时
|
|
40
|
+
|
|
41
|
+
// 文件相关
|
|
42
|
+
SCRIPT_EXTENSION: '.js',
|
|
43
|
+
SCRIPT_NAME_DEFAULT: 'index',
|
|
44
|
+
|
|
45
|
+
// 签名相关
|
|
46
|
+
SIGNATURE_ALGORITHM: 'sha256',
|
|
47
|
+
SIGNATURE_MIN_LENGTH: 32,
|
|
48
|
+
|
|
49
|
+
// 状态相关
|
|
50
|
+
TASK_STATUSES: {
|
|
51
|
+
PENDING: 'pending',
|
|
52
|
+
RUNNING: 'running',
|
|
53
|
+
WAITING: 'waiting',
|
|
54
|
+
SUCCESS: 'success',
|
|
55
|
+
FAILED: 'failed',
|
|
56
|
+
CANCELED: 'canceled'
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
// 执行者类型
|
|
60
|
+
RUNNER_TYPES: {
|
|
61
|
+
SYSTEM: 'system',
|
|
62
|
+
MANUAL: 'manual'
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
// 时间范围
|
|
66
|
+
TIME_RANGES: {
|
|
67
|
+
'7d': { value: 7, unit: 'day', label: '近7天' },
|
|
68
|
+
'1m': { value: 1, unit: 'month', label: '近1个月' },
|
|
69
|
+
'3m': { value: 3, unit: 'month', label: '近3个月' },
|
|
70
|
+
'1y': { value: 1, unit: 'year', label: '近1年' }
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
// 统计相关
|
|
74
|
+
STATISTICS_ATTRIBUTES: ['total', 'success', 'failed', 'canceled', 'waitingTime', 'executionTime', 'totalTime'],
|
|
75
|
+
STATISTICS_PERIODS: ['h', 'd'],
|
|
76
|
+
|
|
77
|
+
// 数据库相关
|
|
78
|
+
DB_TABLE_PREFIX_DEFAULT: 't_',
|
|
79
|
+
DB_INDEXES: ['created_at', 'status', 'type', 'runner_type', ['type', 'status'], ['runner_type', 'status'], ['target_id', 'target_type', 'type'], ['parent_task_id'], ['priority']]
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 默认配置
|
|
84
|
+
*/
|
|
85
|
+
const DEFAULT_CONFIG = {
|
|
86
|
+
// 数据库配置
|
|
87
|
+
dbTableNamePrefix: CONFIG_CONSTANTS.DB_TABLE_PREFIX_DEFAULT,
|
|
88
|
+
|
|
89
|
+
// API配置
|
|
90
|
+
prefix: '/api/task',
|
|
91
|
+
name: 'task',
|
|
92
|
+
|
|
93
|
+
// 并发配置
|
|
94
|
+
limit: CONFIG_CONSTANTS.CONCURRENT_TASKS_DEFAULT,
|
|
95
|
+
|
|
96
|
+
// 目录配置
|
|
97
|
+
dir: path.resolve(process.cwd(), 'libs', 'tasks'),
|
|
98
|
+
dirs: null,
|
|
99
|
+
|
|
100
|
+
// 定时任务配置
|
|
101
|
+
cronTime: '*/10 * * * *',
|
|
102
|
+
|
|
103
|
+
// 脚本配置
|
|
104
|
+
scriptName: CONFIG_CONSTANTS.SCRIPT_NAME_DEFAULT,
|
|
105
|
+
|
|
106
|
+
// 轮询配置
|
|
107
|
+
maxPollTimes: CONFIG_CONSTANTS.POLL_MAX_TIMES,
|
|
108
|
+
pollInterval: CONFIG_CONSTANTS.POLL_INTERVAL_MS,
|
|
109
|
+
|
|
110
|
+
// 超时配置
|
|
111
|
+
taskTimeout: CONFIG_CONSTANTS.TASK_TIMEOUT_DEFAULT,
|
|
112
|
+
|
|
113
|
+
// 重试配置
|
|
114
|
+
retryBaseDelay: CONFIG_CONSTANTS.RETRY_BASE_DELAY,
|
|
115
|
+
|
|
116
|
+
// 用户模型获取
|
|
117
|
+
getUserModel: () => {
|
|
118
|
+
throw new Error('getUserModel 必须在配置中提供');
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
// 认证获取
|
|
122
|
+
getAuthenticate: () => {
|
|
123
|
+
throw new Error('getAuthenticate 必须在配置中提供');
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
// 任务处理器
|
|
127
|
+
task: {},
|
|
128
|
+
|
|
129
|
+
// 任务错误统一处理(可选)
|
|
130
|
+
// 当任务执行失败时调用,接收参数: { task, error, type }
|
|
131
|
+
// - task: 任务对象
|
|
132
|
+
// - error: 错误信息
|
|
133
|
+
// - type: 错误类型(execution/timeout/callback/retry_exhausted)
|
|
134
|
+
errorHandler: null,
|
|
135
|
+
|
|
136
|
+
// 统计配置
|
|
137
|
+
statistics: {
|
|
138
|
+
enabled: true,
|
|
139
|
+
retentionDays: CONFIG_CONSTANTS.LOGS_RETENTION_DAYS
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
// 安全配置
|
|
143
|
+
security: {
|
|
144
|
+
enableSignature: true,
|
|
145
|
+
enablePathValidation: true,
|
|
146
|
+
enableInputValidation: true,
|
|
147
|
+
maxTaskSize: 1024 * 1024 // 1MB
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 配置管理器
|
|
153
|
+
*/
|
|
154
|
+
class ConfigManager {
|
|
155
|
+
constructor(userConfig = {}) {
|
|
156
|
+
this.config = this.mergeConfig(DEFAULT_CONFIG, userConfig);
|
|
157
|
+
this.validateConfig();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 合并配置
|
|
162
|
+
* @param {Object} defaults - 默认配置
|
|
163
|
+
* @param {Object} userConfig - 用户配置
|
|
164
|
+
* @returns {Object} 合并后的配置
|
|
165
|
+
*/
|
|
166
|
+
mergeConfig(defaults, userConfig) {
|
|
167
|
+
// 深拷贝默认配置的嵌套对象,避免修改 DEFAULT_CONFIG
|
|
168
|
+
const merged = {};
|
|
169
|
+
Object.keys(defaults).forEach(key => {
|
|
170
|
+
const val = defaults[key];
|
|
171
|
+
if (typeof val === 'object' && !Array.isArray(val) && val !== null) {
|
|
172
|
+
merged[key] = { ...val };
|
|
173
|
+
} else if (Array.isArray(val)) {
|
|
174
|
+
merged[key] = [...val];
|
|
175
|
+
} else {
|
|
176
|
+
merged[key] = val;
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// 处理 dirs 配置的向后兼容
|
|
181
|
+
if (!userConfig.dirs) {
|
|
182
|
+
merged.dirs = [merged.dir];
|
|
183
|
+
} else if (!userConfig.dirs.includes(merged.dir)) {
|
|
184
|
+
merged.dirs = [merged.dir, ...userConfig.dirs];
|
|
185
|
+
} else {
|
|
186
|
+
merged.dirs = [...userConfig.dirs];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 合并其他配置
|
|
190
|
+
Object.keys(userConfig).forEach(key => {
|
|
191
|
+
if (key !== 'dirs') {
|
|
192
|
+
if (typeof userConfig[key] === 'object' && !Array.isArray(userConfig[key]) && userConfig[key] !== null) {
|
|
193
|
+
merged[key] = { ...defaults[key], ...userConfig[key] };
|
|
194
|
+
} else {
|
|
195
|
+
merged[key] = userConfig[key];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return merged;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* 验证配置
|
|
205
|
+
* @throws {Error} 如果配置无效
|
|
206
|
+
*/
|
|
207
|
+
validateConfig() {
|
|
208
|
+
// 验证必需的配置项
|
|
209
|
+
if (!this.config.name || typeof this.config.name !== 'string') {
|
|
210
|
+
throw new Error('配置项 name 必须为非空字符串');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 验证数值范围
|
|
214
|
+
if (this.config.limit < CONFIG_CONSTANTS.CONCURRENT_TASKS_MIN || this.config.limit > CONFIG_CONSTANTS.CONCURRENT_TASKS_MAX) {
|
|
215
|
+
throw new Error(`配置项 limit 必须在 ${CONFIG_CONSTANTS.CONCURRENT_TASKS_MIN} 到 ${CONFIG_CONSTANTS.CONCURRENT_TASKS_MAX} 之间`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (this.config.taskTimeout < CONFIG_CONSTANTS.TASK_TIMEOUT_MIN || this.config.taskTimeout > CONFIG_CONSTANTS.TASK_TIMEOUT_MAX) {
|
|
219
|
+
throw new Error(`配置项 taskTimeout 必须在 ${CONFIG_CONSTANTS.TASK_TIMEOUT_MIN} 到 ${CONFIG_CONSTANTS.TASK_TIMEOUT_MAX} 之间`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 验证目录配置
|
|
223
|
+
if (!Array.isArray(this.config.dirs) || this.config.dirs.length === 0) {
|
|
224
|
+
throw new Error('配置项 dirs 必须为非空数组');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 验证函数类型
|
|
228
|
+
if (typeof this.config.getUserModel !== 'function') {
|
|
229
|
+
throw new Error('配置项 getUserModel 必须为函数');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (typeof this.config.getAuthenticate !== 'function') {
|
|
233
|
+
throw new Error('配置项 getAuthenticate 必须为函数');
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 验证 errorHandler(可选,如果提供则必须为函数)
|
|
237
|
+
if (this.config.errorHandler !== null && typeof this.config.errorHandler !== 'function') {
|
|
238
|
+
throw new Error('配置项 errorHandler 必须为函数或 null');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 获取配置值
|
|
244
|
+
* @param {string} key - 配置键
|
|
245
|
+
* @param {*} defaultValue - 默认值
|
|
246
|
+
* @returns {*} 配置值
|
|
247
|
+
*/
|
|
248
|
+
get(key, defaultValue = undefined) {
|
|
249
|
+
const keys = key.split('.');
|
|
250
|
+
let value = this.config;
|
|
251
|
+
|
|
252
|
+
for (const k of keys) {
|
|
253
|
+
if (value && typeof value === 'object' && k in value) {
|
|
254
|
+
value = value[k];
|
|
255
|
+
} else {
|
|
256
|
+
return defaultValue;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return value;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 设置配置值
|
|
265
|
+
* @param {string} key - 配置键
|
|
266
|
+
* @param {*} value - 配置值
|
|
267
|
+
*/
|
|
268
|
+
set(key, value) {
|
|
269
|
+
const keys = key.split('.');
|
|
270
|
+
let target = this.config;
|
|
271
|
+
|
|
272
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
273
|
+
const k = keys[i];
|
|
274
|
+
if (!(k in target) || typeof target[k] !== 'object') {
|
|
275
|
+
target[k] = {};
|
|
276
|
+
}
|
|
277
|
+
target = target[k];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
target[keys[keys.length - 1]] = value;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* 获取完整配置
|
|
285
|
+
* @returns {Object} 完整配置对象
|
|
286
|
+
*/
|
|
287
|
+
getAll() {
|
|
288
|
+
return { ...this.config };
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* 获取常量
|
|
293
|
+
* @returns {Object} 常量对象
|
|
294
|
+
*/
|
|
295
|
+
getConstants() {
|
|
296
|
+
return { ...CONFIG_CONSTANTS };
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 创建单例实例
|
|
301
|
+
let configManagerInstance = null;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 获取配置管理器实例
|
|
305
|
+
* @param {Object} userConfig - 用户配置
|
|
306
|
+
* @returns {ConfigManager} 配置管理器实例
|
|
307
|
+
*/
|
|
308
|
+
function getConfigManager(userConfig = null) {
|
|
309
|
+
if (userConfig) {
|
|
310
|
+
configManagerInstance = new ConfigManager(userConfig);
|
|
311
|
+
}
|
|
312
|
+
return configManagerInstance;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// 测试用:重置单例
|
|
316
|
+
function _resetConfigManager() {
|
|
317
|
+
configManagerInstance = null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
module.exports = {
|
|
321
|
+
CONFIG_CONSTANTS,
|
|
322
|
+
DEFAULT_CONFIG,
|
|
323
|
+
ConfigManager,
|
|
324
|
+
getConfigManager,
|
|
325
|
+
_resetConfigManager
|
|
326
|
+
};
|