@abtnode/core 1.16.11-beta-069c3537 → 1.16.11-beta-f74663ac
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/api/node.js +1 -1
- package/lib/api/team.js +5 -5
- package/lib/blocklet/downloader/blocklet-downloader.js +34 -19
- package/lib/blocklet/downloader/bundle-downloader.js +2 -2
- package/lib/blocklet/hooks.js +1 -1
- package/lib/blocklet/manager/base.js +9 -9
- package/lib/blocklet/manager/disk.js +453 -253
- package/lib/blocklet/manager/helper/install-application-from-backup.js +1 -1
- package/lib/blocklet/manager/helper/install-application-from-general.js +18 -3
- package/lib/blocklet/manager/helper/install-component-from-dev.js +0 -1
- package/lib/blocklet/manager/helper/install-component-from-upload.js +8 -4
- package/lib/blocklet/manager/helper/install-component-from-url.js +4 -2
- package/lib/blocklet/manager/helper/upgrade-components.js +7 -7
- package/lib/blocklet/manager/pm2-events.js +3 -3
- package/lib/blocklet/migration.js +2 -2
- package/lib/blocklet/storage/backup/base.js +1 -0
- package/lib/blocklet/storage/backup/blocklet-extras.js +1 -1
- package/lib/blocklet/storage/backup/blocklet.js +1 -1
- package/lib/blocklet/storage/backup/disk.js +1 -0
- package/lib/blocklet/storage/restore/blocklet-extras.js +1 -1
- package/lib/blocklet/storage/restore/blocklet.js +1 -1
- package/lib/blocklet/storage/restore/disk.js +1 -1
- package/lib/blocklet/storage/restore/spaces.js +1 -1
- package/lib/cert.js +5 -5
- package/lib/crons/rotate-pm2-logs/index.js +1 -1
- package/lib/event.js +11 -1
- package/lib/index.js +8 -2
- package/lib/migrations/1.16.11-component-status.js +22 -0
- package/lib/migrations/index.js +3 -3
- package/lib/monitor/blocklet-runtime-monitor.js +7 -2
- package/lib/processes/updater.js +1 -1
- package/lib/router/helper.js +5 -5
- package/lib/states/audit-log.js +1 -1
- package/lib/states/backup.js +5 -4
- package/lib/states/blocklet-extras.js +8 -13
- package/lib/states/blocklet.js +46 -39
- package/lib/states/cache.js +1 -1
- package/lib/states/node.js +6 -2
- package/lib/states/notification.js +1 -1
- package/lib/states/routing-snapshot.js +1 -1
- package/lib/states/site.js +1 -1
- package/lib/states/user.js +12 -6
- package/lib/team/manager.js +6 -6
- package/lib/util/blocklet.js +132 -120
- package/lib/util/get-accessible-external-node-ip.js +2 -0
- package/lib/util/launcher.js +238 -0
- package/lib/util/reset-node.js +19 -16
- package/lib/util/rpc.js +1 -1
- package/lib/util/store.js +1 -1
- package/lib/validators/blocklet-extra.js +0 -6
- package/lib/validators/space-gateway.js +3 -0
- package/lib/validators/util.js +3 -0
- package/lib/webhook/sender/api/index.js +1 -1
- package/lib/webhook/sender/slack/index.js +1 -1
- package/package.json +18 -18
|
@@ -38,6 +38,10 @@ const {
|
|
|
38
38
|
getRolesFromAuthConfig,
|
|
39
39
|
isInProgress,
|
|
40
40
|
isBeforeInstalled,
|
|
41
|
+
getComponentsInternalInfo,
|
|
42
|
+
forEachComponentV2,
|
|
43
|
+
forEachComponentV2Sync,
|
|
44
|
+
findComponentByIdV2,
|
|
41
45
|
} = require('@blocklet/meta/lib/util');
|
|
42
46
|
const getComponentProcessId = require('@blocklet/meta/lib/get-component-process-id');
|
|
43
47
|
const { update: updateMetaFile } = require('@blocklet/meta/lib/file');
|
|
@@ -49,6 +53,7 @@ const {
|
|
|
49
53
|
BlockletStatus,
|
|
50
54
|
BlockletSource,
|
|
51
55
|
BlockletEvents,
|
|
56
|
+
BlockletInternalEvents,
|
|
52
57
|
BLOCKLET_MODES,
|
|
53
58
|
BlockletGroup,
|
|
54
59
|
fromBlockletStatus,
|
|
@@ -65,7 +70,11 @@ const {
|
|
|
65
70
|
BLOCKLET_CONFIGURABLE_KEY,
|
|
66
71
|
RESTORE_PROGRESS_STATUS,
|
|
67
72
|
CHAIN_PROP_MAP_REVERSE,
|
|
73
|
+
BLOCKLET_CONTROLLER_STATUS,
|
|
74
|
+
SUSPENDED_REASON,
|
|
68
75
|
} = require('@blocklet/constant');
|
|
76
|
+
const isUndefined = require('lodash/isUndefined');
|
|
77
|
+
const { consumeServerlessNFT, consumeLauncherSession } = require('../../util/launcher');
|
|
69
78
|
const util = require('../../util');
|
|
70
79
|
const {
|
|
71
80
|
refresh: refreshAccessibleExternalNodeIp,
|
|
@@ -80,7 +89,6 @@ const {
|
|
|
80
89
|
stopBlockletProcess,
|
|
81
90
|
deleteBlockletProcess,
|
|
82
91
|
reloadBlockletProcess,
|
|
83
|
-
getBlockletStatusFromProcess,
|
|
84
92
|
checkBlockletProcessHealthy,
|
|
85
93
|
validateBlocklet,
|
|
86
94
|
validateBlockletChainInfo,
|
|
@@ -94,7 +102,6 @@ const {
|
|
|
94
102
|
getBlocklet,
|
|
95
103
|
ensureEnvDefault,
|
|
96
104
|
getConfigFromPreferences,
|
|
97
|
-
consumeServerlessNFT,
|
|
98
105
|
validateAppConfig,
|
|
99
106
|
checkDuplicateMountPoint,
|
|
100
107
|
validateStore,
|
|
@@ -104,7 +111,10 @@ const {
|
|
|
104
111
|
updateBlockletFallbackLogo,
|
|
105
112
|
ensureAppLogo,
|
|
106
113
|
getBlockletDidDomainList,
|
|
107
|
-
|
|
114
|
+
getProcessState,
|
|
115
|
+
getBlockletStatus,
|
|
116
|
+
shouldSkipComponent,
|
|
117
|
+
exceedRedemptionPeriod,
|
|
108
118
|
} = require('../../util/blocklet');
|
|
109
119
|
const states = require('../../states');
|
|
110
120
|
const BaseBlockletManager = require('./base');
|
|
@@ -133,7 +143,7 @@ const { migrateApplicationToStructV2 } = require('./helper/migrate-application-t
|
|
|
133
143
|
const { getBackupFilesUrlFromEndpoint, getBackupEndpoint } = require('../../util/spaces');
|
|
134
144
|
const { validateAddSpaceGateway, validateUpdateSpaceGateway } = require('../../validators/space-gateway');
|
|
135
145
|
|
|
136
|
-
const { formatEnvironments,
|
|
146
|
+
const { formatEnvironments, getBlockletMeta, validateOwner } = util;
|
|
137
147
|
|
|
138
148
|
const statusLock = new Lock('blocklet-status-lock');
|
|
139
149
|
|
|
@@ -239,8 +249,8 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
239
249
|
this._rollbackCache = new RollbackCache({ dir: this.dataDirs.tmp });
|
|
240
250
|
|
|
241
251
|
if (daemon) {
|
|
242
|
-
blockletPm2Events.on('online', (data) => this._syncPm2Status('online', data.blockletDid));
|
|
243
|
-
blockletPm2Events.on('stop', (data) => this._syncPm2Status('stop', data.blockletDid));
|
|
252
|
+
blockletPm2Events.on('online', (data) => this._syncPm2Status('online', data.blockletDid, data.componentDid));
|
|
253
|
+
blockletPm2Events.on('stop', (data) => this._syncPm2Status('stop', data.blockletDid, data.componentDid));
|
|
244
254
|
}
|
|
245
255
|
}
|
|
246
256
|
|
|
@@ -400,7 +410,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
400
410
|
throw new Error('Unknown source');
|
|
401
411
|
}
|
|
402
412
|
|
|
403
|
-
|
|
413
|
+
diff({ did, hashFiles, rootDid }) {
|
|
404
414
|
return diff({ did, hashFiles, rootDid, states, manager: this });
|
|
405
415
|
}
|
|
406
416
|
|
|
@@ -408,7 +418,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
408
418
|
* After the dev function finished, the caller should send a BlockletEvents.installed event to the daemon
|
|
409
419
|
* @returns {Object} blocklet
|
|
410
420
|
*/
|
|
411
|
-
|
|
421
|
+
dev(folder, { rootDid, mountPoint, defaultStoreUrl } = {}) {
|
|
412
422
|
logger.info('dev component', { folder, rootDid, mountPoint });
|
|
413
423
|
|
|
414
424
|
const meta = getBlockletMeta(folder, { defaultStoreUrl });
|
|
@@ -423,15 +433,15 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
423
433
|
return installComponentFromDev({ folder, meta, rootDid, mountPoint, manager: this, states });
|
|
424
434
|
}
|
|
425
435
|
|
|
426
|
-
|
|
436
|
+
checkComponentsForUpdates({ did }) {
|
|
427
437
|
return UpgradeComponents.check({ did, states });
|
|
428
438
|
}
|
|
429
439
|
|
|
430
|
-
|
|
431
|
-
return UpgradeComponents.upgrade({ updateId,
|
|
440
|
+
upgradeComponents({ updateId, selectedComponents: componentDids }, context = {}) {
|
|
441
|
+
return UpgradeComponents.upgrade({ updateId, componentDids, context, states, manager: this });
|
|
432
442
|
}
|
|
433
443
|
|
|
434
|
-
|
|
444
|
+
migrateApplicationToStructV2({ did, appSk, context = {} }) {
|
|
435
445
|
return migrateApplicationToStructV2({ did, appSk, context, manager: this, states });
|
|
436
446
|
}
|
|
437
447
|
|
|
@@ -445,16 +455,18 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
445
455
|
return { did, isInstalled: !!blocklet, isRunning };
|
|
446
456
|
}
|
|
447
457
|
|
|
448
|
-
async start({ did, throwOnError, checkHealthImmediately = false, e2eMode = false }, context) {
|
|
449
|
-
logger.info('start blocklet', { did });
|
|
458
|
+
async start({ did, throwOnError, checkHealthImmediately = false, e2eMode = false, componentDids }, context) {
|
|
459
|
+
logger.info('start blocklet', { did, componentDids, throwOnError, checkHealthImmediately, e2eMode });
|
|
450
460
|
// should check blocklet integrity
|
|
451
461
|
const blocklet = await this.ensureBlocklet(did, { e2eMode });
|
|
452
462
|
|
|
463
|
+
await this.checkControllerStatus(blocklet, 'start');
|
|
464
|
+
|
|
453
465
|
try {
|
|
454
466
|
// blocklet may be manually stopped durning starting
|
|
455
467
|
// so error message would not be sent if blocklet is stopped
|
|
456
468
|
// so we need update status first
|
|
457
|
-
await states.blocklet.setBlockletStatus(did, BlockletStatus.starting);
|
|
469
|
+
await states.blocklet.setBlockletStatus(did, BlockletStatus.starting, { componentDids });
|
|
458
470
|
blocklet.status = BlockletStatus.starting;
|
|
459
471
|
|
|
460
472
|
// validate requirement and engine
|
|
@@ -507,6 +519,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
507
519
|
nodeEnvironments,
|
|
508
520
|
nodeInfo: await states.node.read(),
|
|
509
521
|
e2eMode,
|
|
522
|
+
componentDids,
|
|
510
523
|
});
|
|
511
524
|
|
|
512
525
|
// check blocklet healthy
|
|
@@ -516,6 +529,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
516
529
|
context,
|
|
517
530
|
minConsecutiveTime,
|
|
518
531
|
timeout: startTimeout,
|
|
532
|
+
componentDids,
|
|
519
533
|
};
|
|
520
534
|
|
|
521
535
|
if (checkHealthImmediately) {
|
|
@@ -548,8 +562,8 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
548
562
|
severity: 'error',
|
|
549
563
|
});
|
|
550
564
|
|
|
551
|
-
await this.deleteProcess({ did });
|
|
552
|
-
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.error);
|
|
565
|
+
await this.deleteProcess({ did, componentDids });
|
|
566
|
+
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.error, { componentDids });
|
|
553
567
|
this.emit(BlockletEvents.startFailed, { ...res, error: { message: error.message } });
|
|
554
568
|
|
|
555
569
|
if (throwOnError) {
|
|
@@ -560,14 +574,14 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
560
574
|
}
|
|
561
575
|
}
|
|
562
576
|
|
|
563
|
-
async stop({ did, updateStatus = true, silent = false }, context) {
|
|
564
|
-
logger.info('stop blocklet', { did });
|
|
577
|
+
async stop({ did, updateStatus = true, silent = false, componentDids }, context) {
|
|
578
|
+
logger.info('stop blocklet', { did, componentDids, updateStatus, silent });
|
|
565
579
|
|
|
566
580
|
const blocklet = await this.getBlocklet(did);
|
|
567
581
|
const { processId } = blocklet.env;
|
|
568
582
|
|
|
569
583
|
if (updateStatus) {
|
|
570
|
-
await states.blocklet.setBlockletStatus(did, BlockletStatus.stopping);
|
|
584
|
+
await states.blocklet.setBlockletStatus(did, BlockletStatus.stopping, { componentDids });
|
|
571
585
|
blocklet.status = BlockletStatus.stopping;
|
|
572
586
|
this.emit(BlockletEvents.statusChange, blocklet);
|
|
573
587
|
}
|
|
@@ -587,11 +601,12 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
587
601
|
silent,
|
|
588
602
|
...getHooksOutputFiles(b),
|
|
589
603
|
}),
|
|
604
|
+
componentDids,
|
|
590
605
|
});
|
|
591
606
|
} catch (error) {
|
|
592
607
|
logger.error('Failed to stop blocklet', { error, did });
|
|
593
608
|
if (updateStatus) {
|
|
594
|
-
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.error);
|
|
609
|
+
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.error, { componentDids });
|
|
595
610
|
this.emit(BlockletEvents.statusChange, res);
|
|
596
611
|
}
|
|
597
612
|
throw error;
|
|
@@ -600,13 +615,18 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
600
615
|
logger.info('blocklet stopped successfully', { processId, did });
|
|
601
616
|
|
|
602
617
|
if (updateStatus) {
|
|
603
|
-
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped);
|
|
618
|
+
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped, { componentDids });
|
|
604
619
|
// send notification to websocket channel
|
|
605
620
|
this.emit(BlockletEvents.statusChange, res);
|
|
606
621
|
|
|
607
622
|
// send notification to wallet
|
|
608
623
|
this.emit(BlockletEvents.stopped, res);
|
|
609
624
|
|
|
625
|
+
this.emit(BlockletInternalEvents.componentsUpdated, {
|
|
626
|
+
appDid: blocklet.appDid,
|
|
627
|
+
components: getComponentsInternalInfo(res),
|
|
628
|
+
});
|
|
629
|
+
|
|
610
630
|
return res;
|
|
611
631
|
}
|
|
612
632
|
|
|
@@ -636,13 +656,23 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
636
656
|
}
|
|
637
657
|
|
|
638
658
|
/**
|
|
639
|
-
* FIXME: @linchen support cancel
|
|
640
659
|
* @param {import('@abtnode/client').RequestRestoreBlockletInput} input
|
|
641
660
|
* @memberof BlockletManager
|
|
642
661
|
*/
|
|
643
|
-
// eslint-disable-next-line no-unused-vars
|
|
644
662
|
async restoreBlocklet(input, context) {
|
|
645
663
|
const { from, ...param } = input;
|
|
664
|
+
|
|
665
|
+
if (input.overwrite) {
|
|
666
|
+
try {
|
|
667
|
+
const blocklet = await this.getBlocklet(input.appPid);
|
|
668
|
+
if (blocklet) {
|
|
669
|
+
await this.delete({ did: input.appPid, keepData: false, keepLogsDir: false, keepConfigs: false }, context);
|
|
670
|
+
}
|
|
671
|
+
} catch (error) {
|
|
672
|
+
logger.error('Failed to delete blocklet', { error, did: input.appPid });
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
646
676
|
if (from === 'spaces') {
|
|
647
677
|
return this._restoreFromSpaces(param, context);
|
|
648
678
|
}
|
|
@@ -660,18 +690,26 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
660
690
|
* @param {Record<string, any>} context
|
|
661
691
|
* @returns {import('@abtnode/client').BlockletState}
|
|
662
692
|
*/
|
|
663
|
-
async restart({ did }, context) {
|
|
693
|
+
async restart({ did, componentDids }, context) {
|
|
664
694
|
logger.info('restart blocklet', { did });
|
|
665
|
-
|
|
666
|
-
await
|
|
695
|
+
const blocklet = await this.getBlocklet(did);
|
|
696
|
+
await this.checkControllerStatus(blocklet, 'restart');
|
|
697
|
+
await states.blocklet.setBlockletStatus(did, BlockletStatus.stopping, { componentDids });
|
|
667
698
|
const result = await states.blocklet.getBlocklet(did);
|
|
668
699
|
this.emit(BlockletEvents.statusChange, result);
|
|
669
700
|
|
|
670
|
-
const ticket = this.startQueue.push({
|
|
701
|
+
const ticket = this.startQueue.push({
|
|
702
|
+
entity: 'blocklet',
|
|
703
|
+
action: 'restart',
|
|
704
|
+
id: did,
|
|
705
|
+
did,
|
|
706
|
+
componentDids,
|
|
707
|
+
context,
|
|
708
|
+
});
|
|
671
709
|
ticket.on('failed', async (err) => {
|
|
672
710
|
logger.error('failed to restart blocklet', { did, error: err });
|
|
673
711
|
|
|
674
|
-
const state = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped);
|
|
712
|
+
const state = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped, { componentDids });
|
|
675
713
|
this.emit(BlockletEvents.statusChange, state);
|
|
676
714
|
|
|
677
715
|
this._createNotification(did, {
|
|
@@ -687,16 +725,29 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
687
725
|
}
|
|
688
726
|
|
|
689
727
|
// eslint-disable-next-line no-unused-vars
|
|
690
|
-
async reload({ did }, context) {
|
|
728
|
+
async reload({ did, componentDids: list }, context) {
|
|
691
729
|
const blocklet = await this.getBlocklet(did);
|
|
730
|
+
await this.checkControllerStatus(blocklet, 'reload');
|
|
731
|
+
|
|
732
|
+
const componentDids = (blocklet.children || [])
|
|
733
|
+
.filter((x) => x.status === BlockletStatus.running)
|
|
734
|
+
.filter((x) => !shouldSkipComponent(x.meta.did, list))
|
|
735
|
+
.map((x) => x.meta.did);
|
|
692
736
|
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
logger.info('blocklet reload successfully', { did });
|
|
737
|
+
if (!componentDids.length) {
|
|
738
|
+
throw new Error('No running component found');
|
|
739
|
+
}
|
|
697
740
|
|
|
698
|
-
|
|
699
|
-
|
|
741
|
+
try {
|
|
742
|
+
await reloadBlockletProcess(blocklet, { componentDids });
|
|
743
|
+
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.running, { componentDids });
|
|
744
|
+
logger.info('blocklet reload successfully', { did, componentDids });
|
|
745
|
+
this.emit(BlockletEvents.statusChange, res);
|
|
746
|
+
return res;
|
|
747
|
+
} catch (error) {
|
|
748
|
+
logger.error('Failed to reload blocklet', { error, did, componentDids });
|
|
749
|
+
throw error;
|
|
750
|
+
}
|
|
700
751
|
}
|
|
701
752
|
|
|
702
753
|
async delete({ did, keepData, keepLogsDir, keepConfigs }, context) {
|
|
@@ -755,17 +806,17 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
755
806
|
|
|
756
807
|
const blocklet = await this.getBlocklet(did);
|
|
757
808
|
|
|
758
|
-
if (
|
|
759
|
-
|
|
760
|
-
|
|
809
|
+
if (!childDid) {
|
|
810
|
+
if (isInProgress(blocklet.status || blocklet.status === BlockletStatus.running)) {
|
|
811
|
+
throw new Error('Cannot reset when blocklet is in progress');
|
|
812
|
+
}
|
|
761
813
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
814
|
+
try {
|
|
815
|
+
await this.deleteProcess({ did }, context);
|
|
816
|
+
} catch {
|
|
817
|
+
// do nothing
|
|
818
|
+
}
|
|
767
819
|
|
|
768
|
-
if (!childDid) {
|
|
769
820
|
// Cleanup disk storage
|
|
770
821
|
const { cacheDir, logsDir, dataDir } = blocklet.env;
|
|
771
822
|
fs.removeSync(cacheDir);
|
|
@@ -783,6 +834,16 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
783
834
|
throw new Error('Child does not exist');
|
|
784
835
|
}
|
|
785
836
|
|
|
837
|
+
if (isInProgress(child.status || child.status === BlockletStatus.running)) {
|
|
838
|
+
throw new Error('Cannot reset when component is in progress');
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
try {
|
|
842
|
+
await this.deleteProcess({ did, componentDids: [child.meta.did] }, context);
|
|
843
|
+
} catch {
|
|
844
|
+
// do nothing
|
|
845
|
+
}
|
|
846
|
+
|
|
786
847
|
// Cleanup disk storage
|
|
787
848
|
const { cacheDir, logsDir, dataDir } = child.env;
|
|
788
849
|
fs.removeSync(cacheDir);
|
|
@@ -864,6 +925,11 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
864
925
|
action: `/blocklets/${newBlocklet.meta.did}/components`,
|
|
865
926
|
});
|
|
866
927
|
|
|
928
|
+
this.emit(BlockletInternalEvents.componentsUpdated, {
|
|
929
|
+
appDid: blocklet.appDid,
|
|
930
|
+
components: getComponentsInternalInfo(newBlocklet),
|
|
931
|
+
});
|
|
932
|
+
|
|
867
933
|
return newBlocklet;
|
|
868
934
|
}
|
|
869
935
|
|
|
@@ -922,7 +988,14 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
922
988
|
// fallback blocklet status to error
|
|
923
989
|
const blocklet = await states.blocklet.getBlocklet(inputDid);
|
|
924
990
|
if (blocklet) {
|
|
925
|
-
|
|
991
|
+
const componentDids = [];
|
|
992
|
+
forEachComponentV2Sync(blocklet, (x) => {
|
|
993
|
+
if ([BlockletStatus.waiting, BlockletStatus.downloading].includes(x.status)) {
|
|
994
|
+
componentDids.push(x.meta.did);
|
|
995
|
+
}
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
await states.blocklet.setBlockletStatus(blocklet.meta.did, BlockletStatus.error, { componentDids });
|
|
926
999
|
}
|
|
927
1000
|
statusLock.release();
|
|
928
1001
|
} catch (err) {
|
|
@@ -935,15 +1008,15 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
935
1008
|
}
|
|
936
1009
|
|
|
937
1010
|
// eslint-disable-next-line no-unused-vars
|
|
938
|
-
async deleteProcess({ did }, context) {
|
|
1011
|
+
async deleteProcess({ did, componentDids }, context) {
|
|
939
1012
|
const blocklet = await this.getBlocklet(did);
|
|
940
1013
|
|
|
941
|
-
logger.info('delete blocklet process', { did });
|
|
1014
|
+
logger.info('delete blocklet process', { did, componentDids });
|
|
942
1015
|
|
|
943
|
-
await deleteBlockletProcess(blocklet, context);
|
|
1016
|
+
await deleteBlockletProcess(blocklet, { ...context, componentDids });
|
|
944
1017
|
|
|
945
|
-
const result = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped);
|
|
946
|
-
logger.info('blocklet process deleted successfully', { did });
|
|
1018
|
+
const result = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped, { componentDids });
|
|
1019
|
+
logger.info('blocklet process deleted successfully', { did, componentDids });
|
|
947
1020
|
return result;
|
|
948
1021
|
}
|
|
949
1022
|
|
|
@@ -986,22 +1059,9 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
986
1059
|
}
|
|
987
1060
|
}
|
|
988
1061
|
|
|
989
|
-
async list({ includeRuntimeInfo = true, query
|
|
1062
|
+
async list({ includeRuntimeInfo = true, query } = {}, context) {
|
|
990
1063
|
const condition = { ...flat(query || {}) };
|
|
991
|
-
if (filter === 'external-only') {
|
|
992
|
-
condition.controller = {
|
|
993
|
-
$exists: true,
|
|
994
|
-
};
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
if (filter === 'external-excluded') {
|
|
998
|
-
condition.controller = {
|
|
999
|
-
$exists: false,
|
|
1000
|
-
};
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
1064
|
const blocklets = await states.blocklet.getBlocklets(condition);
|
|
1004
|
-
|
|
1005
1065
|
if (includeRuntimeInfo) {
|
|
1006
1066
|
return this._attachBlockletListRuntimeInfo({ blocklets }, context);
|
|
1007
1067
|
}
|
|
@@ -1009,7 +1069,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1009
1069
|
return blocklets;
|
|
1010
1070
|
}
|
|
1011
1071
|
|
|
1012
|
-
|
|
1072
|
+
listBackups() {
|
|
1013
1073
|
return getBackupList(this.dataDirs.data);
|
|
1014
1074
|
}
|
|
1015
1075
|
|
|
@@ -1021,19 +1081,18 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1021
1081
|
}
|
|
1022
1082
|
|
|
1023
1083
|
const tmpDids = Array.isArray(did) ? did : [did];
|
|
1024
|
-
const [rootDid,
|
|
1084
|
+
const [rootDid, childDid] = tmpDids;
|
|
1025
1085
|
const rootMetaDid = await states.blocklet.getBlockletMetaDid(rootDid);
|
|
1026
|
-
const dids = [rootMetaDid, ...childDids];
|
|
1027
1086
|
|
|
1028
|
-
logger.info('config blocklet', {
|
|
1087
|
+
logger.info('config blocklet', { rootDid, rootMetaDid, childDid });
|
|
1029
1088
|
|
|
1030
1089
|
const ancestors = [];
|
|
1031
1090
|
let blocklet = await this.getBlocklet(rootDid);
|
|
1032
|
-
|
|
1091
|
+
if (childDid) {
|
|
1033
1092
|
ancestors.push(blocklet);
|
|
1034
1093
|
blocklet = blocklet.children.find((x) => x.meta.did === childDid);
|
|
1035
1094
|
if (!blocklet) {
|
|
1036
|
-
throw new Error('Child blocklet does not exist', {
|
|
1095
|
+
throw new Error('Child blocklet does not exist', { rootDid, childDid });
|
|
1037
1096
|
}
|
|
1038
1097
|
}
|
|
1039
1098
|
|
|
@@ -1046,8 +1105,8 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1046
1105
|
await environmentNameSchema.validateAsync(x.key);
|
|
1047
1106
|
} else if (BLOCKLET_CONFIGURABLE_KEY[x.key] && x.key.startsWith('BLOCKLET_')) {
|
|
1048
1107
|
// app key
|
|
1049
|
-
if (
|
|
1050
|
-
logger.error(`Cannot set ${x.key} to child blocklet`,
|
|
1108
|
+
if (childDid) {
|
|
1109
|
+
logger.error(`Cannot set ${x.key} to child blocklet`, { rootDid, childDid });
|
|
1051
1110
|
throw new Error(`Cannot set ${x.key} to child blocklet`);
|
|
1052
1111
|
}
|
|
1053
1112
|
await validateAppConfig(x, states);
|
|
@@ -1086,7 +1145,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1086
1145
|
Object.assign(blocklet.configObj, configObj);
|
|
1087
1146
|
|
|
1088
1147
|
// update db
|
|
1089
|
-
await states.blockletExtras.setConfigs(
|
|
1148
|
+
await states.blockletExtras.setConfigs([rootMetaDid, childDid].filter(Boolean), newConfigs);
|
|
1090
1149
|
|
|
1091
1150
|
if (willAppSkChange) {
|
|
1092
1151
|
const info = await states.node.read();
|
|
@@ -1106,7 +1165,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1106
1165
|
}
|
|
1107
1166
|
|
|
1108
1167
|
// update blocklet meta
|
|
1109
|
-
if (blocklet.structVersion && !
|
|
1168
|
+
if (blocklet.structVersion && !childDid) {
|
|
1110
1169
|
const changedTitle = newConfigs.find((x) => x.key === BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_NAME)?.value;
|
|
1111
1170
|
const changedDescription = newConfigs.find(
|
|
1112
1171
|
(x) => x.key === BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_DESCRIPTION
|
|
@@ -1135,6 +1194,13 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1135
1194
|
await this._updateDidDocument(newState);
|
|
1136
1195
|
}
|
|
1137
1196
|
|
|
1197
|
+
if (!childDid && !newConfigs.some((x) => x.key === BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_SK)) {
|
|
1198
|
+
this.emit(BlockletInternalEvents.appConfigChanged, {
|
|
1199
|
+
appDid: newState.appDid,
|
|
1200
|
+
configs: newConfigs.map((x) => ({ key: x.key, value: x.value })),
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1138
1204
|
this.emit(BlockletEvents.updated, newState);
|
|
1139
1205
|
|
|
1140
1206
|
return newState;
|
|
@@ -1311,6 +1377,10 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1311
1377
|
await states.blocklet.updateBlocklet(rootDid, { mountPoint: blocklet.mountPoint, children: blocklet.children });
|
|
1312
1378
|
|
|
1313
1379
|
this.emit(BlockletEvents.upgraded, { blocklet, context: { ...context, createAuditLog: false } }); // trigger router refresh
|
|
1380
|
+
this.emit(BlockletInternalEvents.componentsUpdated, {
|
|
1381
|
+
appDid: blocklet.appDid,
|
|
1382
|
+
components: getComponentsInternalInfo(blocklet),
|
|
1383
|
+
});
|
|
1314
1384
|
|
|
1315
1385
|
return this.getBlocklet(rootDid);
|
|
1316
1386
|
}
|
|
@@ -1329,15 +1399,15 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1329
1399
|
});
|
|
1330
1400
|
}
|
|
1331
1401
|
|
|
1332
|
-
|
|
1402
|
+
ensureBlocklet(did, opts = {}) {
|
|
1333
1403
|
return getBlocklet({ ...opts, states, dataDirs: this.dataDirs, did, ensureIntegrity: true });
|
|
1334
1404
|
}
|
|
1335
1405
|
|
|
1336
|
-
|
|
1406
|
+
getBlocklet(did, opts = {}) {
|
|
1337
1407
|
return getBlocklet({ ...opts, states, dataDirs: this.dataDirs, did, ensureIntegrity: false });
|
|
1338
1408
|
}
|
|
1339
1409
|
|
|
1340
|
-
|
|
1410
|
+
hasBlocklet({ did }) {
|
|
1341
1411
|
return states.blocklet.hasBlocklet(did);
|
|
1342
1412
|
}
|
|
1343
1413
|
|
|
@@ -1369,60 +1439,77 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1369
1439
|
return this.getBlocklet(did);
|
|
1370
1440
|
}
|
|
1371
1441
|
|
|
1372
|
-
async status(did, { forceSync = false } = {}) {
|
|
1373
|
-
const
|
|
1374
|
-
const { status } = blocklet;
|
|
1375
|
-
const { added, waiting, downloading, installing, installed, upgrading } = BlockletStatus;
|
|
1442
|
+
async status(did, { forceSync = false, componentDids } = {}) {
|
|
1443
|
+
const blocklet = await this.getBlocklet(did);
|
|
1376
1444
|
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1445
|
+
// for backward compatibility
|
|
1446
|
+
if (!blocklet.structVersion) {
|
|
1447
|
+
return blocklet;
|
|
1448
|
+
}
|
|
1380
1449
|
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1450
|
+
// {Record<BlockletStatus, Array<Did>>}
|
|
1451
|
+
const updates = new Map();
|
|
1452
|
+
const addToUpdates = (componentDid, status) => {
|
|
1453
|
+
if (updates.has(status)) {
|
|
1454
|
+
updates.get(status).push(componentDid);
|
|
1455
|
+
} else {
|
|
1456
|
+
updates.set(status, [componentDid]);
|
|
1457
|
+
}
|
|
1384
1458
|
};
|
|
1385
1459
|
|
|
1386
|
-
|
|
1460
|
+
await forEachComponentV2(
|
|
1461
|
+
blocklet,
|
|
1462
|
+
async (component) => {
|
|
1463
|
+
if (!forceSync && !util.shouldUpdateBlockletStatus(component.status)) {
|
|
1464
|
+
return;
|
|
1465
|
+
}
|
|
1387
1466
|
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
// FIXME @linchen 在彻底修复 blocklet status 稳定性之后, uptime check 应该去掉. 如果 hook 执行时间很长, uptime check 会影响 hook 执行过程中的 blocklet status
|
|
1392
|
-
if (uptime > 120 * 1000) {
|
|
1393
|
-
shouldUpdateStatus = true;
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1467
|
+
if (shouldSkipComponent(component.meta.did, componentDids)) {
|
|
1468
|
+
return;
|
|
1469
|
+
}
|
|
1396
1470
|
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1471
|
+
try {
|
|
1472
|
+
const status = await getProcessState(component.env.processId);
|
|
1473
|
+
const oldStatus = component.status;
|
|
1474
|
+
if (component.status !== status) {
|
|
1475
|
+
component.status = status;
|
|
1476
|
+
addToUpdates(component.meta.did, status);
|
|
1477
|
+
logger.info('will sync status from pm2', { did, status, oldStatus, componentDid: component.meta.did });
|
|
1478
|
+
}
|
|
1479
|
+
} catch {
|
|
1480
|
+
if (
|
|
1481
|
+
![
|
|
1482
|
+
BlockletStatus.added,
|
|
1483
|
+
BlockletStatus.waiting,
|
|
1484
|
+
BlockletStatus.downloading,
|
|
1485
|
+
BlockletStatus.installing,
|
|
1486
|
+
BlockletStatus.installed,
|
|
1487
|
+
BlockletStatus.upgrading,
|
|
1488
|
+
].includes(component.status)
|
|
1489
|
+
) {
|
|
1490
|
+
const status = BlockletStatus.stopped;
|
|
1491
|
+
component.status = status;
|
|
1492
|
+
addToUpdates(component.meta.did, status);
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
},
|
|
1496
|
+
{ parallel: true }
|
|
1497
|
+
);
|
|
1400
1498
|
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
) {
|
|
1407
|
-
const res = await states.blocklet.setBlockletStatus(did, BlockletStatus.stopped);
|
|
1408
|
-
this.emit(BlockletEvents.statusChange, res);
|
|
1409
|
-
return res;
|
|
1499
|
+
// iterate updates
|
|
1500
|
+
if (updates.size > 0) {
|
|
1501
|
+
for (const [status, dids] of updates) {
|
|
1502
|
+
logger.info('sync status from pm2', { did, status, componentDids: dids });
|
|
1503
|
+
await states.blocklet.setBlockletStatus(did, status, { componentDids: dids });
|
|
1410
1504
|
}
|
|
1505
|
+
}
|
|
1411
1506
|
|
|
1412
|
-
|
|
1413
|
-
if (blocklet.status !== status) {
|
|
1414
|
-
const res = await states.blocklet.setBlockletStatus(did, status);
|
|
1415
|
-
this.emit(BlockletEvents.statusChange, res);
|
|
1416
|
-
return res;
|
|
1417
|
-
}
|
|
1507
|
+
blocklet.status = getBlockletStatus(blocklet);
|
|
1418
1508
|
|
|
1419
|
-
|
|
1420
|
-
} catch (err) {
|
|
1421
|
-
return fastReturnOnForceSync(blocklet);
|
|
1422
|
-
}
|
|
1509
|
+
return blocklet;
|
|
1423
1510
|
}
|
|
1424
1511
|
|
|
1425
|
-
|
|
1512
|
+
refreshListCache() {
|
|
1426
1513
|
this.list({ useCache: false }).catch((err) => {
|
|
1427
1514
|
logger.error('refresh blocklet list failed', { error: err });
|
|
1428
1515
|
});
|
|
@@ -1486,6 +1573,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1486
1573
|
name: 'sync-blocklet-status',
|
|
1487
1574
|
time: '*/60 * * * * *', // 60s
|
|
1488
1575
|
fn: this._syncBlockletStatus.bind(this),
|
|
1576
|
+
options: { runOnInit: false },
|
|
1489
1577
|
},
|
|
1490
1578
|
{
|
|
1491
1579
|
name: 'sync-blocklet-list',
|
|
@@ -1495,22 +1583,29 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1495
1583
|
{
|
|
1496
1584
|
name: 'refresh-accessible-ip',
|
|
1497
1585
|
time: '0 */10 * * * *', // 10min
|
|
1586
|
+
options: { runOnInit: true },
|
|
1498
1587
|
fn: async () => {
|
|
1499
1588
|
const nodeInfo = await states.node.read();
|
|
1500
1589
|
await refreshAccessibleExternalNodeIp(nodeInfo);
|
|
1501
1590
|
},
|
|
1502
1591
|
},
|
|
1503
1592
|
{
|
|
1504
|
-
name: '
|
|
1593
|
+
name: 'check-renewed-blocklet',
|
|
1594
|
+
time: '0 */10 * * * *', // 10min
|
|
1595
|
+
options: { runOnInit: false },
|
|
1596
|
+
fn: () => this.checkRenewedBlocklet(),
|
|
1597
|
+
},
|
|
1598
|
+
{
|
|
1599
|
+
name: 'stop-expired-external-blocklet',
|
|
1505
1600
|
time: '0 */30 * * * *', // 30min
|
|
1506
1601
|
options: { runOnInit: false },
|
|
1507
|
-
fn: () => this.
|
|
1602
|
+
fn: () => this.stopExpiredBlocklets(),
|
|
1508
1603
|
},
|
|
1509
1604
|
{
|
|
1510
1605
|
name: 'clean-expired-blocklet-data',
|
|
1511
1606
|
time: '0 */20 0 * * *', // 每天凌晨 0 点的每 20 分钟
|
|
1512
1607
|
options: { runOnInit: false },
|
|
1513
|
-
fn: () => this.
|
|
1608
|
+
fn: () => this.cleanExpiredBlocklets(),
|
|
1514
1609
|
},
|
|
1515
1610
|
{
|
|
1516
1611
|
name: 'record-blocklet-runtime-history',
|
|
@@ -1548,14 +1643,20 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1548
1643
|
|
|
1549
1644
|
/**
|
|
1550
1645
|
* @description
|
|
1551
|
-
* @param {import('@abtnode/client').RequestAddBlockletSpaceGatewayInput} { did,
|
|
1646
|
+
* @param {import('@abtnode/client').RequestAddBlockletSpaceGatewayInput} { did, spaceGatewayDid }
|
|
1552
1647
|
* @return {Promise<void>}
|
|
1553
1648
|
* @memberof BlockletManager
|
|
1554
1649
|
*/
|
|
1555
|
-
async deleteBlockletSpaceGateway({ did,
|
|
1650
|
+
async deleteBlockletSpaceGateway({ did, spaceGatewayDid }) {
|
|
1556
1651
|
const spaceGateways = await this.getBlockletSpaceGateways({ did });
|
|
1557
1652
|
|
|
1558
|
-
const latestSpaceGateways = spaceGateways.filter((s) =>
|
|
1653
|
+
const latestSpaceGateways = spaceGateways.filter((s) => {
|
|
1654
|
+
if (spaceGatewayDid) {
|
|
1655
|
+
return s?.did !== spaceGatewayDid;
|
|
1656
|
+
}
|
|
1657
|
+
// note: 兼容性处理,所有没有 did 的 spaceGateway 统一会被删除
|
|
1658
|
+
return !isUndefined(s?.did);
|
|
1659
|
+
});
|
|
1559
1660
|
|
|
1560
1661
|
await states.blockletExtras.setSettings(did, { spaceGateways: latestSpaceGateways });
|
|
1561
1662
|
}
|
|
@@ -1610,7 +1711,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1610
1711
|
* @return {Promise<Array<import('@abtnode/client').Backup>>}
|
|
1611
1712
|
* @memberof BlockletManager
|
|
1612
1713
|
*/
|
|
1613
|
-
|
|
1714
|
+
getBlockletBackups({ did }) {
|
|
1614
1715
|
return states.backup.getBlockletBackups({ did });
|
|
1615
1716
|
}
|
|
1616
1717
|
|
|
@@ -1636,7 +1737,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1636
1737
|
* oldBlocklet: {},
|
|
1637
1738
|
* throwOnError: Error,
|
|
1638
1739
|
* skipCheckStatusBeforeDownload: boolean,
|
|
1639
|
-
*
|
|
1740
|
+
* componentDids: Array<did>,
|
|
1640
1741
|
* }} params
|
|
1641
1742
|
* @return {*}
|
|
1642
1743
|
* @memberof BlockletManager
|
|
@@ -1649,7 +1750,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1649
1750
|
oldBlocklet,
|
|
1650
1751
|
throwOnError,
|
|
1651
1752
|
skipCheckStatusBeforeDownload,
|
|
1652
|
-
|
|
1753
|
+
componentDids,
|
|
1653
1754
|
skipCheckIntegrity,
|
|
1654
1755
|
} = params;
|
|
1655
1756
|
const { meta } = blocklet;
|
|
@@ -1686,8 +1787,8 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1686
1787
|
const blockletForDownload = {
|
|
1687
1788
|
...blocklet,
|
|
1688
1789
|
children: (blocklet.children || []).filter((x) => {
|
|
1689
|
-
if (
|
|
1690
|
-
return
|
|
1790
|
+
if (componentDids?.length) {
|
|
1791
|
+
return componentDids.includes(x.meta.did);
|
|
1691
1792
|
}
|
|
1692
1793
|
return x;
|
|
1693
1794
|
}),
|
|
@@ -1758,12 +1859,12 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1758
1859
|
}
|
|
1759
1860
|
|
|
1760
1861
|
if (postAction === 'install') {
|
|
1761
|
-
const state = await states.blocklet.setBlockletStatus(did, BlockletStatus.installing);
|
|
1862
|
+
const state = await states.blocklet.setBlockletStatus(did, BlockletStatus.installing, { componentDids });
|
|
1762
1863
|
this.emit(BlockletEvents.statusChange, state);
|
|
1763
1864
|
}
|
|
1764
1865
|
|
|
1765
1866
|
if (postAction === 'upgrade') {
|
|
1766
|
-
const state = await states.blocklet.setBlockletStatus(did, BlockletStatus.upgrading);
|
|
1867
|
+
const state = await states.blocklet.setBlockletStatus(did, BlockletStatus.upgrading, { componentDids });
|
|
1767
1868
|
this.emit(BlockletEvents.statusChange, state);
|
|
1768
1869
|
}
|
|
1769
1870
|
|
|
@@ -1777,13 +1878,13 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1777
1878
|
try {
|
|
1778
1879
|
// install blocklet
|
|
1779
1880
|
if (postAction === 'install') {
|
|
1780
|
-
await this._onInstall({ blocklet, context, oldBlocklet });
|
|
1881
|
+
await this._onInstall({ blocklet, componentDids, context, oldBlocklet });
|
|
1781
1882
|
return;
|
|
1782
1883
|
}
|
|
1783
1884
|
|
|
1784
1885
|
// upgrade blocklet
|
|
1785
1886
|
if (postAction === 'upgrade') {
|
|
1786
|
-
await this._onUpgrade({ oldBlocklet, newBlocklet: blocklet, context });
|
|
1887
|
+
await this._onUpgrade({ oldBlocklet, componentDids, newBlocklet: blocklet, context });
|
|
1787
1888
|
}
|
|
1788
1889
|
} catch (error) {
|
|
1789
1890
|
if (throwOnError) {
|
|
@@ -1792,7 +1893,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1792
1893
|
}
|
|
1793
1894
|
}
|
|
1794
1895
|
|
|
1795
|
-
async _onInstall({ blocklet, context, oldBlocklet }) {
|
|
1896
|
+
async _onInstall({ blocklet, componentDids, context, oldBlocklet }) {
|
|
1796
1897
|
const { meta } = blocklet;
|
|
1797
1898
|
const { did, version } = meta;
|
|
1798
1899
|
logger.info('do install blocklet', { did, version });
|
|
@@ -1800,6 +1901,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1800
1901
|
try {
|
|
1801
1902
|
const installedBlocklet = await this._installBlocklet({
|
|
1802
1903
|
did,
|
|
1904
|
+
componentDids,
|
|
1803
1905
|
context,
|
|
1804
1906
|
oldBlocklet,
|
|
1805
1907
|
});
|
|
@@ -1807,7 +1909,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1807
1909
|
if (context.startImmediately && installedBlocklet?.settings.initialized) {
|
|
1808
1910
|
try {
|
|
1809
1911
|
logger.info('start blocklet after installed', { did });
|
|
1810
|
-
await this.start({ did, checkHealthImmediately: true });
|
|
1912
|
+
await this.start({ did, checkHealthImmediately: true, componentDids });
|
|
1811
1913
|
} catch (error) {
|
|
1812
1914
|
logger.warn('attempt to start immediately failed', { did, error });
|
|
1813
1915
|
}
|
|
@@ -1820,7 +1922,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1820
1922
|
}
|
|
1821
1923
|
}
|
|
1822
1924
|
|
|
1823
|
-
async _onUpgrade({ oldBlocklet, newBlocklet, context }) {
|
|
1925
|
+
async _onUpgrade({ oldBlocklet, newBlocklet, componentDids, context }) {
|
|
1824
1926
|
const { version, did } = newBlocklet.meta;
|
|
1825
1927
|
logger.info('do upgrade blocklet', { did, version });
|
|
1826
1928
|
|
|
@@ -1828,6 +1930,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1828
1930
|
await this._upgradeBlocklet({
|
|
1829
1931
|
newBlocklet,
|
|
1830
1932
|
oldBlocklet,
|
|
1933
|
+
componentDids,
|
|
1831
1934
|
context,
|
|
1832
1935
|
});
|
|
1833
1936
|
} catch (err) {
|
|
@@ -1835,13 +1938,13 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1835
1938
|
}
|
|
1836
1939
|
}
|
|
1837
1940
|
|
|
1838
|
-
async _onRestart({ did, context }) {
|
|
1839
|
-
await this.stop({ did, updateStatus: false }, context);
|
|
1840
|
-
await this.start({ did }, context);
|
|
1941
|
+
async _onRestart({ did, componentDids, context }) {
|
|
1942
|
+
await this.stop({ did, componentDids, updateStatus: false }, context);
|
|
1943
|
+
await this.start({ did, componentDids }, context);
|
|
1841
1944
|
}
|
|
1842
1945
|
|
|
1843
1946
|
async _onCheckIfStarted(jobInfo, { throwOnError } = {}) {
|
|
1844
|
-
const { did, context, minConsecutiveTime = 5000, timeout } = jobInfo;
|
|
1947
|
+
const { did, context, minConsecutiveTime = 5000, timeout, componentDids } = jobInfo;
|
|
1845
1948
|
const blocklet = await this.getBlocklet(did);
|
|
1846
1949
|
|
|
1847
1950
|
const { meta } = blocklet;
|
|
@@ -1849,10 +1952,18 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1849
1952
|
|
|
1850
1953
|
try {
|
|
1851
1954
|
// healthy check
|
|
1852
|
-
await checkBlockletProcessHealthy(blocklet, { minConsecutiveTime, timeout });
|
|
1955
|
+
await checkBlockletProcessHealthy(blocklet, { minConsecutiveTime, timeout, componentDids });
|
|
1853
1956
|
|
|
1854
1957
|
// update blocklet status after healthy check
|
|
1855
|
-
|
|
1958
|
+
await states.blocklet.setBlockletStatus(did, BlockletStatus.running, { componentDids });
|
|
1959
|
+
|
|
1960
|
+
const res = await this.getBlocklet(did);
|
|
1961
|
+
|
|
1962
|
+
this.emit(BlockletInternalEvents.componentsUpdated, {
|
|
1963
|
+
appDid: blocklet.appDid,
|
|
1964
|
+
components: getComponentsInternalInfo(res),
|
|
1965
|
+
});
|
|
1966
|
+
|
|
1856
1967
|
this.emit(BlockletEvents.started, res);
|
|
1857
1968
|
} catch (error) {
|
|
1858
1969
|
const status = await states.blocklet.getBlockletStatus(did);
|
|
@@ -1864,7 +1975,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
1864
1975
|
logger.error('check blocklet if started failed', { did, name, context, timeout, error });
|
|
1865
1976
|
|
|
1866
1977
|
await this.deleteProcess({ did }, context);
|
|
1867
|
-
await states.blocklet.setBlockletStatus(did, BlockletStatus.error);
|
|
1978
|
+
await states.blocklet.setBlockletStatus(did, BlockletStatus.error, { componentDids });
|
|
1868
1979
|
|
|
1869
1980
|
this._createNotification(did, {
|
|
1870
1981
|
title: 'Blocklet Start Failed',
|
|
@@ -2256,7 +2367,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2256
2367
|
* @param {string} opt.did
|
|
2257
2368
|
* @param {object} opt.context
|
|
2258
2369
|
*/
|
|
2259
|
-
async _installBlocklet({ did, oldBlocklet, context, createNotification = true }) {
|
|
2370
|
+
async _installBlocklet({ did, oldBlocklet, componentDids, context, createNotification = true }) {
|
|
2260
2371
|
try {
|
|
2261
2372
|
// should ensure blocklet integrity
|
|
2262
2373
|
let blocklet = await this.ensureBlocklet(did);
|
|
@@ -2280,7 +2391,9 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2280
2391
|
// post install
|
|
2281
2392
|
await this._runPostInstallHook(blocklet, context);
|
|
2282
2393
|
|
|
2283
|
-
await states.blocklet.
|
|
2394
|
+
await states.blocklet.setInstalledAt(did);
|
|
2395
|
+
|
|
2396
|
+
await states.blocklet.setBlockletStatus(did, BlockletStatus.installed, { componentDids });
|
|
2284
2397
|
blocklet = await this.getBlocklet(did);
|
|
2285
2398
|
logger.info('blocklet installed', { source, did: meta.did });
|
|
2286
2399
|
|
|
@@ -2308,14 +2421,22 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2308
2421
|
});
|
|
2309
2422
|
}
|
|
2310
2423
|
|
|
2311
|
-
if (blocklet
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
})
|
|
2424
|
+
if (blocklet.controller && process.env.NODE_ENV !== 'test') {
|
|
2425
|
+
let isNFTConsumed = false;
|
|
2426
|
+
if (blocklet.controller.launcherSessionId && blocklet.controller.launcherUrl) {
|
|
2427
|
+
await consumeLauncherSession({ params: blocklet.controller, blocklet });
|
|
2428
|
+
isNFTConsumed = true;
|
|
2429
|
+
} else if (blocklet.controller.nftId) {
|
|
2430
|
+
await consumeServerlessNFT({ nftId: blocklet.controller.nftId, blocklet });
|
|
2431
|
+
isNFTConsumed = true;
|
|
2432
|
+
}
|
|
2317
2433
|
|
|
2318
|
-
|
|
2434
|
+
if (isNFTConsumed) {
|
|
2435
|
+
await states.blockletExtras.updateByDid(did, {
|
|
2436
|
+
controller: { ...blocklet.controller, consumedAt: new Date().toISOString() },
|
|
2437
|
+
});
|
|
2438
|
+
this.emit(BlockletEvents.nftConsumed, { blocklet, context });
|
|
2439
|
+
}
|
|
2319
2440
|
}
|
|
2320
2441
|
|
|
2321
2442
|
if (createNotification) {
|
|
@@ -2361,7 +2482,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2361
2482
|
}
|
|
2362
2483
|
}
|
|
2363
2484
|
|
|
2364
|
-
async _upgradeBlocklet({ newBlocklet, oldBlocklet, context = {} }) {
|
|
2485
|
+
async _upgradeBlocklet({ newBlocklet, oldBlocklet, componentDids, context = {} }) {
|
|
2365
2486
|
const { meta, source, deployedFrom, children } = newBlocklet;
|
|
2366
2487
|
const { did, version, name, title } = meta;
|
|
2367
2488
|
|
|
@@ -2384,6 +2505,8 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2384
2505
|
|
|
2385
2506
|
// update state
|
|
2386
2507
|
await states.blocklet.upgradeBlocklet({ meta, source, deployedFrom, children });
|
|
2508
|
+
// ensure component status is upgrading
|
|
2509
|
+
await states.blocklet.setBlockletStatus(did, BlockletStatus.upgrading, { componentDids });
|
|
2387
2510
|
await this._setConfigsFromMeta(did);
|
|
2388
2511
|
|
|
2389
2512
|
// should ensure blocklet integrity
|
|
@@ -2424,14 +2547,14 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2424
2547
|
|
|
2425
2548
|
logger.info('updated blocklet for upgrading', { did, version, source, name });
|
|
2426
2549
|
|
|
2427
|
-
const status =
|
|
2428
|
-
oldBlocklet.status === BlockletStatus.installed ? BlockletStatus.installed : BlockletStatus.stopped;
|
|
2429
|
-
await states.blocklet.setBlockletStatus(did, status, { children: 'all' });
|
|
2430
|
-
|
|
2431
|
-
// start new process
|
|
2432
2550
|
if (oldBlocklet.status === BlockletStatus.running) {
|
|
2433
|
-
|
|
2551
|
+
// start new process
|
|
2552
|
+
await this.start({ did, componentDids }, context);
|
|
2434
2553
|
logger.info('started blocklet for upgrading', { did, version });
|
|
2554
|
+
} else {
|
|
2555
|
+
const status =
|
|
2556
|
+
oldBlocklet.status === BlockletStatus.installed ? BlockletStatus.installed : BlockletStatus.stopped;
|
|
2557
|
+
await states.blocklet.setBlockletStatus(did, status, { componentDids });
|
|
2435
2558
|
}
|
|
2436
2559
|
|
|
2437
2560
|
blocklet = await this.getBlocklet(did, context);
|
|
@@ -2461,6 +2584,11 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2461
2584
|
|
|
2462
2585
|
await this._rollbackCache.remove({ did: blocklet.meta.did });
|
|
2463
2586
|
|
|
2587
|
+
this.emit(BlockletInternalEvents.componentsUpdated, {
|
|
2588
|
+
appDid: blocklet.appDid,
|
|
2589
|
+
components: getComponentsInternalInfo(blocklet),
|
|
2590
|
+
});
|
|
2591
|
+
|
|
2464
2592
|
return blocklet;
|
|
2465
2593
|
} catch (err) {
|
|
2466
2594
|
const b = await this._rollback('upgrade', did, oldBlocklet);
|
|
@@ -2507,7 +2635,7 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2507
2635
|
* @return {*}
|
|
2508
2636
|
* @memberof BlockletManager
|
|
2509
2637
|
*/
|
|
2510
|
-
|
|
2638
|
+
_downloadBlocklet(blocklet, context = {}) {
|
|
2511
2639
|
const {
|
|
2512
2640
|
appDid,
|
|
2513
2641
|
meta: { did },
|
|
@@ -2517,12 +2645,14 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2517
2645
|
...context,
|
|
2518
2646
|
preDownload: async ({ downloadComponentIds }) => {
|
|
2519
2647
|
// update children status
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2648
|
+
if (downloadComponentIds?.length) {
|
|
2649
|
+
const blocklet1 = await states.blocklet.setBlockletStatus(did, BlockletStatus.downloading, {
|
|
2650
|
+
componentDids: downloadComponentIds,
|
|
2651
|
+
});
|
|
2652
|
+
this.emit(BlockletEvents.statusChange, blocklet1);
|
|
2653
|
+
}
|
|
2524
2654
|
},
|
|
2525
|
-
onProgress:
|
|
2655
|
+
onProgress: (data) => {
|
|
2526
2656
|
this.emit(BlockletEvents.downloadBundleProgress, { appDid: appDid || did, meta: { did }, ...data });
|
|
2527
2657
|
},
|
|
2528
2658
|
postDownload: async ({ isCancelled }) => {
|
|
@@ -2534,16 +2664,17 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2534
2664
|
});
|
|
2535
2665
|
}
|
|
2536
2666
|
|
|
2537
|
-
async _syncPm2Status(pm2Status, did) {
|
|
2667
|
+
async _syncPm2Status(pm2Status, did, componentDid) {
|
|
2538
2668
|
try {
|
|
2539
|
-
const
|
|
2540
|
-
|
|
2669
|
+
const blocklet = await states.blocklet.getBlocklet(did);
|
|
2670
|
+
const component = findComponentByIdV2(blocklet, componentDid);
|
|
2671
|
+
if (component && util.shouldUpdateBlockletStatus(component.status)) {
|
|
2541
2672
|
const newStatus = pm2StatusMap[pm2Status];
|
|
2542
|
-
await states.blocklet.setBlockletStatus(did, newStatus);
|
|
2543
|
-
logger.info('sync pm2 status to blocklet', { did, pm2Status, newStatus });
|
|
2673
|
+
await states.blocklet.setBlockletStatus(did, newStatus, { componentDids: [componentDid] });
|
|
2674
|
+
logger.info('sync pm2 status to blocklet', { did, componentDid, pm2Status, newStatus });
|
|
2544
2675
|
}
|
|
2545
2676
|
} catch (error) {
|
|
2546
|
-
logger.error('sync pm2 status to blocklet failed', { did, pm2Status, error });
|
|
2677
|
+
logger.error('sync pm2 status to blocklet failed', { did, componentDid, pm2Status, error });
|
|
2547
2678
|
}
|
|
2548
2679
|
}
|
|
2549
2680
|
|
|
@@ -2764,71 +2895,102 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2764
2895
|
}
|
|
2765
2896
|
}
|
|
2766
2897
|
|
|
2767
|
-
async
|
|
2898
|
+
async checkRenewedBlocklet() {
|
|
2899
|
+
try {
|
|
2900
|
+
logger.info('start check renewed blocklet');
|
|
2901
|
+
|
|
2902
|
+
// 只检查因为过期而被 suspended 的 blocklet
|
|
2903
|
+
const blockletExtras = await states.blockletExtras.find(
|
|
2904
|
+
{
|
|
2905
|
+
'controller.status.value': BLOCKLET_CONTROLLER_STATUS.suspended,
|
|
2906
|
+
'controller.status.reason': SUSPENDED_REASON.expired,
|
|
2907
|
+
},
|
|
2908
|
+
{ did: 1, meta: 1, controller: 1 }
|
|
2909
|
+
);
|
|
2910
|
+
|
|
2911
|
+
for (const data of blockletExtras) {
|
|
2912
|
+
const blocklet = await states.blocklet.getBlocklet(data.did);
|
|
2913
|
+
if (!blocklet) {
|
|
2914
|
+
logger.error('blocklet not found', { did: data.did });
|
|
2915
|
+
// eslint-disable-next-line no-continue
|
|
2916
|
+
continue;
|
|
2917
|
+
}
|
|
2918
|
+
|
|
2919
|
+
const assetState = await util.getNFTState(data.controller.chainHost, data.controller.nftId);
|
|
2920
|
+
const isExpired = isNFTExpired(assetState);
|
|
2921
|
+
if (isExpired === false) {
|
|
2922
|
+
logger.info('blocklet is renewed', { did: data.did });
|
|
2923
|
+
await states.blockletExtras.updateByDid(data.did, {
|
|
2924
|
+
...data.controller,
|
|
2925
|
+
status: { value: BLOCKLET_CONTROLLER_STATUS.normal, reason: '' },
|
|
2926
|
+
});
|
|
2927
|
+
|
|
2928
|
+
logger.info('start to start blocklet', { did: data.did });
|
|
2929
|
+
|
|
2930
|
+
if (![BlockletStatus.starting, BlockletStatus.running].includes(blocklet.status)) {
|
|
2931
|
+
await this.start({ did: data.did });
|
|
2932
|
+
logger.info('start blocklet success', { did: data.did });
|
|
2933
|
+
}
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
} catch (error) {
|
|
2937
|
+
logger.error('start check renewed blocklet failed', { error });
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2940
|
+
|
|
2941
|
+
async stopExpiredBlocklets() {
|
|
2768
2942
|
try {
|
|
2769
|
-
logger.info('start
|
|
2943
|
+
logger.info('start checking expired blocklet');
|
|
2770
2944
|
const blockletExtras = await states.blockletExtras.find(
|
|
2771
2945
|
{
|
|
2772
2946
|
controller: {
|
|
2773
2947
|
$exists: true,
|
|
2774
2948
|
},
|
|
2775
|
-
expiredAt: {
|
|
2776
|
-
$exists: false,
|
|
2777
|
-
},
|
|
2778
2949
|
},
|
|
2779
2950
|
{ did: 1, meta: 1, controller: 1 }
|
|
2780
2951
|
);
|
|
2781
2952
|
|
|
2782
|
-
|
|
2953
|
+
if (blockletExtras.length === 0) {
|
|
2954
|
+
logger.info('no expired blocklet');
|
|
2955
|
+
return;
|
|
2956
|
+
}
|
|
2957
|
+
|
|
2958
|
+
logger.info('expired blocklet count', { count: blockletExtras.length });
|
|
2783
2959
|
|
|
2784
2960
|
for (const data of blockletExtras) {
|
|
2785
2961
|
try {
|
|
2786
|
-
if (isEmpty(data.controller)) {
|
|
2787
|
-
logger.info('skip the blocklet without controller', { blockletDid: data.did, controller: data.controller });
|
|
2788
|
-
// eslint-disable-next-line no-continue
|
|
2789
|
-
continue;
|
|
2790
|
-
}
|
|
2791
|
-
|
|
2792
2962
|
const { did } = data;
|
|
2793
2963
|
const assetState = await util.getNFTState(data.controller.chainHost, data.controller.nftId);
|
|
2794
2964
|
const isExpired = isNFTExpired(assetState);
|
|
2965
|
+
const blocklet = await states.blocklet.getBlocklet(did);
|
|
2795
2966
|
|
|
2796
|
-
if (isExpired) {
|
|
2797
|
-
|
|
2967
|
+
if (isExpired && blocklet.status !== BlockletStatus.stopped) {
|
|
2968
|
+
const expiredAt = getNftExpirationDate(assetState);
|
|
2969
|
+
logger.info('the blocklet already expired and will be stopped', {
|
|
2798
2970
|
blockletDid: did,
|
|
2799
2971
|
nftId: data.controller.nftId,
|
|
2972
|
+
expiredAt,
|
|
2800
2973
|
});
|
|
2801
2974
|
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
await this.delete({ did, keepData: true, keepConfigs: true, keepLogsDir: true });
|
|
2812
|
-
logger.info('the expired blocklet already deleted', {
|
|
2813
|
-
blockletDid: did,
|
|
2814
|
-
nftId: data.controller.nftId,
|
|
2815
|
-
});
|
|
2816
|
-
}
|
|
2817
|
-
|
|
2818
|
-
const expiredAt = getNftExpirationDate(assetState);
|
|
2819
|
-
await states.blockletExtras.updateExpireInfo({ did, expiredAt });
|
|
2820
|
-
logger.info('updated expired blocklet extra info', {
|
|
2821
|
-
nftId: data.controller.nftId,
|
|
2822
|
-
blockletDid: did,
|
|
2975
|
+
await this.stop({ did });
|
|
2976
|
+
await states.blockletExtras.updateByDid(did, {
|
|
2977
|
+
controller: {
|
|
2978
|
+
...data.controller,
|
|
2979
|
+
status: {
|
|
2980
|
+
value: BLOCKLET_CONTROLLER_STATUS.suspended,
|
|
2981
|
+
reason: SUSPENDED_REASON.expired,
|
|
2982
|
+
},
|
|
2983
|
+
},
|
|
2823
2984
|
});
|
|
2985
|
+
logger.info('the expired blocklet is stopped', { did });
|
|
2824
2986
|
|
|
2825
|
-
//
|
|
2987
|
+
// 为了减小服务器压力,每次删除一个 blocklet 后,休息 10s
|
|
2826
2988
|
if (process.env.NODE_ENV !== 'test') {
|
|
2827
2989
|
await sleep(10 * 1000);
|
|
2828
2990
|
}
|
|
2829
2991
|
}
|
|
2830
2992
|
} catch (error) {
|
|
2831
|
-
logger.error('
|
|
2993
|
+
logger.error('stop expired blocklet failed', {
|
|
2832
2994
|
blockletDid: data.did,
|
|
2833
2995
|
nftId: data.controller?.nftId,
|
|
2834
2996
|
error,
|
|
@@ -2842,62 +3004,68 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
2842
3004
|
}
|
|
2843
3005
|
}
|
|
2844
3006
|
|
|
2845
|
-
async
|
|
3007
|
+
async cleanExpiredBlocklets() {
|
|
2846
3008
|
try {
|
|
2847
|
-
logger.info('start
|
|
2848
|
-
const blockletExtras = await states.blockletExtras.
|
|
3009
|
+
logger.info('start checking exceeding the redemption period blocklet');
|
|
3010
|
+
const blockletExtras = await states.blockletExtras.find(
|
|
3011
|
+
{
|
|
3012
|
+
'controller.status.value': BLOCKLET_CONTROLLER_STATUS.suspended,
|
|
3013
|
+
},
|
|
3014
|
+
{ did: 1, meta: 1, controller: 1 }
|
|
3015
|
+
);
|
|
3016
|
+
|
|
2849
3017
|
if (blockletExtras.length === 0) {
|
|
2850
|
-
logger.info('no
|
|
3018
|
+
logger.info('no exceeding the redemption blocklet need to be cleaned');
|
|
2851
3019
|
return;
|
|
2852
3020
|
}
|
|
2853
3021
|
|
|
2854
|
-
|
|
2855
|
-
const blocklet = await this.getBlocklet(did);
|
|
2856
|
-
if (blocklet) {
|
|
2857
|
-
logger.error('skip cleaning the blocklet which is not deleted', { blockletDid: did });
|
|
2858
|
-
return;
|
|
2859
|
-
}
|
|
2860
|
-
|
|
2861
|
-
const assetState = await util.getNFTState(controller.chainHost, controller.nftId);
|
|
2862
|
-
const expirationDate = getNftExpirationDate(assetState);
|
|
3022
|
+
logger.info('exceeding the redemption blocklet count', { count: blockletExtras.length });
|
|
2863
3023
|
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
let blockletMeta = meta;
|
|
2871
|
-
if (isEmpty(blockletMeta)) {
|
|
2872
|
-
// 旧版本的 blocklet 没有 meta 信息,这里补充一下
|
|
2873
|
-
blockletMeta = { did, name: did };
|
|
2874
|
-
logger.error('the blocklet original meta is empty', { did, blockletMeta });
|
|
2875
|
-
}
|
|
3024
|
+
for (const data of blockletExtras) {
|
|
3025
|
+
try {
|
|
3026
|
+
const { did } = data;
|
|
3027
|
+
const assetState = await util.getNFTState(data.controller.chainHost, data.controller.nftId);
|
|
3028
|
+
const expiredAt = getNftExpirationDate(assetState);
|
|
2876
3029
|
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
3030
|
+
if (!exceedRedemptionPeriod(expiredAt)) {
|
|
3031
|
+
logger.error('skip cleaning the non-exceed redemption blocklet', {
|
|
3032
|
+
blockletDid: did,
|
|
3033
|
+
expiredAt,
|
|
3034
|
+
});
|
|
3035
|
+
// eslint-disable-next-line no-continue
|
|
3036
|
+
continue;
|
|
3037
|
+
}
|
|
2884
3038
|
|
|
2885
|
-
|
|
2886
|
-
|
|
3039
|
+
logger.info('the blocklet already exceed redemption and will be deleted', {
|
|
3040
|
+
blockletDid: did,
|
|
3041
|
+
nftId: data.controller.nftId,
|
|
3042
|
+
expiredAt,
|
|
3043
|
+
});
|
|
2887
3044
|
|
|
2888
|
-
|
|
2889
|
-
blocklet: { meta: blockletMeta },
|
|
2890
|
-
keepRouting: false,
|
|
2891
|
-
});
|
|
3045
|
+
// TODO: 如果绑定了 DID Space 备份到 DID Space
|
|
2892
3046
|
|
|
2893
|
-
|
|
2894
|
-
|
|
3047
|
+
await this.delete({ did, keepData: false, keepConfigs: false, keepLogsDir: false });
|
|
3048
|
+
logger.info('the exceed redemption blocklet already deleted', {
|
|
3049
|
+
blockletDid: did,
|
|
3050
|
+
nftId: data.controller.nftId,
|
|
3051
|
+
});
|
|
2895
3052
|
|
|
2896
|
-
|
|
3053
|
+
// 删除 blocklet 后会 reload nginx, 所以这里每次删除一个
|
|
3054
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
3055
|
+
await sleep(10 * 1000);
|
|
3056
|
+
}
|
|
3057
|
+
} catch (error) {
|
|
3058
|
+
logger.error('delete exceed redemption blocklet failed', {
|
|
3059
|
+
blockletDid: data.did,
|
|
3060
|
+
nftId: data.controller?.nftId,
|
|
3061
|
+
error,
|
|
3062
|
+
});
|
|
3063
|
+
}
|
|
3064
|
+
}
|
|
2897
3065
|
|
|
2898
|
-
logger.info('
|
|
3066
|
+
logger.info('check exceeding the redemption period blocklet end');
|
|
2899
3067
|
} catch (error) {
|
|
2900
|
-
logger.error('
|
|
3068
|
+
logger.error('checking exceeding the redemption period blocklet failed', { error });
|
|
2901
3069
|
}
|
|
2902
3070
|
}
|
|
2903
3071
|
|
|
@@ -3101,6 +3269,38 @@ class BlockletManager extends BaseBlockletManager {
|
|
|
3101
3269
|
}
|
|
3102
3270
|
await this._setConfigsFromMeta(did);
|
|
3103
3271
|
}
|
|
3272
|
+
|
|
3273
|
+
async checkControllerStatus(blocklet, action) {
|
|
3274
|
+
if (isEmpty(blocklet.controller)) {
|
|
3275
|
+
return;
|
|
3276
|
+
}
|
|
3277
|
+
|
|
3278
|
+
const assetState = await util.getNFTState(blocklet.controller?.chainHost, blocklet.controller?.nftId);
|
|
3279
|
+
|
|
3280
|
+
if (isNFTExpired(assetState)) {
|
|
3281
|
+
logger.error(`try to ${action} an expired blocklet`, {
|
|
3282
|
+
did: blocklet.meta.did,
|
|
3283
|
+
nftId: blocklet.controller?.nftId,
|
|
3284
|
+
});
|
|
3285
|
+
throw new Error(`can not ${action} an expired blocklet`);
|
|
3286
|
+
}
|
|
3287
|
+
|
|
3288
|
+
if (
|
|
3289
|
+
blocklet.controller.status?.value === BLOCKLET_CONTROLLER_STATUS.suspended &&
|
|
3290
|
+
blocklet.controller.status?.reason === SUSPENDED_REASON.expired
|
|
3291
|
+
) {
|
|
3292
|
+
// 如果是因为过期被暂停的,续期后, 并且不影响启动
|
|
3293
|
+
await states.blockletExtras.updateByDid(blocklet.meta.did, {
|
|
3294
|
+
controller: {
|
|
3295
|
+
...blocklet.controller,
|
|
3296
|
+
status: {
|
|
3297
|
+
value: BLOCKLET_CONTROLLER_STATUS.normal,
|
|
3298
|
+
reason: '',
|
|
3299
|
+
},
|
|
3300
|
+
},
|
|
3301
|
+
});
|
|
3302
|
+
}
|
|
3303
|
+
}
|
|
3104
3304
|
}
|
|
3105
3305
|
|
|
3106
3306
|
module.exports = BlockletManager;
|