Skip to content

Commit

Permalink
Merge pull request #99 from DIG-Network/release/v0.0.1-alpha.109
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.109
  • Loading branch information
MichaelTaylor3D authored Oct 1, 2024
2 parents 446d3a2 + 3061e5c commit 68e3cdd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.109](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.108...v0.0.1-alpha.109) (2024-10-01)


### Bug Fixes

* lockfile setup ([d038935](https://github.com/DIG-Network/dig-chia-sdk/commit/d038935629e53e8047254ef96c3681b7ea79929d))

### [0.0.1-alpha.108](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.107...v0.0.1-alpha.108) (2024-10-01)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.108",
"version": "0.0.1-alpha.109",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
47 changes: 30 additions & 17 deletions src/blockchain/StoreInfoCacheUpdater.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { setInterval } from "timers";
import { FullNodePeer } from "./FullNodePeer";
import { FileCache } from "../utils";
import { DataStoreSerializer } from "./DataStoreSerializer";
import { withTimeout } from "../utils";
import * as lockfile from 'proper-lockfile';
import * as fs from 'fs';
import * as path from 'path';
import { DIG_FOLDER_PATH } from "../utils";

Expand All @@ -26,7 +24,8 @@ export class StoreInfoCacheUpdater {
// Construct lock file path using the path module
this.lockFilePath = path.join(DIG_FOLDER_PATH, 'store-info-cache.lock');

this.startCacheUpdater();
// Start the cache updater using setTimeout
this.scheduleNextUpdate();

// Set up process exit handlers for cleanup
this.setupExitHandlers();
Expand All @@ -39,24 +38,34 @@ export class StoreInfoCacheUpdater {
return StoreInfoCacheUpdater.instance;
}

private startCacheUpdater() {
setInterval(() => this.updateCache(), this.updateInterval);
private scheduleNextUpdate() {
setTimeout(() => this.checkAndUpdateCache(), this.updateInterval);
}

private async updateCache() {
private async checkAndUpdateCache() {
try {
// Ensure the lock file exists before attempting to lock
if (!fs.existsSync(this.lockFilePath)) {
fs.writeFileSync(this.lockFilePath, '');
const isLocked = await lockfile.check(this.lockFilePath, {
stale: this.updateInterval,
});

if (!isLocked) {
await this.updateCache();
}
// Else, lock is held; skip update without logging
} catch (error) {
console.error("Error checking lockfile:", error);
} finally {
// Schedule the next update regardless of whether we updated or not
this.scheduleNextUpdate();
}
}

// Acquire a file lock with stale lock duration matching the update interval
private async updateCache() {
try {
// Attempt to acquire the lock
const release = await lockfile.lock(this.lockFilePath, {
retries: {
retries: 10, // Retry 10 times to acquire the lock
factor: 2,
minTimeout: 100,
maxTimeout: 1000,
retries: 0, // No retries since we already checked the lock
},
stale: this.updateInterval, // Lock expires after the update interval
});
Expand Down Expand Up @@ -126,16 +135,20 @@ export class StoreInfoCacheUpdater {
});
} catch (error) {
console.error(`Failed to update cache for storeId ${storeId}:`, error);
// Optionally handle specific errors or continue with the next storeId
// Continue with the next storeId
}
}
} finally {
// Always release the lock after finishing the update
await this.releaseLock?.();
this.releaseLock = null;
}
} catch (error) {
console.error("Failed to update store cache:", error);
} catch (error: any) {
if (error.code === 'ELOCKED') {
// Another process acquired the lock; skip without logging
} else {
console.error("Failed to update store cache:", error);
}
}
}

Expand Down

0 comments on commit 68e3cdd

Please sign in to comment.