@dignetwork/dig-sdk 0.0.1-alpha.115 → 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,
|
|
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.
|
|
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,34 +63,51 @@ class StoreInfoCacheUpdater {
|
|
|
59
63
|
}
|
|
60
64
|
async startMonitors() {
|
|
61
65
|
try {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
|
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
|
}
|
|
107
|
+
this.isMonitoring = true;
|
|
88
108
|
// Wait for all monitors to settle
|
|
89
109
|
const monitorPromises = Array.from(this.monitors.values());
|
|
110
|
+
console.log("Waiting for all monitor promises to settle...");
|
|
90
111
|
await Promise.all(monitorPromises);
|
|
91
112
|
}
|
|
92
113
|
catch (error) {
|
|
@@ -96,6 +117,7 @@ class StoreInfoCacheUpdater {
|
|
|
96
117
|
// Release the lock
|
|
97
118
|
if (this.releaseLock) {
|
|
98
119
|
try {
|
|
120
|
+
console.log("Releasing lock...");
|
|
99
121
|
await this.releaseLock();
|
|
100
122
|
console.log("Lock released successfully.");
|
|
101
123
|
}
|
|
@@ -103,13 +125,43 @@ class StoreInfoCacheUpdater {
|
|
|
103
125
|
console.error("Error releasing the lock:", releaseError);
|
|
104
126
|
}
|
|
105
127
|
}
|
|
128
|
+
// Clear the lock renewal interval
|
|
129
|
+
if (this.lockRenewalInterval) {
|
|
130
|
+
clearInterval(this.lockRenewalInterval);
|
|
131
|
+
this.lockRenewalInterval = null;
|
|
132
|
+
}
|
|
106
133
|
}
|
|
107
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
|
+
}
|
|
108
159
|
// Monitor a single store's coin
|
|
109
160
|
async monitorStore(storeId) {
|
|
110
161
|
while (this.isMonitoring) {
|
|
111
162
|
let peer = null;
|
|
112
163
|
try {
|
|
164
|
+
console.log(`Monitoring store ${storeId}`);
|
|
113
165
|
// Connect to a peer
|
|
114
166
|
peer = await (0, utils_2.withTimeout)(FullNodePeer_1.FullNodePeer.connect(), 60000, "Timeout connecting to FullNodePeer");
|
|
115
167
|
// Get the latest store info (from cache if available)
|
|
@@ -135,7 +187,6 @@ class StoreInfoCacheUpdater {
|
|
|
135
187
|
let updatedStore, newHeight;
|
|
136
188
|
try {
|
|
137
189
|
// When resolved, sync the store
|
|
138
|
-
//const { latestStore: updatedStore, latestHeight: newHeight } = await withTimeout(
|
|
139
190
|
const storeInfo = await (0, utils_2.withTimeout)(peer.syncStore(latestStore, latestHeight, Buffer.from(latestHash, "hex"), false // withHistory
|
|
140
191
|
), 60000, `Timeout syncing store for storeId ${storeId}`);
|
|
141
192
|
updatedStore = storeInfo.latestStore;
|
|
@@ -177,9 +228,6 @@ class StoreInfoCacheUpdater {
|
|
|
177
228
|
}
|
|
178
229
|
}
|
|
179
230
|
isUnrecoverableError(error) {
|
|
180
|
-
// Determine whether the error is unrecoverable
|
|
181
|
-
// For this example, we'll treat any unexpected error as unrecoverable
|
|
182
|
-
// You can customize this logic based on your application's needs
|
|
183
231
|
return true;
|
|
184
232
|
}
|
|
185
233
|
}
|