mm_machine 3.5.4 → 3.5.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/lib/manager.js CHANGED
@@ -1,17 +1,19 @@
1
- const { Mod } = require('./mod.js');
1
+ const { Manager: Core } = require('mm_mod');
2
+ const { hasDir, hasFile, fullname, basename, dirname } = require('mm_helper');
3
+ const { Loader } = require('mm_loader');
4
+ const { Mod, addModMethods } = require('./mod.js');
2
5
  const { Drive, MODE } = require('./drive.js');
3
- const { Scanner } = require('./scanner.js');
4
6
 
5
7
  /**
6
8
  * 管理器类
7
9
  * @class
8
- * @extends Mod
10
+ * @augments Core
9
11
  * @param {object} config 配置项
10
12
  * @param {object} parent 父项对象
11
13
  * @param {object} mods 模块对象列表
12
14
  * @param {object} Drive 驱动对象
13
15
  */
14
- class Manager extends Mod {
16
+ class Manager extends Core {
15
17
  /**
16
18
  * 配置项
17
19
  * @type {object}
@@ -52,11 +54,6 @@ class Manager extends Mod {
52
54
  * @type {string}
53
55
  */
54
56
  dir: '', // './ai/manager'.fullname(),
55
- /**
56
- * 搜索模式 dir按目录搜索 | file按文件名搜索
57
- * @type {string}
58
- */
59
- search_way: 'file',
60
57
  /**
61
58
  * 类型 script脚本 | json配置 | drive驱动
62
59
  * @type {string}
@@ -81,12 +78,7 @@ class Manager extends Mod {
81
78
  * 作用域
82
79
  * @type {string}
83
80
  */
84
- scope: $.val && $.val.scope ? $.val.scope + '' : 'server',
85
- /**
86
- * 最大搜索深度 -1表示不限制深度
87
- * @type {number}
88
- */
89
- max_depth: 3,
81
+ scope: 'server',
90
82
  /**
91
83
  * 忽略目录数组
92
84
  * @type {Array}
@@ -98,10 +90,9 @@ class Manager extends Mod {
98
90
  * 构造函数
99
91
  * @param {object} config 配置项
100
92
  * @param {object} parent 父项对象
101
- * @param {object} mods 模块对象列表
102
- * @param {object} Drive 驱动对象
93
+ * @param {object} [mods] 模块对象列表,传入时作为受管记录的实例表(保持外部引用)
103
94
  */
104
- constructor(config, parent, mods = {}, Drive) {
95
+ constructor(config, parent, mods) {
105
96
  super(
106
97
  {
107
98
  ...Manager.config,
@@ -110,24 +101,28 @@ class Manager extends Mod {
110
101
  parent
111
102
  );
112
103
 
113
- this.scanner = new Scanner(this.config.ignore, this.config.max_depth);
114
- // 模块
115
- this.mods = mods;
116
- // 不使用注册表时,用内部缓存记录模块条目信息
117
- this.infos = [];
118
- if (Drive) {
119
- this.Drive = Drive;
104
+ if (parent) {
105
+ this.getParent = function () {
106
+ return parent;
107
+ };
120
108
  }
109
+ // 管理器自身的加载标记(受管模块状态由状态存储器管理)
110
+ this._loaded = false;
111
+ // 目录监听器,键为目录路径,值为该目录的配置加载器
112
+ this._dir_watchers = new Map();
113
+ // 外部传入实例表时,直接作为受管记录的实例表,保证外部引用同步
114
+ if (mods) {
115
+ this._getRecord().mods = mods;
116
+ }
117
+ // 监听事件
118
+ this._listen();
121
119
  }
122
120
  }
123
121
 
124
122
  /**
125
- * 获取事件触发器
126
- * @returns {object} 事件触发器
123
+ * 默认驱动
127
124
  */
128
- Manager.prototype.getEventer = function () {
129
- return $.eventer;
130
- };
125
+ Manager.prototype.Drive = Drive;
131
126
 
132
127
  /**
133
128
  * 获取父项键名
@@ -142,7 +137,7 @@ Manager.prototype.getParentKey = function () {
142
137
  * @returns {object[]} 模块配置数组
143
138
  */
144
139
  Manager.prototype.getInfos = function () {
145
- return this._getInfos();
140
+ return this.configs;
146
141
  };
147
142
 
148
143
  /**
@@ -151,7 +146,7 @@ Manager.prototype.getInfos = function () {
151
146
  * @returns {object|null} 模块配置对象
152
147
  */
153
148
  Manager.prototype.getInfo = function (name) {
154
- return this.infos.getObj({ name });
149
+ return this.configs.find((o) => o.name === name) || null;
155
150
  };
156
151
 
157
152
  /**
@@ -160,7 +155,7 @@ Manager.prototype.getInfo = function (name) {
160
155
  * @returns {object[]} 模块对象数组
161
156
  */
162
157
  Manager.prototype.getList = function (keyword) {
163
- let infos = this._getInfos();
158
+ let infos = this.configs;
164
159
 
165
160
  // 先用 info 做关键词过滤,避免无效实例化
166
161
  if (keyword) {
@@ -195,29 +190,30 @@ Manager.prototype.getTplDir = function () {
195
190
  };
196
191
 
197
192
  /**
198
- * 默认驱动
199
- */
200
- Manager.prototype.Drive = Drive;
201
-
202
- /**
203
- * 获取模块配置列表
204
- * @returns {Array} 排序后的模块配置数组
193
+ * 获取配置加载器
194
+ * @returns {Loader} 配置加载器
195
+ * @private
205
196
  */
206
- Manager.prototype._getInfos = function () {
207
- return this.infos || [];
197
+ Manager.prototype._getLoader = function () {
198
+ return new Loader({
199
+ resources: {
200
+ mod: {
201
+ ext: this.config.filename,
202
+ hot: false
203
+ }
204
+ },
205
+ ignores: this.config.ignore
206
+ });
208
207
  };
209
208
 
210
209
  // ==== 模块管理接口 ====
211
210
  /**
212
211
  * 清除所有模块
212
+ * @returns {Promise<void>} 无返回值
213
213
  */
214
- Manager.prototype.clearMods = function () {
215
- for (let name in this.mods) {
216
- delete this.mods[name];
217
- }
218
- if (this.infos) {
219
- this.infos = [];
220
- }
214
+ Manager.prototype.clearMods = async function () {
215
+ // 统一由受管记录释放模块实例,同时清空配置列表
216
+ await this.clear();
221
217
  };
222
218
 
223
219
  /**
@@ -226,11 +222,14 @@ Manager.prototype.clearMods = function () {
226
222
  * @returns {object|null} 删除的模块对象
227
223
  */
228
224
  Manager.prototype.delMod = function (name) {
229
- let mod = this.mods[name];
225
+ let record = this._getRecord();
226
+ let mod = record.mods[name];
230
227
  if (mod) {
231
- delete this.mods[name];
232
- if (this.infos) {
233
- this.infos = this.infos.filter((o) => { return o.name !== name; });
228
+ delete record.mods[name];
229
+ let infos = this.configs;
230
+ let index = infos.findIndex((o) => o.name === name);
231
+ if (index >= 0) {
232
+ infos.splice(index, 1);
234
233
  }
235
234
  }
236
235
  return mod;
@@ -242,7 +241,7 @@ Manager.prototype.delMod = function (name) {
242
241
  * @param {object} mod 模块对象
243
242
  */
244
243
  Manager.prototype.setMod = function (name, mod) {
245
- this.mods[name] = mod;
244
+ this._getRecord().mods[name] = mod;
246
245
  };
247
246
 
248
247
  /**
@@ -251,7 +250,7 @@ Manager.prototype.setMod = function (name, mod) {
251
250
  * @returns {object} 模块对象
252
251
  */
253
252
  Manager.prototype.getMod = function (name) {
254
- return this.mods[name];
253
+ return this._getRecord().mods[name];
255
254
  };
256
255
 
257
256
  /**
@@ -259,7 +258,28 @@ Manager.prototype.getMod = function (name) {
259
258
  * @returns {object[]} 模块对象数组
260
259
  */
261
260
  Manager.prototype.getMods = function () {
262
- return this.mods;
261
+ return this._getRecord().mods;
262
+ };
263
+
264
+ /**
265
+ * 读取模块配置
266
+ * @description 配置文件由管理器统一读取,调用方已提供名称时直接使用传入配置
267
+ * @param {string} config_file 配置文件路径
268
+ * @param {object} config 配置项
269
+ * @returns {object} 包含名称的配置项
270
+ * @private
271
+ */
272
+ Manager.prototype._readModConfig = function (config_file, config = {}) {
273
+ let mod_config = { ...config };
274
+ if (mod_config.name || !config_file) {
275
+ return mod_config;
276
+ }
277
+ let file_config = hasFile(config_file) ? this._getLoader().load(config_file) : null;
278
+ mod_config = { ...file_config, ...mod_config };
279
+ if (!mod_config.name) {
280
+ mod_config.name = basename(dirname(config_file));
281
+ }
282
+ return mod_config;
263
283
  };
264
284
 
265
285
  /**
@@ -272,20 +292,22 @@ Manager.prototype.getMods = function () {
272
292
  Manager.prototype.newMod = function (config_file, config, script) {
273
293
  try {
274
294
  let mod;
295
+ let mod_config = null;
275
296
  switch (this.config.mod_type) {
276
297
  case 'script':
277
298
  mod = new Mod(config, this.getParent() || this);
278
299
  break;
279
300
  default:
280
- mod = new this.Drive(config, this.getParent() || this);
301
+ // 配置由管理器读取,模块构造时即具备完整配置
302
+ mod_config = this._readModConfig(config_file, config);
303
+ mod = new this.Drive(mod_config, this.getParent() || this);
281
304
  break;
282
305
  }
283
306
  mod.mode = this.config.mode;
284
307
 
285
- if (config && config.name) {
286
- mod.config_file = config_file;
287
- } else {
288
- mod.loadConfig(config_file);
308
+ if (mod_config && config_file) {
309
+ mod.applyConfig(config_file, mod_config);
310
+ mod.watchConfig(config_file);
289
311
  }
290
312
 
291
313
  if (script) {
@@ -317,7 +339,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
317
339
  if (!mod) return null;
318
340
  this.setMod(mod.config.name, mod);
319
341
  let info = { ...mod.config, config_file: config_file };
320
- this.infos.push(info);
342
+ this.configs.push(info);
321
343
  return mod;
322
344
  };
323
345
 
@@ -405,25 +427,20 @@ Manager.prototype.loadMods = async function () {
405
427
  await Promise.all(promises);
406
428
  };
407
429
 
408
- /**
409
- * 获取目录名
410
- * @returns {string} 目录名
411
- */
412
- Manager.prototype.getDirName = function () {
413
- let prefix = this.constructor.name.toLowerCase();
414
- let name = this.config.name.toLowerCase();
415
- return prefix + '_' + name;
416
- };
417
-
418
430
  /**
419
431
  * 更新配置前的钩子函数
432
+ * @param {string} dir 检索的路径
433
+ * @param {boolean} clear 是否清除缓存
434
+ * @returns {Promise<void>} 无返回值
420
435
  */
421
- Manager.prototype.updateBefore = function () { };
436
+ Manager.prototype.updateBefore = async function (dir, clear) { };
422
437
 
423
438
  /**
424
- * 更新配置前的钩子函数
439
+ * 更新配置后的钩子函数
440
+ * @param {string} dir 检索的路径
441
+ * @returns {Promise<void>} 无返回值
425
442
  */
426
- Manager.prototype.updateAfter = function () {
443
+ Manager.prototype.updateAfter = async function (dir) {
427
444
  this._sort();
428
445
  };
429
446
 
@@ -431,7 +448,7 @@ Manager.prototype.updateAfter = function () {
431
448
  * 排序模块信息
432
449
  */
433
450
  Manager.prototype._sort = function () {
434
- this.infos.sort((a, b) => {
451
+ this.configs.sort((a, b) => {
435
452
  // 先按 sort 排序
436
453
  let cha = a.sort - b.sort;
437
454
  if (cha !== 0) {
@@ -449,8 +466,10 @@ Manager.prototype._sort = function () {
449
466
  */
450
467
  Manager.prototype.update = async function (dir, clear = true) {
451
468
  try {
469
+ await this.updateBefore(dir, clear);
470
+
452
471
  if (clear) {
453
- this.clearMods();
472
+ await this.clearMods();
454
473
  }
455
474
 
456
475
  let search_dir = dir || this.config.dir;
@@ -460,7 +479,10 @@ Manager.prototype.update = async function (dir, clear = true) {
460
479
  if (!this.config.lazy_load) {
461
480
  await this.loadMods();
462
481
  }
463
- this._is_loaded = true;
482
+
483
+ await this.updateAfter(search_dir);
484
+ // 检索目录开启监听,配置文件变化后增量维护模块
485
+ this._watchDir(search_dir);
464
486
  } catch (err) {
465
487
  this.log('error', '更新配置失败', err);
466
488
  }
@@ -477,13 +499,114 @@ Manager.prototype._findFiles = async function (dir) {
477
499
  this.log('warn', '目录为空,跳过扫描');
478
500
  return [];
479
501
  }
480
- if (!dir.hasDir()) {
502
+ if (!hasDir(dir)) {
481
503
  return [];
482
504
  }
483
- if (this.config.search_way === 'dir') {
484
- return await this.scanner.scan(dir, this.config.filename, this.getDirName());
505
+ let files = this._getLoader().search(dir);
506
+ let filename = this.config.filename;
507
+ if (!filename) {
508
+ return files;
509
+ }
510
+ return files.filter((file) => basename(file) === filename);
511
+ };
512
+
513
+ // ==== 目录监听 ====
514
+ /**
515
+ * 监听目录中配置文件的变化
516
+ * @description 只扫描一次,之后由文件事件增量维护模块
517
+ * @param {string} dir 目录路径
518
+ * @private
519
+ */
520
+ Manager.prototype._watchDir = function (dir) {
521
+ if (!dir || this._dir_watchers.has(dir)) return;
522
+ if (!hasDir(dir)) return;
523
+ let loader = this._getLoader();
524
+ let success = loader.watch(dir, (changes) => {
525
+ this._applyChanges(changes);
526
+ });
527
+ if (success) {
528
+ this._dir_watchers.set(dir, loader);
529
+ }
530
+ };
531
+
532
+ /**
533
+ * 应用配置文件变更
534
+ * @param {object[]} changes 变更列表,每项含 type 与 file
535
+ * @private
536
+ */
537
+ Manager.prototype._applyChanges = function (changes) {
538
+ this._applyChangesAsync(changes).catch((err) => {
539
+ this.log('error', '应用配置文件变更失败', err);
540
+ });
541
+ };
542
+
543
+ /**
544
+ * 按变更列表增量维护模块
545
+ * @description 新增与修改时重新注册模块,删除时注销模块;热更新模式下仅同步配置快照
546
+ * @param {object[]} changes 变更列表,每项含 type 与 file
547
+ * @returns {Promise<void>} 无返回值
548
+ * @private
549
+ */
550
+ Manager.prototype._applyChangesAsync = async function (changes) {
551
+ let filename = this.config.filename;
552
+ let lazy = this.config.lazy_load;
553
+ let hot = this.config.mode >= MODE.HOT_CONFIG;
554
+
555
+ for (let change of changes) {
556
+ if (filename && basename(change.file) !== filename) continue;
557
+ let info = this._getInfoByFile(change.file);
558
+
559
+ if (change.type === 'unlink') {
560
+ if (info) await this.unregMod(info.name);
561
+ continue;
562
+ }
563
+
564
+ if (change.type === 'change' && info && hot) {
565
+ this._syncInfo(info, change.file);
566
+ continue;
567
+ }
568
+
569
+ if (info) await this.unregMod(info.name);
570
+ let mod = this.regMod(change.file);
571
+ if (mod && !lazy) {
572
+ await this.loadMod(mod.config.name);
573
+ }
574
+ }
575
+
576
+ this._sort();
577
+ };
578
+
579
+ /**
580
+ * 按配置文件路径获取模块信息
581
+ * @param {string} file 配置文件路径
582
+ * @returns {object|null} 模块信息,未注册时返回null
583
+ * @private
584
+ */
585
+ Manager.prototype._getInfoByFile = function (file) {
586
+ return this.configs.find((info) => info.config_file === file) || null;
587
+ };
588
+
589
+ /**
590
+ * 同步模块信息
591
+ * @description 热更新模式下配置与脚本的重载由驱动负责,这里只维护管理器的配置快照
592
+ * @param {object} info 模块信息
593
+ * @param {string} file 配置文件路径
594
+ * @private
595
+ */
596
+ Manager.prototype._syncInfo = function (info, file) {
597
+ let mod_config = this._readModConfig(file);
598
+ Object.assign(info, mod_config, { config_file: file });
599
+ };
600
+
601
+ /**
602
+ * 关闭全部目录监听
603
+ * @returns {void}
604
+ */
605
+ Manager.prototype.unwatch = function () {
606
+ for (let loader of this._dir_watchers.values()) {
607
+ loader.unwatch();
485
608
  }
486
- return await this.scanner.scanFile(dir, this.config.filename);
609
+ this._dir_watchers.clear();
487
610
  };
488
611
 
489
612
  // ==== 高性能调用接口 ====
@@ -516,12 +639,9 @@ Manager.prototype.main = async function (name, method, ...params) {
516
639
  */
517
640
  Manager.prototype._runAllEnabled = async function (method, ...params) {
518
641
  let result;
519
- let infos = this._getInfos();
642
+ let infos = this.configs;
520
643
  for (let i = 0; i < infos.length; i++) {
521
644
  let info = infos[i];
522
- // 直接用 info.state 过滤,避免无效实例化
523
- if (!info.state) continue;
524
-
525
645
  let mod = this.getMod(info.name);
526
646
  if (!mod) continue;
527
647
 
@@ -551,12 +671,9 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
551
671
  */
552
672
  Manager.prototype.doAll = async function (method, ...params) {
553
673
  let promises = [];
554
- let infos = this._getInfos();
674
+ let infos = this.configs;
555
675
  for (let i = 0; i < infos.length; i++) {
556
676
  let info = infos[i];
557
- // 直接用 info.state 过滤,避免无效实例化
558
- if (!info.state) continue;
559
-
560
677
  let mod = this.getMod(info.name);
561
678
  if (!mod) continue;
562
679
  try {
@@ -573,20 +690,11 @@ Manager.prototype.doAll = async function (method, ...params) {
573
690
  * @param {object} mod 模块对象
574
691
  * @param {string} method 方法名称
575
692
  * @param {...any} params 参数集合
576
- * @returns {Promise<any>} 执行结果
693
+ * @returns {Promise<any>} 执行结果,模块未运行时返回 null
577
694
  * @private
578
695
  */
579
696
  Manager.prototype._runMod = async function (mod, method, ...params) {
580
- mod = await this._ensureModLoaded(mod, method);
581
-
582
- // 执行方法
583
- let ret = await mod.do(method, ...params);
584
- // 根据模式决定是否重载
585
- if (this.config.mode >= MODE.HOT_BOTH) {
586
- await mod.do('reload');
587
- }
588
-
589
- return ret;
697
+ return await this._runModWith(mod, 'do', method, ...params);
590
698
  };
591
699
 
592
700
  /**
@@ -594,17 +702,36 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
594
702
  * @param {object} mod 模块对象
595
703
  * @param {string} method 方法名称
596
704
  * @param {...any} params 参数集合
597
- * @returns {Promise<any>} 执行结果
705
+ * @returns {Promise<any>} 执行结果,模块未运行时返回 null
598
706
  * @private
599
707
  */
600
708
  Manager.prototype._callMod = async function (mod, method, ...params) {
601
- mod = await this._ensureModLoaded(mod, method);
709
+ return await this._runModWith(mod, 'call', method, ...params);
710
+ };
711
+
712
+ /**
713
+ * 按指定执行器运行模块方法 - 内部方法
714
+ * @param {object} mod 模块对象
715
+ * @param {string} handler 执行器名称(do/call)
716
+ * @param {string} method 方法名称
717
+ * @param {...any} params 参数集合
718
+ * @returns {Promise<any>} 执行结果,模块未运行时返回 null
719
+ * @private
720
+ */
721
+ Manager.prototype._runModWith = async function (mod, handler, method, ...params) {
722
+ // 状态由管理器统一判断:模块未运行则跳过
723
+ let name = mod.config.name;
724
+ let is_running = await this.isRunning(name);
725
+ if (!is_running) {
726
+ return null;
727
+ }
728
+ let target = await this._ensureModLoaded(mod, method);
602
729
 
603
730
  // 执行方法
604
- let ret = await mod.call(method, ...params);
731
+ let ret = await target[handler](method, ...params);
605
732
  // 根据模式决定是否重载
606
733
  if (this.config.mode >= MODE.HOT_BOTH) {
607
- await mod.do('reload');
734
+ await target.do('reload');
608
735
  }
609
736
 
610
737
  return ret;
@@ -678,16 +805,18 @@ Manager.prototype._loadCore = async function () {
678
805
  this._initManager();
679
806
  // 加载资源
680
807
  await this._loadSources();
808
+ // 标记管理器已完成加载
809
+ this._loaded = true;
681
810
  };
682
811
 
683
812
  /**
684
813
  * 初始化公共属性
685
814
  */
686
815
  Manager.prototype._initCore = async function () {
687
- if (!this.isLoaded()) {
816
+ if (!this._loaded) {
688
817
  await this.do('load');
689
818
  }
690
- // 初始化AI客户端
819
+ // 初始化资源
691
820
  await this._initSources();
692
821
  };
693
822
 
@@ -738,19 +867,22 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
738
867
  throw new TypeError(`模块(mod)参数不能为空`);
739
868
  }
740
869
  if (!['load', 'init'].includes(method)) {
741
- if (!mod.isLoaded()) {
870
+ let name = mod.config.name;
871
+ // 状态由管理器统一判断:无运行状态记录视为尚未加载与初始化
872
+ let is_running = await this.isRunning(name);
873
+ if (is_running) {
742
874
  try {
743
875
  await mod.do('load');
744
876
  } catch (error) {
745
- throw new Error(`模块${mod.config.name}加载失败,无法执行方法 ${method}`);
877
+ throw new Error(`模块${name}加载失败,无法执行方法 ${method}`);
746
878
  }
747
- }
748
- if (!mod.isInited()) {
749
879
  try {
750
880
  await mod.do('init');
751
881
  } catch (error) {
752
- throw new Error(`模块${mod.config.name}初始化失败,无法执行方法 ${method}`);
882
+ throw new Error(`模块${name}初始化失败,无法执行方法 ${method}`);
753
883
  }
884
+ // 记录受管模块已完成初始化
885
+ await this.setState(name, 'inited');
754
886
  }
755
887
  };
756
888
  return mod;
@@ -762,11 +894,10 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
762
894
  */
763
895
  Manager.prototype._ensureAllLoaded = async function (method) {
764
896
  let promises = [];
765
- let infos = this._getInfos();
897
+ let infos = this.configs;
766
898
 
767
899
  for (let i = 0; i < infos.length; i++) {
768
900
  let info = infos[i];
769
- if (!info.state) continue;
770
901
  let mod = this.getMod(info.name);
771
902
  if (!mod) continue;
772
903
  try {
@@ -791,11 +922,10 @@ Manager.prototype.runWait = async function (method, ...params) {
791
922
  }
792
923
 
793
924
  let result;
794
- let infos = this._getInfos();
925
+ let infos = this.configs;
795
926
 
796
927
  for (let i = 0; i < infos.length; i++) {
797
928
  let info = infos[i];
798
- if (!info.state) continue;
799
929
  let mod = this.getMod(info.name);
800
930
  if (!mod) continue;
801
931
  try {
@@ -826,11 +956,10 @@ Manager.prototype.runAsync = async function (method, ...params) {
826
956
  // 确保所有模块都已加载
827
957
  await this._ensureAllLoaded(method);
828
958
 
829
- let infos = this._getInfos();
959
+ let infos = this.configs;
830
960
 
831
961
  for (let i = 0; i < infos.length; i++) {
832
962
  let info = infos[i];
833
- if (!info.state) continue;
834
963
  let mod = this.getMod(info.name);
835
964
  if (!mod) continue;
836
965
 
@@ -842,6 +971,27 @@ Manager.prototype.runAsync = async function (method, ...params) {
842
971
  return true;
843
972
  };
844
973
 
974
+ /**
975
+ * 运行模块生命周期方法并同步记录运行状态
976
+ * @param {object} mod 模块对象
977
+ * @param {string} method 方法名称
978
+ * @param {...any} params 方法参数
979
+ * @returns {Promise<any>} 执行结果
980
+ * @private
981
+ */
982
+ Manager.prototype._runLifecycle = async function (mod, method, ...params) {
983
+ const name = mod.config.name;
984
+ await this._markState(name, method, 'running');
985
+ try {
986
+ const result = await mod.do(method, ...params);
987
+ await this._markState(name, method, 'done');
988
+ return result;
989
+ } catch (error) {
990
+ await this._safeSetState(name, { state: 'error' });
991
+ throw error;
992
+ }
993
+ };
994
+
845
995
  /**
846
996
  * 异步触发事件,并发执行等待完成
847
997
  * @param {string} method 方法名称
@@ -854,15 +1004,14 @@ Manager.prototype.runAll = async function (method, ...params) {
854
1004
  }
855
1005
 
856
1006
  const promises = [];
857
- let infos = this._getInfos();
1007
+ let infos = this.configs;
858
1008
  for (let i = 0; i < infos.length; i++) {
859
1009
  let info = infos[i];
860
- if (!info.state) continue;
861
1010
  let mod = this.getMod(info.name);
862
1011
  if (!mod) continue;
863
1012
  try {
864
1013
  mod = await this._ensureModLoaded(mod, method);
865
- promises.push(mod.do(method, ...params));
1014
+ promises.push(this._runLifecycle(mod, method, ...params));
866
1015
  } catch (error) {
867
1016
  this._logModError(info.name, method, error);
868
1017
  }
@@ -886,11 +1035,10 @@ Manager.prototype.runRace = async function (method, ...params) {
886
1035
  await this._ensureAllLoaded(method);
887
1036
 
888
1037
  const promises = [];
889
- let infos = this._getInfos();
1038
+ let infos = this.configs;
890
1039
 
891
1040
  for (let i = 0; i < infos.length; i++) {
892
1041
  let info = infos[i];
893
- if (!info.state) continue;
894
1042
  let mod = this.getMod(info.name);
895
1043
  if (!mod) continue;
896
1044
 
@@ -916,11 +1064,10 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
916
1064
  throw new TypeError('方法名称必须是字符串');
917
1065
  }
918
1066
 
919
- let infos = this._getInfos();
1067
+ let infos = this.configs;
920
1068
 
921
1069
  for (let i = 0; i < infos.length; i++) {
922
1070
  let info = infos[i];
923
- if (!info.state) continue;
924
1071
  let mod = this.getMod(info.name);
925
1072
  if (!mod) continue;
926
1073
  try {
@@ -978,12 +1125,8 @@ Manager.prototype.createFile = function (config, file) {
978
1125
  let f = file;
979
1126
  if (!f) {
980
1127
  f = `${config.name}/${this.config.filename}`;
981
- if (this.config.search_way === 'dir') {
982
- let dir_name = this.getDirName();
983
- f = `${dir_name}/${f}`;
984
- }
985
1128
  }
986
- f = f.fullname(this.config.dir);
1129
+ f = fullname(f, this.config.dir);
987
1130
  mod.createConfigFile(f, config);
988
1131
  return f;
989
1132
  };
@@ -1023,6 +1166,10 @@ Manager.prototype._logModError = function (name, method, error) {
1023
1166
  this.log('error', '模块' + name + '执行' + method + '方法失败', error);
1024
1167
  };
1025
1168
 
1026
- module.exports = {
1027
- Manager
1028
- };
1169
+ /**
1170
+ * 复用模块类的调度与生命周期能力
1171
+ * @description Manager 继承 mm_mod.Manager,需要将 mm_machine 的 Mod 原型成员复制到 Manager 原型
1172
+ */
1173
+ addModMethods(Manager);
1174
+
1175
+ module.exports = Manager;