oak-backend-base 4.1.10 → 4.1.12

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.
@@ -16,6 +16,8 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
16
16
  protected synchronizer?: Synchronizer<ED, Cxt>;
17
17
  protected contextBuilder: (store: DbStore<ED, Cxt>) => Cxt;
18
18
  private nsSocket?;
19
+ private watcherTimerId?;
20
+ private scheduledJobs;
19
21
  private requireSth;
20
22
  protected makeContext(cxtStr?: string, headers?: IncomingHttpHeaders): Promise<Cxt>;
21
23
  /**
package/lib/AppLoader.js CHANGED
@@ -24,6 +24,8 @@ class AppLoader extends types_1.AppLoader {
24
24
  synchronizer;
25
25
  contextBuilder;
26
26
  nsSocket;
27
+ watcherTimerId;
28
+ scheduledJobs = {};
27
29
  requireSth(filePath) {
28
30
  const depFilePath = (0, path_1.join)(this.path, filePath);
29
31
  let sth;
@@ -149,8 +151,18 @@ class AppLoader extends types_1.AppLoader {
149
151
  this.dbStore.connect();
150
152
  }
151
153
  async unmount() {
152
- (0, index_1.clearPorts)();
153
- this.dbStore.disconnect();
154
+ if (this.watcherTimerId) {
155
+ console.log('取消watcher...');
156
+ clearTimeout(this.watcherTimerId);
157
+ this.watcherTimerId = undefined;
158
+ }
159
+ for (const job in this.scheduledJobs) {
160
+ console.log(`取消定时任务【${job}】...`);
161
+ await this.scheduledJobs[job]?.cancel();
162
+ delete this.scheduledJobs[job];
163
+ }
164
+ await (0, index_1.clearPorts)();
165
+ await this.dbStore.disconnect();
154
166
  }
155
167
  async execAspect(name, headers, contextString, params) {
156
168
  // 从aspect过来的,不能有空cxtString,以防被误判为root
@@ -229,7 +241,7 @@ class AppLoader extends types_1.AppLoader {
229
241
  }
230
242
  }
231
243
  }
232
- this.dbStore.disconnect();
244
+ await this.dbStore.disconnect();
233
245
  }
234
246
  getStore() {
235
247
  return this.dbStore;
@@ -369,7 +381,7 @@ class AppLoader extends types_1.AppLoader {
369
381
  catch (err) {
370
382
  console.error(`执行了checkpoint,发生错误:`, err);
371
383
  }
372
- setTimeout(() => doWatchers(), 120000);
384
+ this.watcherTimerId = setTimeout(() => doWatchers(), 120000);
373
385
  };
374
386
  doWatchers();
375
387
  }
@@ -382,7 +394,7 @@ class AppLoader extends types_1.AppLoader {
382
394
  if (timers) {
383
395
  for (const timer of timers) {
384
396
  const { cron, name } = timer;
385
- (0, node_schedule_1.scheduleJob)(name, cron, async (date) => {
397
+ const job = (0, node_schedule_1.scheduleJob)(name, cron, async (date) => {
386
398
  const start = Date.now();
387
399
  console.log(`定时器【${name}】开始执行,时间是【${date.toLocaleTimeString()}】`);
388
400
  if (timer.hasOwnProperty('entity')) {
@@ -391,7 +403,7 @@ class AppLoader extends types_1.AppLoader {
391
403
  console.log(`定时器【${name}】执行成功,耗时${Date.now() - start}毫秒】,结果是`, result);
392
404
  }
393
405
  catch (err) {
394
- console.log(`定时器【${name}】执行成功,耗时${Date.now() - start}毫秒】,错误是`, err);
406
+ console.warn(`定时器【${name}】执行失败,耗时${Date.now() - start}毫秒】,错误是`, err);
395
407
  }
396
408
  }
397
409
  else {
@@ -414,6 +426,15 @@ class AppLoader extends types_1.AppLoader {
414
426
  }
415
427
  }
416
428
  });
429
+ if (!job) {
430
+ // console.error(`定时器【${name}】创建失败,请检查cron表达式是否正确`);
431
+ throw new Error(`定时器【${name}】创建失败,请检查cron表达式是否正确`);
432
+ }
433
+ if (this.scheduledJobs[name]) {
434
+ // console.error(`定时器【${name}】已经存在,请检查定时器名称是否重复`);
435
+ throw new Error(`定时器【${name}】已经存在,请检查定时器名称是否重复`);
436
+ }
437
+ this.scheduledJobs[name] = job;
417
438
  }
418
439
  }
419
440
  }
@@ -424,7 +445,7 @@ class AppLoader extends types_1.AppLoader {
424
445
  const start = Date.now();
425
446
  try {
426
447
  const result = await this.execWatcher(routine);
427
- console.warn(`例程【${routine.name}】执行成功,耗时${Date.now() - start}毫秒,结果是`, result);
448
+ console.log(`例程【${routine.name}】执行成功,耗时${Date.now() - start}毫秒,结果是`, result);
428
449
  }
429
450
  catch (err) {
430
451
  console.warn(`例程【${routine.name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
@@ -66,7 +66,7 @@ class Synchronizer {
66
66
  contextBuilder;
67
67
  pushAccessMap = {};
68
68
  async startChannel2(context, channel) {
69
- const { queue, api, selfEncryptInfo, entity, entityId, onFailed, timeout } = channel;
69
+ const { queue, api, selfEncryptInfo, entity, entityId, onFailed, timeout = 5000 } = channel;
70
70
  // todo 加密
71
71
  const opers = queue.map(ele => ele.oper);
72
72
  if (process.env.NODE_ENV === 'development') {
@@ -91,7 +91,7 @@ class Synchronizer {
91
91
  [OAK_SYNC_HEADER_SIGN]: signature,
92
92
  },
93
93
  body,
94
- }, timeout || 5000);
94
+ }, timeout);
95
95
  if (res.status !== 200) {
96
96
  throw new Error(`sync数据时,访问api「${finalApi}」的结果不是200。「${res.status}」`);
97
97
  }
@@ -104,7 +104,7 @@ class Synchronizer {
104
104
  if (onFailed) {
105
105
  context.on('rollback', async () => {
106
106
  const context2 = this.contextBuilder();
107
- context2.begin();
107
+ await context2.begin();
108
108
  try {
109
109
  await onFailed({
110
110
  remoteEntity: entity,
@@ -117,10 +117,10 @@ class Synchronizer {
117
117
  })),
118
118
  reason: err,
119
119
  }, context2);
120
- context2.commit();
120
+ await context2.commit();
121
121
  }
122
122
  catch (err) {
123
- context2.rollback();
123
+ await context2.rollback();
124
124
  }
125
125
  });
126
126
  }
@@ -526,7 +526,7 @@ class Synchronizer {
526
526
  this.remotePullInfoMap[entity] = {};
527
527
  }
528
528
  if (!this.remotePullInfoMap[entity][entityId]) {
529
- const { getPullInfo, pullEntities } = this.config.remotes.find(ele => ele.entity === entity);
529
+ const { getPullInfo, pullEntities, clockDriftDuration } = this.config.remotes.find(ele => ele.entity === entity);
530
530
  const pullEntityDict = {};
531
531
  if (pullEntities) {
532
532
  pullEntities.forEach((def) => pullEntityDict[def.entity] = def);
@@ -538,10 +538,11 @@ class Synchronizer {
538
538
  remoteEntityId: entityId,
539
539
  }),
540
540
  pullEntityDict,
541
+ clockDriftDuration,
541
542
  };
542
543
  closeFn();
543
544
  }
544
- const { pullInfo, pullEntityDict } = this.remotePullInfoMap[entity][entityId];
545
+ const { pullInfo, pullEntityDict, clockDriftDuration } = this.remotePullInfoMap[entity][entityId];
545
546
  const { userId, algorithm, publicKey, cxtInfo } = pullInfo;
546
547
  (0, assert_1.default)(userId);
547
548
  context.setCurrentUserId(userId);
@@ -549,8 +550,10 @@ class Synchronizer {
549
550
  await context.initialize(cxtInfo);
550
551
  }
551
552
  const syncTimestamp = parseInt(syncTs, 10);
552
- if (!(Date.now() - syncTimestamp < 10000)) {
553
- throw new Error('同步时钟漂移过长');
553
+ if (clockDriftDuration !== 0) {
554
+ if (!(Date.now() - syncTimestamp < (clockDriftDuration || 10000))) {
555
+ throw new types_1.OakClockDriftException('同步时钟漂移过长');
556
+ }
554
557
  }
555
558
  if (!verify(publicKey, JSON.stringify(body), syncTs, syncNonce, syncSign)) {
556
559
  throw new Error('sync验签失败');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-backend-base",
3
- "version": "4.1.10",
3
+ "version": "4.1.12",
4
4
  "description": "oak-backend-base",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -21,9 +21,9 @@
21
21
  "mysql2": "^2.3.3",
22
22
  "node-schedule": "^2.1.0",
23
23
  "oak-common-aspect": "^3.0.4",
24
- "oak-db": "^3.3.2",
25
- "oak-domain": "^5.1.10",
26
- "oak-frontend-base": "^5.3.19",
24
+ "oak-db": "^3.3.4",
25
+ "oak-domain": "^5.1.13",
26
+ "oak-frontend-base": "^5.3.20",
27
27
  "socket.io": "^4.7.2",
28
28
  "socket.io-client": "^4.7.2",
29
29
  "uuid": "^8.3.2"