@ticatec/common-express-server 0.1.8 → 0.2.0
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.
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import log4js from "log4js";
|
|
2
|
+
export declare enum ProcessStatus {
|
|
3
|
+
Napping = 0,
|
|
4
|
+
Running = 1
|
|
5
|
+
}
|
|
6
|
+
export default abstract class CommonProcessor<T> {
|
|
7
|
+
protected logger: log4js.Logger;
|
|
8
|
+
protected interval: number;
|
|
9
|
+
protected processInterval: any;
|
|
10
|
+
private nappingDuration;
|
|
11
|
+
private status;
|
|
12
|
+
private readonly ants;
|
|
13
|
+
/**
|
|
14
|
+
* 构造函数
|
|
15
|
+
* @param interval 检查间隔
|
|
16
|
+
* @param ants 工蚁数量,可以同步执行的线程数量
|
|
17
|
+
* @protected
|
|
18
|
+
*/
|
|
19
|
+
protected constructor(interval: number, ants?: number);
|
|
20
|
+
/**
|
|
21
|
+
* 启动
|
|
22
|
+
*/
|
|
23
|
+
startup(): void;
|
|
24
|
+
/**
|
|
25
|
+
* 停止
|
|
26
|
+
*/
|
|
27
|
+
stop(): void;
|
|
28
|
+
private checkNap;
|
|
29
|
+
/**
|
|
30
|
+
* 开始处理数据
|
|
31
|
+
* @protected
|
|
32
|
+
*/
|
|
33
|
+
protected startProcess(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* 读取待处理的数据
|
|
36
|
+
* @protected
|
|
37
|
+
*/
|
|
38
|
+
protected abstract loadToProcessData(): Promise<Array<T>>;
|
|
39
|
+
/**
|
|
40
|
+
* 立即执行
|
|
41
|
+
*/
|
|
42
|
+
runImmediately(): void;
|
|
43
|
+
/**
|
|
44
|
+
* 处理单条数据
|
|
45
|
+
* @protected
|
|
46
|
+
* @param item
|
|
47
|
+
*/
|
|
48
|
+
protected abstract processItem(item: T): Promise<void>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ProcessStatus = void 0;
|
|
7
|
+
const node_timers_1 = require("node:timers");
|
|
8
|
+
const log4js_1 = __importDefault(require("log4js"));
|
|
9
|
+
var ProcessStatus;
|
|
10
|
+
(function (ProcessStatus) {
|
|
11
|
+
ProcessStatus[ProcessStatus["Napping"] = 0] = "Napping";
|
|
12
|
+
ProcessStatus[ProcessStatus["Running"] = 1] = "Running";
|
|
13
|
+
})(ProcessStatus || (exports.ProcessStatus = ProcessStatus = {}));
|
|
14
|
+
class CommonProcessor {
|
|
15
|
+
/**
|
|
16
|
+
* 构造函数
|
|
17
|
+
* @param interval 检查间隔
|
|
18
|
+
* @param ants 工蚁数量,可以同步执行的线程数量
|
|
19
|
+
* @protected
|
|
20
|
+
*/
|
|
21
|
+
constructor(interval, ants = 5) {
|
|
22
|
+
this.logger = log4js_1.default.getLogger(this.constructor.name);
|
|
23
|
+
this.interval = Math.max(Math.round(interval), 5);
|
|
24
|
+
this.ants = ants;
|
|
25
|
+
this.nappingDuration = 0;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 启动
|
|
29
|
+
*/
|
|
30
|
+
startup() {
|
|
31
|
+
if (!this.processInterval) {
|
|
32
|
+
this.logger.debug('启动处理器');
|
|
33
|
+
this.nappingDuration = this.interval;
|
|
34
|
+
this.status = ProcessStatus.Napping;
|
|
35
|
+
this.processInterval = setInterval(this.checkNap(), 1000);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 停止
|
|
40
|
+
*/
|
|
41
|
+
stop() {
|
|
42
|
+
if (this.processInterval) {
|
|
43
|
+
this.logger.debug('停止处理器');
|
|
44
|
+
(0, node_timers_1.clearInterval)(this.processInterval);
|
|
45
|
+
this.processInterval = null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
checkNap() {
|
|
49
|
+
return async () => {
|
|
50
|
+
this.nappingDuration++;
|
|
51
|
+
if (this.nappingDuration >= this.interval && this.status == ProcessStatus.Napping) {
|
|
52
|
+
this.status = ProcessStatus.Running;
|
|
53
|
+
try {
|
|
54
|
+
await this.startProcess();
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
this.status = ProcessStatus.Napping;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 开始处理数据
|
|
64
|
+
* @protected
|
|
65
|
+
*/
|
|
66
|
+
async startProcess() {
|
|
67
|
+
let arr = await this.loadToProcessData();
|
|
68
|
+
if (arr.length > 0) {
|
|
69
|
+
this.logger.debug('有待处理的数据,开始处理数据');
|
|
70
|
+
const pool = new Set();
|
|
71
|
+
for (const item of arr) {
|
|
72
|
+
const task = this.processItem(item)
|
|
73
|
+
.catch((ex) => {
|
|
74
|
+
this.logger.error(`执行任务 [${item}] 发生错误:`, ex);
|
|
75
|
+
})
|
|
76
|
+
.finally(() => {
|
|
77
|
+
pool.delete(task);
|
|
78
|
+
});
|
|
79
|
+
pool.add(task);
|
|
80
|
+
if (pool.size >= this.ants) {
|
|
81
|
+
await Promise.race(pool);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
await Promise.all(pool);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
this.nappingDuration = 0;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 立即执行
|
|
92
|
+
*/
|
|
93
|
+
runImmediately() {
|
|
94
|
+
this.logger.debug('立即执行');
|
|
95
|
+
this.nappingDuration = this.interval;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.default = CommonProcessor;
|
|
99
|
+
//# sourceMappingURL=CommonProcessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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;YACL,CAAC;YACD,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,eAAe,GAAG,CAAC,CAAC;QAC7B,CAAC;IACL,CAAC;IASD;;OAEG;IACH,cAAc;QACV,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC1B,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,QAAQ,CAAC;IACzC,CAAC;CAQJ;AA5GD,kCA4GC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import CommonProcessor from "./CommonProcessor";
|
|
2
|
+
export default class ProcessorManager {
|
|
3
|
+
private static instance;
|
|
4
|
+
private map;
|
|
5
|
+
private constructor();
|
|
6
|
+
static getInstance(): ProcessorManager;
|
|
7
|
+
get(name: string): CommonProcessor<any>;
|
|
8
|
+
/**
|
|
9
|
+
* 组成一个处理器
|
|
10
|
+
* @param Constructor
|
|
11
|
+
* @param args
|
|
12
|
+
*/
|
|
13
|
+
register(Constructor: new (name: string) => CommonProcessor<any>, args?: any): CommonProcessor<any>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ProcessorManager {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.map = new Map();
|
|
6
|
+
}
|
|
7
|
+
static getInstance() {
|
|
8
|
+
return ProcessorManager.instance;
|
|
9
|
+
}
|
|
10
|
+
get(name) {
|
|
11
|
+
return this.map.get(name);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 组成一个处理器
|
|
15
|
+
* @param Constructor
|
|
16
|
+
* @param args
|
|
17
|
+
*/
|
|
18
|
+
register(Constructor, args) {
|
|
19
|
+
let constructorName = Constructor.name;
|
|
20
|
+
let processor = this.map.get(constructorName);
|
|
21
|
+
if (!processor) {
|
|
22
|
+
processor = new Constructor(args);
|
|
23
|
+
this.map.set(constructorName, processor);
|
|
24
|
+
}
|
|
25
|
+
return processor;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
ProcessorManager.instance = new ProcessorManager();
|
|
29
|
+
exports.default = ProcessorManager;
|
|
30
|
+
//# sourceMappingURL=ProcessorManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProcessorManager.js","sourceRoot":"src/","sources":["lib/ProcessorManager.ts"],"names":[],"mappings":";;AAEA,MAAqB,gBAAgB;IAKjC;QACI,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,EAAgC,CAAC;IACvD,CAAC;IAED,MAAM,CAAC,WAAW;QACd,OAAO,gBAAgB,CAAC,QAAQ,CAAC;IACrC,CAAC;IAED,GAAG,CAAC,IAAY;QACZ,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,QAAQ,CAAC,WAAuD,EAAE,IAAU;QACxE,IAAI,eAAe,GAAG,WAAW,CAAC,IAAI,CAAC;QACvC,IAAI,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;QAC9C,IAAI,CAAC,SAAS,EAAE,CAAC;YACb,SAAS,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,CAAA;YACjC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,SAAS,CAAC;IACrB,CAAC;;AA5Bc,yBAAQ,GAAqB,IAAI,gBAAgB,EAAE,CAAC;kBAFlD,gBAAgB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ticatec/common-express-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A comprehensive TypeScript library providing common classes, controllers, and middleware for building scalable Express.js applications with multi-tenant support.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|