@dignetwork/dig-sdk 0.0.1-alpha.116 → 0.0.1-alpha.118

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.
@@ -5,9 +5,11 @@ export declare class StoreInfoCacheUpdater {
5
5
  private lockFilePath;
6
6
  private releaseLock;
7
7
  private isMonitoring;
8
+ private lockRenewalInterval;
8
9
  private constructor();
9
10
  static initInstance(): StoreInfoCacheUpdater;
10
11
  private startMonitors;
12
+ private renewLock;
11
13
  private monitorStore;
12
14
  private isUnrecoverableError;
13
15
  }
@@ -1 +1 @@
1
- {"version":3,"file":"StoreInfoCacheUpdater.d.ts","sourceRoot":"","sources":["../../src/blockchain/StoreInfoCacheUpdater.ts"],"names":[],"mappings":"AAaA,qBAAa,qBAAqB;IAChC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAwB;IAC/C,OAAO,CAAC,cAAc,CAInB;IACH,OAAO,CAAC,QAAQ,CAAyC;IACzD,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,WAAW,CAAsC;IACzD,OAAO,CAAC,YAAY,CAAiB;IAErC,OAAO;WAeO,YAAY,IAAI,qBAAqB;YAQrC,aAAa;YAwDb,YAAY;IAyH1B,OAAO,CAAC,oBAAoB;CAM7B"}
1
+ {"version":3,"file":"StoreInfoCacheUpdater.d.ts","sourceRoot":"","sources":["../../src/blockchain/StoreInfoCacheUpdater.ts"],"names":[],"mappings":"AAaA,qBAAa,qBAAqB;IAChC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAwB;IAC/C,OAAO,CAAC,cAAc,CAIsB;IAC5C,OAAO,CAAC,QAAQ,CAAyC;IACzD,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,WAAW,CAAsC;IACzD,OAAO,CAAC,YAAY,CAAiB;IACrC,OAAO,CAAC,mBAAmB,CAA+B;IAE1D,OAAO;WAiBO,YAAY,IAAI,qBAAqB;YAQrC,aAAa;IA6E3B,OAAO,CAAC,SAAS;YA0BH,YAAY;IAwH1B,OAAO,CAAC,oBAAoB;CAG7B"}
@@ -37,14 +37,18 @@ const path = __importStar(require("path"));
37
37
  const datalayer_driver_1 = require("@dignetwork/datalayer-driver");
38
38
  class StoreInfoCacheUpdater {
39
39
  constructor() {
40
+ this.storeCoinCache = new utils_1.FileCache(`stores`, utils_1.USER_DIR_PATH);
40
41
  this.monitors = new Map();
41
42
  this.releaseLock = null;
42
43
  this.isMonitoring = true;
43
- this.storeCoinCache = new utils_1.FileCache(`stores`, utils_1.USER_DIR_PATH);
44
+ this.lockRenewalInterval = null;
45
+ console.log("Constructor: Initializing StoreInfoCacheUpdater");
44
46
  // Construct lock file path using the path module
45
47
  this.lockFilePath = path.join(utils_1.USER_DIR_PATH, "store-info-cache.lock");
48
+ console.log("Lock file path:", this.lockFilePath);
46
49
  const lockDir = path.dirname(this.lockFilePath);
47
50
  if (!fs_1.default.existsSync(lockDir)) {
51
+ console.log(`Creating lock directory: ${lockDir}`);
48
52
  fs_1.default.mkdirSync(lockDir, { recursive: true });
49
53
  }
50
54
  // Start monitors for existing storeIds
@@ -59,35 +63,51 @@ class StoreInfoCacheUpdater {
59
63
  }
60
64
  async startMonitors() {
61
65
  try {
62
- // Check if the lockfile is already held
63
- const isLocked = await lockfile.check(this.lockFilePath, {
64
- realpath: false,
65
- });
66
- if (isLocked) {
67
- // Another process is already running the monitors; skip starting monitors
68
- console.log("Another process is already running the StoreInfoCacheUpdater.");
69
- return;
66
+ console.log("Checking if lockfile exists...");
67
+ // Check if the lock file exists
68
+ if (!fs_1.default.existsSync(this.lockFilePath)) {
69
+ console.log("Lockfile does not exist. Proceeding without lock.");
70
+ }
71
+ else {
72
+ // Check if the lockfile is already held
73
+ const isLocked = await lockfile.check(this.lockFilePath, {
74
+ realpath: false,
75
+ });
76
+ if (isLocked) {
77
+ console.log("Another process is already running the StoreInfoCacheUpdater.");
78
+ return;
79
+ }
70
80
  }
71
81
  // Attempt to acquire the lock
82
+ console.log("Attempting to acquire lock...");
72
83
  this.releaseLock = await lockfile.lock(this.lockFilePath, {
73
84
  retries: {
74
85
  retries: 0, // No retries since we only need one lock
75
86
  },
76
- stale: 60000, // Lock expires after 1 minute (adjust as needed)
87
+ stale: 60000, // Lock expires after 1 minute
77
88
  realpath: false, // Ensure lockfile uses the exact path
78
89
  });
90
+ console.log("Lock acquired, starting monitors...");
91
+ // Renew the lock every minute by reacquiring it
92
+ this.renewLock();
79
93
  const storeIds = this.storeCoinCache.getCachedKeys();
94
+ console.log(`Found ${storeIds.length} store IDs in cache:`, storeIds);
80
95
  for (const storeId of storeIds) {
81
96
  // Check if a monitor is already running for this storeId
82
97
  if (!this.monitors.has(storeId)) {
98
+ console.log(`Starting monitor for storeId: ${storeId}`);
83
99
  // Start monitoring in the background
84
100
  const monitorPromise = this.monitorStore(storeId);
85
101
  this.monitors.set(storeId, monitorPromise);
86
102
  }
103
+ else {
104
+ console.log(`Monitor already exists for storeId: ${storeId}`);
105
+ }
87
106
  }
88
107
  this.isMonitoring = true;
89
108
  // Wait for all monitors to settle
90
109
  const monitorPromises = Array.from(this.monitors.values());
110
+ console.log("Waiting for all monitor promises to settle...");
91
111
  await Promise.all(monitorPromises);
92
112
  }
93
113
  catch (error) {
@@ -97,6 +117,7 @@ class StoreInfoCacheUpdater {
97
117
  // Release the lock
98
118
  if (this.releaseLock) {
99
119
  try {
120
+ console.log("Releasing lock...");
100
121
  await this.releaseLock();
101
122
  console.log("Lock released successfully.");
102
123
  }
@@ -104,8 +125,37 @@ class StoreInfoCacheUpdater {
104
125
  console.error("Error releasing the lock:", releaseError);
105
126
  }
106
127
  }
128
+ // Clear the lock renewal interval
129
+ if (this.lockRenewalInterval) {
130
+ clearInterval(this.lockRenewalInterval);
131
+ this.lockRenewalInterval = null;
132
+ }
107
133
  }
108
134
  }
135
+ renewLock() {
136
+ // Set up a renewal process that releases and reacquires the lock every minute
137
+ this.lockRenewalInterval = setInterval(async () => {
138
+ try {
139
+ if (this.releaseLock) {
140
+ console.log("Releasing the lock for renewal...");
141
+ await this.releaseLock();
142
+ console.log("Lock released for renewal.");
143
+ }
144
+ // Reacquire the lock
145
+ this.releaseLock = await lockfile.lock(this.lockFilePath, {
146
+ retries: {
147
+ retries: 0, // No retries since we only need one lock
148
+ },
149
+ stale: 60000, // Lock expires after 1 minute
150
+ realpath: false, // Ensure lockfile uses the exact path
151
+ });
152
+ console.log("Lock reacquired for renewal.");
153
+ }
154
+ catch (error) {
155
+ console.error("Error renewing the lock:", error);
156
+ }
157
+ }, 60000); // Renew the lock every 60 seconds
158
+ }
109
159
  // Monitor a single store's coin
110
160
  async monitorStore(storeId) {
111
161
  while (this.isMonitoring) {
@@ -130,14 +180,13 @@ class StoreInfoCacheUpdater {
130
180
  });
131
181
  // Get the coinId associated with the store
132
182
  const coinId = (0, datalayer_driver_1.getCoinId)(latestStore.coin);
133
- console.log(`!!! Waiting for coin to be spent: ${coinId.toString("hex")}`);
183
+ console.log(`Waiting for coin to be spent: ${coinId.toString("hex")}`);
134
184
  // Wait for the coin to be spent
135
185
  await peer.waitForCoinToBeSpent(coinId, latestHeight, Buffer.from(latestHash, "hex"));
136
186
  console.log(`Detected Coin Spend: ${coinId.toString("hex")}`);
137
187
  let updatedStore, newHeight;
138
188
  try {
139
189
  // When resolved, sync the store
140
- //const { latestStore: updatedStore, latestHeight: newHeight } = await withTimeout(
141
190
  const storeInfo = await (0, utils_2.withTimeout)(peer.syncStore(latestStore, latestHeight, Buffer.from(latestHash, "hex"), false // withHistory
142
191
  ), 60000, `Timeout syncing store for storeId ${storeId}`);
143
192
  updatedStore = storeInfo.latestStore;
@@ -179,9 +228,6 @@ class StoreInfoCacheUpdater {
179
228
  }
180
229
  }
181
230
  isUnrecoverableError(error) {
182
- // Determine whether the error is unrecoverable
183
- // For this example, we'll treat any unexpected error as unrecoverable
184
- // You can customize this logic based on your application's needs
185
231
  return true;
186
232
  }
187
233
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.116",
3
+ "version": "0.0.1-alpha.118",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",