@abtnode/core 1.16.20-beta-e363262e → 1.16.20-beta-ce2cd157

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.
@@ -94,6 +94,7 @@ const pRetry = require('p-retry');
94
94
 
95
95
  const isFunction = require('lodash/isFunction');
96
96
  const { encode } = require('@abtnode/util/lib/base32');
97
+ const formatContext = require('@abtnode/util/lib/format-context');
97
98
  const { consumeServerlessNFT, consumeLauncherSession } = require('../../util/launcher');
98
99
  const util = require('../../util');
99
100
  const {
@@ -190,6 +191,7 @@ const {
190
191
  updateSelectedResources,
191
192
  } = require('../project');
192
193
  const { callFederated } = require('../../util/federated');
194
+ const { isDnsIpMappingCorrect } = require('../../util/ip');
193
195
 
194
196
  const { formatEnvironments, getBlockletMeta, validateOwner, isCLI } = util;
195
197
 
@@ -270,6 +272,44 @@ class DiskBlockletManager extends BaseBlockletManager {
270
272
  }
271
273
  }
272
274
 
275
+ async initialize() {
276
+ await this.ensureAutoBackupJobs();
277
+ }
278
+
279
+ async ensureAutoBackupJobs() {
280
+ const blocklets = await states.blockletExtras.find({
281
+ 'settings.autoBackup.enabled': true,
282
+ });
283
+
284
+ const info = await states.node.read();
285
+
286
+ await Promise.all(
287
+ blocklets.map(async (x) => {
288
+ const { did } = x;
289
+ const jobId = getBackupJobId(did);
290
+ const job = await this.backupQueue.get(jobId);
291
+
292
+ if (job) {
293
+ return;
294
+ }
295
+
296
+ this.backupQueue.push(
297
+ {
298
+ entity: 'blocklet',
299
+ action: 'backupToSpaces',
300
+ did,
301
+ context: formatContext({
302
+ user: { did: info.did },
303
+ }),
304
+ },
305
+ jobId,
306
+ true,
307
+ BACKUPS.JOB.INTERVAL
308
+ );
309
+ })
310
+ );
311
+ }
312
+
273
313
  // ============================================================================================
274
314
  // Public API for Installing/Upgrading Application or Components
275
315
  // ============================================================================================
@@ -2320,7 +2360,31 @@ class DiskBlockletManager extends BaseBlockletManager {
2320
2360
  * }} { blocklet, context }
2321
2361
  * @memberof BlockletManager
2322
2362
  */
2323
- async _onBackupToSpaces({ did, context, backupState }) {
2363
+ async _onBackupToSpaces({
2364
+ did,
2365
+ context,
2366
+ backupState = {
2367
+ strategy: BACKUPS.STRATEGY.AUTO,
2368
+ },
2369
+ }) {
2370
+ const blocklet = await states.blocklet.getBlocklet(did);
2371
+ const {
2372
+ appDid,
2373
+ meta: { did: appPid },
2374
+ } = blocklet;
2375
+
2376
+ if (backupState?.strategy === BACKUPS.STRATEGY.AUTO && blocklet.status === BlockletStatus.stopped) {
2377
+ // 自动备份时,应用停止运行就跳过本次备份
2378
+ logger.warn('Skip automatic backups because the application is not running.', { appPid });
2379
+ return;
2380
+ }
2381
+
2382
+ const correct = await isDnsIpMappingCorrect(blocklet.meta.did); // dns/ip 映射错误就跳过本次备份
2383
+ if (!correct) {
2384
+ logger.warn('Skipping automatic backups due to dns mapping errors', { appPid });
2385
+ return;
2386
+ }
2387
+
2324
2388
  const {
2325
2389
  user: { did: userDid, locale },
2326
2390
  } = context;
@@ -2339,12 +2403,6 @@ class DiskBlockletManager extends BaseBlockletManager {
2339
2403
  );
2340
2404
  }
2341
2405
 
2342
- const blocklet = await states.blocklet.getBlocklet(did);
2343
- const {
2344
- appDid,
2345
- meta: { did: appPid },
2346
- } = blocklet;
2347
-
2348
2406
  let backup = await states.backup.findOne({ appPid }, {}, { createdAt: -1 });
2349
2407
  if (backup?.status !== BACKUPS.STATUS.PROGRESS) {
2350
2408
  // 创建备份记录
package/lib/index.js CHANGED
@@ -628,6 +628,15 @@ function ABTNode(options) {
628
628
  logger.info('Cron jobs start successfully on daemon start');
629
629
  }
630
630
  }, 1000);
631
+
632
+ blockletManager
633
+ .initialize()
634
+ .then(() => {
635
+ logger.info('blockletManager initialized');
636
+ })
637
+ .catch((error) => {
638
+ logger.error('blockletManager initialize failed', { error });
639
+ });
631
640
  }
632
641
  });
633
642
 
@@ -69,6 +69,7 @@ const init = (dataDirs, config = {}) => {
69
69
  * blockletExtras: import('./blocklet-extras'),
70
70
  * notification: import('./notification'),
71
71
  * job: import('./job'),
72
+ * node: import('./node'),
72
73
  * [key: string]: any
73
74
  * }}
74
75
  */
@@ -58,7 +58,7 @@ class NodeState extends BaseState {
58
58
  /**
59
59
  * Ensure we have an node record in the database
60
60
  *
61
- * @returns {object} Node document json
61
+ * @returns {Promise<import('@abtnode/client').NodeState>} Node document json
62
62
  * @memberof NodeState
63
63
  */
64
64
  async read() {
package/lib/util/ip.js CHANGED
@@ -1,5 +1,12 @@
1
+ const getIP = require('@abtnode/util/lib/get-ip');
1
2
  const getIp = require('@abtnode/util/lib/get-ip');
3
+ const { encode } = require('@abtnode/util/lib/base32');
4
+ const { DEFAULT_DID_DOMAIN } = require('@abtnode/constant');
2
5
  const logger = require('@abtnode/logger')('@abtnode/core:ip');
6
+ const dns = require('dns');
7
+ const { promisify } = require('util');
8
+
9
+ const lookup = promisify(dns.lookup);
3
10
 
4
11
  let cache = null;
5
12
  const fetch = async () => {
@@ -23,6 +30,20 @@ const cron = {
23
30
  options: { runOnInit: true, runInService: true },
24
31
  };
25
32
 
33
+ async function isDnsIpMappingCorrect(did) {
34
+ try {
35
+ const { internal, external } = await getIP();
36
+ const didDomain = `${encode(did)}.${DEFAULT_DID_DOMAIN}`;
37
+ const { address } = await lookup(didDomain, { rrtype: 'A' });
38
+
39
+ return internal === address || external === address;
40
+ } catch (error) {
41
+ logger.error(error);
42
+ return false;
43
+ }
44
+ }
45
+
26
46
  module.exports.fetch = fetch;
27
47
  module.exports.get = get;
28
48
  module.exports.cron = cron;
49
+ module.exports.isDnsIpMappingCorrect = isDnsIpMappingCorrect;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.20-beta-e363262e",
6
+ "version": "1.16.20-beta-ce2cd157",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,19 +19,19 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "Apache-2.0",
21
21
  "dependencies": {
22
- "@abtnode/analytics": "1.16.20-beta-e363262e",
23
- "@abtnode/auth": "1.16.20-beta-e363262e",
24
- "@abtnode/certificate-manager": "1.16.20-beta-e363262e",
25
- "@abtnode/constant": "1.16.20-beta-e363262e",
26
- "@abtnode/cron": "1.16.20-beta-e363262e",
27
- "@abtnode/logger": "1.16.20-beta-e363262e",
28
- "@abtnode/models": "1.16.20-beta-e363262e",
29
- "@abtnode/queue": "1.16.20-beta-e363262e",
30
- "@abtnode/rbac": "1.16.20-beta-e363262e",
31
- "@abtnode/router-provider": "1.16.20-beta-e363262e",
32
- "@abtnode/static-server": "1.16.20-beta-e363262e",
33
- "@abtnode/timemachine": "1.16.20-beta-e363262e",
34
- "@abtnode/util": "1.16.20-beta-e363262e",
22
+ "@abtnode/analytics": "1.16.20-beta-ce2cd157",
23
+ "@abtnode/auth": "1.16.20-beta-ce2cd157",
24
+ "@abtnode/certificate-manager": "1.16.20-beta-ce2cd157",
25
+ "@abtnode/constant": "1.16.20-beta-ce2cd157",
26
+ "@abtnode/cron": "1.16.20-beta-ce2cd157",
27
+ "@abtnode/logger": "1.16.20-beta-ce2cd157",
28
+ "@abtnode/models": "1.16.20-beta-ce2cd157",
29
+ "@abtnode/queue": "1.16.20-beta-ce2cd157",
30
+ "@abtnode/rbac": "1.16.20-beta-ce2cd157",
31
+ "@abtnode/router-provider": "1.16.20-beta-ce2cd157",
32
+ "@abtnode/static-server": "1.16.20-beta-ce2cd157",
33
+ "@abtnode/timemachine": "1.16.20-beta-ce2cd157",
34
+ "@abtnode/util": "1.16.20-beta-ce2cd157",
35
35
  "@arcblock/did": "1.18.103",
36
36
  "@arcblock/did-auth": "1.18.103",
37
37
  "@arcblock/did-ext": "^1.18.103",
@@ -42,11 +42,11 @@
42
42
  "@arcblock/pm2-events": "^0.0.5",
43
43
  "@arcblock/validator": "^1.18.103",
44
44
  "@arcblock/vc": "1.18.103",
45
- "@blocklet/constant": "1.16.20-beta-e363262e",
46
- "@blocklet/env": "1.16.20-beta-e363262e",
47
- "@blocklet/meta": "1.16.20-beta-e363262e",
48
- "@blocklet/resolver": "1.16.20-beta-e363262e",
49
- "@blocklet/sdk": "1.16.20-beta-e363262e",
45
+ "@blocklet/constant": "1.16.20-beta-ce2cd157",
46
+ "@blocklet/env": "1.16.20-beta-ce2cd157",
47
+ "@blocklet/meta": "1.16.20-beta-ce2cd157",
48
+ "@blocklet/resolver": "1.16.20-beta-ce2cd157",
49
+ "@blocklet/sdk": "1.16.20-beta-ce2cd157",
50
50
  "@did-space/client": "^0.3.41",
51
51
  "@fidm/x509": "^1.2.1",
52
52
  "@ocap/mcrypto": "1.18.103",
@@ -101,5 +101,5 @@
101
101
  "jest": "^27.5.1",
102
102
  "unzipper": "^0.10.11"
103
103
  },
104
- "gitHead": "479cea04dd620d8194d6221b2594b6a0f390ba76"
104
+ "gitHead": "a081d3e4fa2a6d51f343df07eb631fa9300d7538"
105
105
  }