oak-backend-base 4.1.27 → 4.1.28

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.
@@ -19,6 +19,7 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
19
19
  private watcherTimerId?;
20
20
  private scheduledJobs;
21
21
  private internalErrorHandlers;
22
+ private watcherExecutingData;
22
23
  regAllExceptionHandler(): void;
23
24
  /**
24
25
  * 注册一个内部错误处理器
@@ -32,9 +33,15 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
32
33
  private requireSth;
33
34
  protected makeContext(cxtStr?: string, headers?: IncomingHttpHeaders): Promise<Cxt>;
34
35
  /**
35
- * 后台启动的configuration,统一放在这里读取
36
+ * 获取数据库配置
37
+ * @returns 读取数据库配置
36
38
  */
37
- private getConfiguration;
39
+ private getDbConfig;
40
+ /**
41
+ * 获取同步配置
42
+ * @returns 读取同步配置
43
+ */
44
+ private getSyncConfig;
38
45
  constructor(path: string, nsSubscribe?: Namespace, nsSocket?: Namespace, nsServer?: Namespace);
39
46
  protected registerTrigger(trigger: Trigger<ED, keyof ED, Cxt>): void;
40
47
  protected initTriggers(): void;
@@ -55,6 +62,30 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
55
62
  }>][];
56
63
  protected operateInWatcher<T extends keyof ED>(entity: T, operation: ED[T]['Update'], context: Cxt, singleton?: true): Promise<OperationResult<ED>>;
57
64
  protected selectInWatcher<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, forUpdate?: true, singleton?: true): Promise<Partial<ED[T]["Schema"]>[]>;
65
+ /**
66
+ * 检查某个数据是否正在被watcher执行
67
+ * @param name watcher名称
68
+ * @param dataId 数据ID
69
+ * @returns 如果没有正在执行则返回true,否则返回false
70
+ */
71
+ private checkDataExecuting;
72
+ /**
73
+ * 过滤出未在执行中的数据行,并标记为执行中
74
+ * @returns [过滤后的行, 是否有行被跳过]
75
+ */
76
+ private filterAndMarkExecutingRows;
77
+ /**
78
+ * 清理执行标记
79
+ */
80
+ private cleanupExecutingMarks;
81
+ /**
82
+ * 解析 filter 和 projection(支持函数或静态值)
83
+ */
84
+ private resolveFilterAndProjection;
85
+ /**
86
+ * 执行 WB 类型 watcher 的查询操作
87
+ */
88
+ private selectForWBWatcher;
58
89
  protected execWatcher(watcher: Watcher<ED, keyof ED, Cxt>): Promise<OperationResult<ED> | undefined>;
59
90
  protected getCheckpointTs(): number;
60
91
  protected checkpoint(): Promise<number>;
package/lib/AppLoader.js CHANGED
@@ -30,6 +30,7 @@ class AppLoader extends types_1.AppLoader {
30
30
  watcherTimerId;
31
31
  scheduledJobs = {};
32
32
  internalErrorHandlers = new Array();
33
+ watcherExecutingData = new Map();
33
34
  regAllExceptionHandler() {
34
35
  const handlers = this.requireSth('lib/configuration/exception');
35
36
  if (Array.isArray(handlers)) {
@@ -92,20 +93,24 @@ class AppLoader extends types_1.AppLoader {
92
93
  return context;
93
94
  }
94
95
  /**
95
- * 后台启动的configuration,统一放在这里读取
96
+ * 获取数据库配置
97
+ * @returns 读取数据库配置
96
98
  */
97
- getConfiguration() {
98
- const dbConfig = (0, dbPriority_1.getDbConfig)(this.path);
99
+ getDbConfig() {
100
+ return (0, dbPriority_1.getDbConfig)(this.path);
101
+ }
102
+ /**
103
+ * 获取同步配置
104
+ * @returns 读取同步配置
105
+ */
106
+ getSyncConfig() {
99
107
  const syncConfigFile = (0, path_1.join)(this.path, 'lib', 'configuration', 'sync.js');
100
108
  const syncConfigs = (0, fs_1.existsSync)(syncConfigFile) && require(syncConfigFile).default;
101
- return {
102
- dbConfig: dbConfig,
103
- syncConfig: syncConfigs,
104
- };
109
+ return syncConfigs;
105
110
  }
106
111
  constructor(path, nsSubscribe, nsSocket, nsServer) {
107
112
  super(path);
108
- const { dbConfig } = this.getConfiguration();
113
+ const dbConfig = this.getDbConfig();
109
114
  const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
110
115
  const depGraph = (0, dependencyBuilder_1.analyzeDepedency)(process.cwd());
111
116
  this.externalDependencies = depGraph.ascOrder;
@@ -173,7 +178,7 @@ class AppLoader extends types_1.AppLoader {
173
178
  async mount(initialize) {
174
179
  const { path } = this;
175
180
  if (!initialize) {
176
- const { syncConfig: syncConfig } = this.getConfiguration();
181
+ const syncConfig = this.getSyncConfig();
177
182
  if (syncConfig) {
178
183
  this.synchronizer = new Synchronizer_1.default(syncConfig, this.dbStore.getSchema(), () => this.contextBuilder(this.dbStore));
179
184
  }
@@ -182,7 +187,7 @@ class AppLoader extends types_1.AppLoader {
182
187
  }
183
188
  const { importations, exportations } = require(`${path}/lib/ports/index`);
184
189
  (0, index_1.registerPorts)(importations || [], exportations || []);
185
- this.dbStore.connect();
190
+ return this.dbStore.connect();
186
191
  }
187
192
  async unmount() {
188
193
  if (this.watcherTimerId) {
@@ -368,60 +373,172 @@ class AppLoader extends types_1.AppLoader {
368
373
  forUpdate,
369
374
  });
370
375
  }
376
+ /**
377
+ * 检查某个数据是否正在被watcher执行
378
+ * @param name watcher名称
379
+ * @param dataId 数据ID
380
+ * @returns 如果没有正在执行则返回true,否则返回false
381
+ */
382
+ checkDataExecuting(name, dataId) {
383
+ let dataSet = this.watcherExecutingData.get(name);
384
+ if (!dataSet) {
385
+ dataSet = new Map();
386
+ this.watcherExecutingData.set(name, dataSet);
387
+ }
388
+ const existingTs = dataSet.get(dataId);
389
+ if (existingTs) {
390
+ const now = Date.now();
391
+ if (now - existingTs > 10 * 60 * 1000) {
392
+ console.error(`检测到执行器【${name}】的数据【${dataId}】标记超时(${Math.floor((now - existingTs) / 1000)}秒),请立即检查是否存在死循环或长时间阻塞的操作`);
393
+ }
394
+ return false;
395
+ }
396
+ dataSet.set(dataId, Date.now());
397
+ return true;
398
+ }
399
+ /**
400
+ * 过滤出未在执行中的数据行,并标记为执行中
401
+ * @returns [过滤后的行, 是否有行被跳过]
402
+ */
403
+ filterAndMarkExecutingRows(watcher, rows) {
404
+ if (watcher.exclusive !== true) {
405
+ // 不需要排他执行,直接返回所有行
406
+ return [rows, false];
407
+ }
408
+ const rowsWithoutExecuting = [];
409
+ let hasSkiped = false;
410
+ const watcherName = watcher.name;
411
+ for (const row of rows) {
412
+ if (!row.id) {
413
+ console.error(`实例【${process.env.OAK_INSTANCE_ID || '单机'}】执行器【${watcherName}】获取的数据没有ID,跳过此数据的并发检查处理:`, row);
414
+ rowsWithoutExecuting.push(row);
415
+ continue;
416
+ }
417
+ if (this.checkDataExecuting(watcherName, row.id)) {
418
+ rowsWithoutExecuting.push(row);
419
+ }
420
+ else {
421
+ hasSkiped = true;
422
+ console.warn(`实例【${process.env.OAK_INSTANCE_ID || '单机'}】执行器【${watcherName}】将跳过正在被执行的数据ID:【${row.id}】,请检查是否执行超时`);
423
+ }
424
+ }
425
+ return [rowsWithoutExecuting, hasSkiped];
426
+ }
427
+ /**
428
+ * 清理执行标记
429
+ */
430
+ cleanupExecutingMarks(watcherName, rows) {
431
+ for (const row of rows) {
432
+ if (row.id) {
433
+ const dataSet = this.watcherExecutingData.get(watcherName);
434
+ if (dataSet) {
435
+ dataSet.delete(row.id);
436
+ }
437
+ }
438
+ }
439
+ }
440
+ /**
441
+ * 解析 filter 和 projection(支持函数或静态值)
442
+ */
443
+ async resolveFilterAndProjection(filter, projection) {
444
+ const filter2 = typeof filter === 'function' ? await filter() : (0, lodash_1.cloneDeep)(filter);
445
+ const projection2 = typeof projection === 'function' ? await projection() : (0, lodash_1.cloneDeep)(projection);
446
+ return [filter2, projection2];
447
+ }
448
+ /**
449
+ * 执行 WB 类型 watcher 的查询操作
450
+ */
451
+ async selectForWBWatcher(watcher, context) {
452
+ const { entity, projection, filter, singleton, forUpdate } = watcher;
453
+ const [filter2, projection2] = await this.resolveFilterAndProjection(filter, projection);
454
+ return await this.selectInWatcher(entity, {
455
+ data: projection2,
456
+ filter: filter2,
457
+ }, context, forUpdate, singleton);
458
+ }
371
459
  async execWatcher(watcher) {
372
460
  let result;
373
- if (watcher.hasOwnProperty('type') && watcher.type === 'free') {
374
- const selectContext = await this.makeContext();
375
- const { entity, projection, fn, filter, singleton, forUpdate } = watcher;
376
- const filter2 = typeof filter === 'function' ? await filter() : (0, lodash_1.cloneDeep)(filter);
377
- const projection2 = typeof projection === 'function' ? await projection() : (0, lodash_1.cloneDeep)(projection);
378
- const rows = await this.selectInWatcher(entity, {
379
- data: projection2,
380
- filter: filter2,
381
- }, selectContext, forUpdate, singleton);
382
- if (rows.length > 0) {
383
- result = await fn(() => this.makeContext(), rows);
461
+ // BBWatcher:直接操作,无需查询
462
+ if (watcher.hasOwnProperty('actionData')) {
463
+ // 如果配置了 exclusive,BBWatcher 不支持
464
+ if (watcher.exclusive === true) {
465
+ console.warn(`BBWatcher【${watcher.name}】配置了 exclusive=true,但 BBWatcher 不支持排他执行,将忽略此配置,请使用 WBWatcher WBFreeWatcher 来实现排他执行`);
384
466
  }
385
- return result;
386
- }
387
- const context = await this.makeContext();
388
- try {
389
- if (watcher.hasOwnProperty('actionData')) {
467
+ const context = await this.makeContext();
468
+ try {
390
469
  const { entity, action, filter, actionData, singleton } = watcher;
391
470
  const filter2 = typeof filter === 'function' ? await filter() : (0, lodash_1.cloneDeep)(filter);
392
- const data = typeof actionData === 'function' ? await (actionData)() : (0, lodash_1.cloneDeep)(actionData);
471
+ const data = typeof actionData === 'function' ? await actionData() : (0, lodash_1.cloneDeep)(actionData);
393
472
  result = await this.operateInWatcher(entity, {
394
473
  id: await (0, uuid_1.generateNewIdAsync)(),
395
474
  action: action,
396
475
  data,
397
476
  filter: filter2,
398
477
  }, context, singleton);
478
+ await context.commit();
479
+ return result;
399
480
  }
400
- else {
401
- const { entity, projection, fn, filter, singleton, forUpdate } = watcher;
402
- const filter2 = typeof filter === 'function' ? await filter() : (0, lodash_1.cloneDeep)(filter);
403
- const projection2 = typeof projection === 'function' ? await projection() : (0, lodash_1.cloneDeep)(projection);
404
- const rows = await this.selectInWatcher(entity, {
405
- data: projection2,
406
- filter: filter2,
407
- }, context, forUpdate, singleton);
408
- if (rows.length > 0) {
409
- result = await fn(context, rows);
481
+ catch (err) {
482
+ if (err instanceof types_1.OakPartialSuccess) {
483
+ await context.commit();
484
+ }
485
+ else {
486
+ await context.rollback();
410
487
  }
488
+ throw err;
489
+ }
490
+ }
491
+ // WBFreeWatcher 和 WBWatcher:查询后执行
492
+ const isFreeType = watcher.hasOwnProperty('type') &&
493
+ watcher.type === 'free';
494
+ // 1. 执行查询(WBFreeWatcher 使用独立 context)
495
+ const selectContext = isFreeType ? await this.makeContext() : await this.makeContext();
496
+ const rows = await this.selectForWBWatcher(watcher, selectContext);
497
+ if (isFreeType) {
498
+ await selectContext.commit();
499
+ }
500
+ // 2. 并发检查:过滤出未在执行中的数据
501
+ const [rowsWithoutExecuting, hasSkipped] = this.filterAndMarkExecutingRows(watcher, rows);
502
+ if (rowsWithoutExecuting.length === 0) {
503
+ if (!isFreeType) {
504
+ await selectContext.commit();
505
+ }
506
+ // 全部行都被跳过,直接返回,实际没有执行任何操作,hasSkipped也是其他执行流在处理
507
+ // this.cleanupExecutingMarks(watcher.name, hasSkipped);
508
+ if (hasSkipped) {
509
+ console.log(`执行器【${watcher.name}】本次没有可执行的数据行,全部数据行均被跳过`);
411
510
  }
412
- await context.commit();
413
511
  return result;
414
512
  }
415
- catch (err) {
416
- if (err instanceof types_1.OakPartialSuccess) {
417
- await context.commit();
513
+ // 3. 执行业务逻辑
514
+ try {
515
+ if (isFreeType) {
516
+ const { fn } = watcher;
517
+ result = await fn(() => this.makeContext(), rowsWithoutExecuting);
418
518
  }
419
519
  else {
420
- await context.rollback();
520
+ const { fn } = watcher;
521
+ result = await fn(selectContext, rowsWithoutExecuting);
522
+ await selectContext.commit();
523
+ }
524
+ return result;
525
+ }
526
+ catch (err) {
527
+ if (!isFreeType) {
528
+ if (err instanceof types_1.OakPartialSuccess) {
529
+ await selectContext.commit();
530
+ }
531
+ else {
532
+ await selectContext.rollback();
533
+ }
421
534
  }
422
- // 不能在这里publish,因为这个方法可能是在timer中调用,也可能是在routine中调用
423
535
  throw err;
424
536
  }
537
+ finally {
538
+ // 清理执行标记
539
+ this.cleanupExecutingMarks(watcher.name, rowsWithoutExecuting);
540
+ // 这里只需要清理被执行的行,因为被跳过的行本来就不是这一次执行被占用的。
541
+ }
425
542
  }
426
543
  getCheckpointTs() {
427
544
  const now = Date.now();
package/lib/DbStore.d.ts CHANGED
@@ -2,6 +2,7 @@ import { DbConfiguration } from 'oak-db/src/types/configuration';
2
2
  import { EntityDict, StorageSchema, Trigger, Checker, SelectFreeEntities, UpdateFreeDict, AuthDeduceRelationMap, VolatileTrigger, OperateOption } from 'oak-domain/lib/types';
3
3
  import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
4
4
  import { BackendRuntimeContext } from 'oak-frontend-base/lib/context/BackendRuntimeContext';
5
+ import { DbTypeSymbol } from './utils/dbPriority';
5
6
  import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
6
7
  import { DbStore } from 'oak-db/lib/types/dbStore';
7
8
  export type TriggerStore<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>> = {
@@ -13,4 +14,6 @@ export type TriggerStore<ED extends EntityDict & BaseEntityDict, Cxt extends Bac
13
14
  independentCheckPoint(name: string, ts: number, instanceCount?: number, instanceId?: number): Promise<number>;
14
15
  };
15
16
  export type AppDbStore<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>> = DbStore<ED, Cxt> & CascadeStore<ED> & TriggerStore<ED, Cxt>;
16
- export declare const createDbStore: <ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(storageSchema: StorageSchema<ED>, contextBuilder: () => Cxt, dbConfiguration: DbConfiguration, authDeduceRelationMap: AuthDeduceRelationMap<ED>, selectFreeEntities?: SelectFreeEntities<ED>, updateFreeDict?: UpdateFreeDict<ED>, onVolatileTrigger?: <T extends keyof ED>(entity: T, trigger: VolatileTrigger<ED, T, Cxt>, ids: string[], cxtStr: string, option: OperateOption) => Promise<void>) => AppDbStore<ED, Cxt>;
17
+ export declare const createDbStore: <ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(storageSchema: StorageSchema<ED>, contextBuilder: () => Cxt, dbConfiguration: DbConfiguration & {
18
+ [DbTypeSymbol]?: string;
19
+ }, authDeduceRelationMap: AuthDeduceRelationMap<ED>, selectFreeEntities?: SelectFreeEntities<ED>, updateFreeDict?: UpdateFreeDict<ED>, onVolatileTrigger?: <T extends keyof ED>(entity: T, trigger: VolatileTrigger<ED, T, Cxt>, ids: string[], cxtStr: string, option: OperateOption) => Promise<void>) => AppDbStore<ED, Cxt>;
package/lib/DbStore.js CHANGED
@@ -5,8 +5,10 @@ const TriggerExecutor_1 = require("oak-domain/lib/store/TriggerExecutor");
5
5
  const RelationAuth_1 = require("oak-domain/lib/store/RelationAuth");
6
6
  const dbPriority_1 = require("./utils/dbPriority");
7
7
  const createDbStore = (storageSchema, contextBuilder, dbConfiguration, authDeduceRelationMap, selectFreeEntities = [], updateFreeDict = {}, onVolatileTrigger) => {
8
- const BaseStoreClass = (0, dbPriority_1.getDbStoreClass)();
8
+ // TODO: 这里的类型检查会过不去,因为ts不知道上层已经实现这个抽象类。
9
+ const BaseStoreClass = (0, dbPriority_1.getDbStoreClass)(dbConfiguration);
9
10
  // 动态创建继承类
11
+ // @ts-ignore
10
12
  class DynamicDbStore extends BaseStoreClass {
11
13
  executor;
12
14
  relationAuth;
@@ -1,10 +1,10 @@
1
1
  import { MysqlStore, PostgreSQLStore } from "oak-db";
2
2
  import { DbConfiguration } from "oak-db/src/types/configuration";
3
- import { AsyncRowStore } from "oak-domain/lib/store/AsyncRowStore";
4
3
  import { EntityDict, StorageSchema } from 'oak-domain/lib/types';
5
4
  import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
6
5
  import { BackendRuntimeContext } from 'oak-frontend-base/lib/context/BackendRuntimeContext';
7
6
  import { CascadeStore } from "oak-domain/lib/store/CascadeStore";
7
+ import { DbStore } from "oak-db/lib/types/dbStore";
8
8
  /**
9
9
  * 数据库优先级列表,按顺序尝试获取配置文件
10
10
  */
@@ -12,5 +12,10 @@ export declare const dbList: {
12
12
  mysql: typeof MysqlStore;
13
13
  postgres: typeof PostgreSQLStore;
14
14
  };
15
- export declare const getDbConfig: (path: string) => DbConfiguration;
16
- export declare const getDbStoreClass: <ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>() => new (schema: StorageSchema<ED>, config: DbConfiguration) => AsyncRowStore<ED, Cxt> & CascadeStore<ED>;
15
+ export declare const DbTypeSymbol: unique symbol;
16
+ export declare const getDbConfig: (path: string) => DbConfiguration & {
17
+ [DbTypeSymbol]: string;
18
+ };
19
+ export declare const getDbStoreClass: <ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(config: {
20
+ [DbTypeSymbol]?: string;
21
+ }) => new (schema: StorageSchema<ED>, config: DbConfiguration) => DbStore<ED, Cxt> & CascadeStore<ED>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDbStoreClass = exports.getDbConfig = exports.dbList = void 0;
3
+ exports.getDbStoreClass = exports.getDbConfig = exports.DbTypeSymbol = exports.dbList = void 0;
4
4
  const oak_db_1 = require("oak-db");
5
5
  const path_1 = require("path");
6
6
  const fs_1 = require("fs");
@@ -11,7 +11,7 @@ exports.dbList = {
11
11
  mysql: oak_db_1.MysqlStore,
12
12
  postgres: oak_db_1.PostgreSQLStore
13
13
  };
14
- let usedDbType = null;
14
+ exports.DbTypeSymbol = Symbol.for('oak:backend:db:type');
15
15
  const getDbConfig = (path) => {
16
16
  for (const db of Object.keys(exports.dbList)) {
17
17
  try {
@@ -25,8 +25,14 @@ const getDbConfig = (path) => {
25
25
  }
26
26
  const config = require(dbConfigFile);
27
27
  console.log(`使用${db}作为数据库`);
28
- usedDbType = db;
29
- return Object.assign({}, config);
28
+ // 定义不可枚举的属性,避免被序列化
29
+ Object.defineProperty(config, exports.DbTypeSymbol, {
30
+ value: db,
31
+ enumerable: false,
32
+ writable: false,
33
+ configurable: false
34
+ });
35
+ return config;
30
36
  }
31
37
  catch (err) {
32
38
  // do nothing
@@ -35,10 +41,11 @@ const getDbConfig = (path) => {
35
41
  throw new Error(`没有找到数据库配置文件,请在configuration目录下添加任一配置文件:${Object.keys(exports.dbList).map(ele => `${ele}.json`).join('、')}`);
36
42
  };
37
43
  exports.getDbConfig = getDbConfig;
38
- const getDbStoreClass = () => {
39
- const dbType = usedDbType || (() => {
40
- throw new Error('无法确定数据库类型');
41
- })();
44
+ const getDbStoreClass = (config) => {
45
+ const dbType = Object.getOwnPropertyDescriptor(config, exports.DbTypeSymbol)?.value;
46
+ if (!dbType) {
47
+ throw new Error('无法获取数据库类型,请确认是否存在数据库配置文件');
48
+ }
42
49
  const DbStoreClass = exports.dbList[dbType.toLowerCase()];
43
50
  if (!DbStoreClass) {
44
51
  throw new Error(`不支持的数据库类型:${dbType},请确认是否存在以下配置文件:${Object.keys(exports.dbList).map(ele => `${ele}.json`).join('、')}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-backend-base",
3
- "version": "4.1.27",
3
+ "version": "4.1.28",
4
4
  "description": "oak-backend-base",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -23,7 +23,7 @@
23
23
  "node-schedule": "^2.1.0",
24
24
  "oak-common-aspect": "^3.0.5",
25
25
  "oak-db": "^3.3.13",
26
- "oak-domain": "^5.1.34",
26
+ "oak-domain": "^5.1.35",
27
27
  "oak-frontend-base": "^5.3.45",
28
28
  "socket.io": "^4.8.1",
29
29
  "socket.io-client": "^4.7.2",