oak-backend-base 4.1.23 → 4.1.24
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/lib/AppLoader.d.ts +12 -0
- package/lib/AppLoader.js +52 -0
- package/lib/types/index.d.ts +9 -0
- package/lib/types/index.js +2 -0
- package/package.json +3 -3
package/lib/AppLoader.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { Namespace } from 'socket.io';
|
|
|
7
7
|
import DataSubscriber from './cluster/DataSubscriber';
|
|
8
8
|
import Synchronizer from './Synchronizer';
|
|
9
9
|
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
|
|
10
|
+
import { InternalErrorHandler } from './types';
|
|
10
11
|
export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>> extends GeneralAppLoader<ED, Cxt> {
|
|
11
12
|
protected dbStore: DbStore<ED, Cxt>;
|
|
12
13
|
private aspectDict;
|
|
@@ -17,6 +18,17 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
|
|
|
17
18
|
private nsSocket?;
|
|
18
19
|
private watcherTimerId?;
|
|
19
20
|
private scheduledJobs;
|
|
21
|
+
private internalErrorHandlers;
|
|
22
|
+
regAllExceptionHandler(): void;
|
|
23
|
+
/**
|
|
24
|
+
* 注册一个内部错误处理器
|
|
25
|
+
* @param handler 内部错误处理器
|
|
26
|
+
*/
|
|
27
|
+
registerInternalErrorHandler(handler: InternalErrorHandler<ED, Cxt>): void;
|
|
28
|
+
/**
|
|
29
|
+
* 发布内部错误事件给注册的处理器
|
|
30
|
+
*/
|
|
31
|
+
private publishInternalError;
|
|
20
32
|
private requireSth;
|
|
21
33
|
protected makeContext(cxtStr?: string, headers?: IncomingHttpHeaders): Promise<Cxt>;
|
|
22
34
|
/**
|
package/lib/AppLoader.js
CHANGED
|
@@ -28,6 +28,51 @@ class AppLoader extends types_1.AppLoader {
|
|
|
28
28
|
nsSocket;
|
|
29
29
|
watcherTimerId;
|
|
30
30
|
scheduledJobs = {};
|
|
31
|
+
internalErrorHandlers = new Array();
|
|
32
|
+
regAllExceptionHandler() {
|
|
33
|
+
const handlers = this.requireSth('lib/configuration/exception');
|
|
34
|
+
if (Array.isArray(handlers)) {
|
|
35
|
+
handlers.forEach((handler) => {
|
|
36
|
+
console.log(`注册内部错误处理器: ${handler.name}`);
|
|
37
|
+
this.registerInternalErrorHandler(handler);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
console.warn('lib/configuration/exception必须默认导出一个处理器数组,当前导出类型不正确,将忽略此配置');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 注册一个内部错误处理器
|
|
46
|
+
* @param handler 内部错误处理器
|
|
47
|
+
*/
|
|
48
|
+
registerInternalErrorHandler(handler) {
|
|
49
|
+
// 检查有没有名称重复
|
|
50
|
+
if (this.internalErrorHandlers.find(h => h.name === handler.name)) {
|
|
51
|
+
throw new Error(`内部错误处理器名称重复: ${handler.name}`);
|
|
52
|
+
}
|
|
53
|
+
this.internalErrorHandlers.push(handler);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 发布内部错误事件给注册的处理器
|
|
57
|
+
*/
|
|
58
|
+
async publishInternalError(type, message, err) {
|
|
59
|
+
const errorToPublish = (0, lodash_1.cloneDeep)(err);
|
|
60
|
+
await Promise.all(this.internalErrorHandlers.map((handler) => {
|
|
61
|
+
return new Promise(async (resolve) => {
|
|
62
|
+
try {
|
|
63
|
+
const ctx = await this.makeContext();
|
|
64
|
+
console.log(`调用internalErrorHandler【${handler.name}】处理内部错误事件`);
|
|
65
|
+
handler.handle(ctx, type, message, errorToPublish);
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
console.error('执行internalErrorHandler时出错', e);
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
resolve();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
31
76
|
requireSth(filePath) {
|
|
32
77
|
return (0, requirePrj_1.default)(this.path, filePath, this.externalDependencies);
|
|
33
78
|
}
|
|
@@ -172,7 +217,9 @@ class AppLoader extends types_1.AppLoader {
|
|
|
172
217
|
};
|
|
173
218
|
}
|
|
174
219
|
catch (err) {
|
|
220
|
+
console.error(`执行aspect「${name}」出错`, err);
|
|
175
221
|
await context.rollback();
|
|
222
|
+
this.publishInternalError(`aspect`, `执行aspect「${name}」出错`, err);
|
|
176
223
|
if (err instanceof types_1.OakException) {
|
|
177
224
|
throw err;
|
|
178
225
|
}
|
|
@@ -357,6 +404,7 @@ class AppLoader extends types_1.AppLoader {
|
|
|
357
404
|
else {
|
|
358
405
|
await context.rollback();
|
|
359
406
|
}
|
|
407
|
+
// 不能在这里publish,因为这个方法可能是在timer中调用,也可能是在routine中调用
|
|
360
408
|
throw err;
|
|
361
409
|
}
|
|
362
410
|
}
|
|
@@ -382,6 +430,7 @@ class AppLoader extends types_1.AppLoader {
|
|
|
382
430
|
}
|
|
383
431
|
catch (err) {
|
|
384
432
|
console.error(`执行watcher【${watcher.name}】失败,耗时【${Date.now() - start}】,结果是:`, err);
|
|
433
|
+
await this.publishInternalError(`watcher`, `执行watcher【${watcher.name}】失败`, err);
|
|
385
434
|
}
|
|
386
435
|
};
|
|
387
436
|
const doWatchers = async () => {
|
|
@@ -400,6 +449,7 @@ class AppLoader extends types_1.AppLoader {
|
|
|
400
449
|
}
|
|
401
450
|
catch (err) {
|
|
402
451
|
console.error(`执行了checkpoint,发生错误:`, err);
|
|
452
|
+
await this.publishInternalError(`checkpoint`, `执行checkpoint发生错误`, err);
|
|
403
453
|
}
|
|
404
454
|
this.watcherTimerId = setTimeout(() => doWatchers(), 120000);
|
|
405
455
|
};
|
|
@@ -424,6 +474,7 @@ class AppLoader extends types_1.AppLoader {
|
|
|
424
474
|
}
|
|
425
475
|
catch (err) {
|
|
426
476
|
console.error(`定时器【${name}】执行失败,耗时${Date.now() - start}毫秒】,错误是`, err);
|
|
477
|
+
this.publishInternalError(`timer`, `定时器【${name}】执行失败`, err);
|
|
427
478
|
}
|
|
428
479
|
}
|
|
429
480
|
else {
|
|
@@ -443,6 +494,7 @@ class AppLoader extends types_1.AppLoader {
|
|
|
443
494
|
else {
|
|
444
495
|
await context.rollback();
|
|
445
496
|
}
|
|
497
|
+
this.publishInternalError(`timer`, `定时器【${name}】执行失败`, err);
|
|
446
498
|
}
|
|
447
499
|
}
|
|
448
500
|
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseEntityDict } from "oak-domain";
|
|
2
|
+
import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore";
|
|
3
|
+
import { EntityDict } from "oak-domain/lib/types";
|
|
4
|
+
export type InternalErrorType = 'aspect' | 'trigger' | 'watcher' | 'timer' | 'checkpoint';
|
|
5
|
+
export type InternalErrorHandler<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = {
|
|
6
|
+
name: string;
|
|
7
|
+
handle: (ctx: Cxt, type: InternalErrorType, message: string, err: Error) => Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
export type ExceptionPublisher = (type: string, message: string, err: any) => Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oak-backend-base",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.24",
|
|
4
4
|
"description": "oak-backend-base",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"author": {
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"node-schedule": "^2.1.0",
|
|
24
24
|
"oak-common-aspect": "^3.0.5",
|
|
25
25
|
"oak-db": "^3.3.11",
|
|
26
|
-
"oak-domain": "^5.1.
|
|
27
|
-
"oak-frontend-base": "^5.3.
|
|
26
|
+
"oak-domain": "^5.1.30",
|
|
27
|
+
"oak-frontend-base": "^5.3.43",
|
|
28
28
|
"socket.io": "^4.8.1",
|
|
29
29
|
"socket.io-client": "^4.7.2",
|
|
30
30
|
"uuid": "^8.3.2"
|