Skip to content

Commit

Permalink
fix: removing throw
Browse files Browse the repository at this point in the history
  • Loading branch information
silkroadnomad committed Jan 23, 2025
1 parent a81aec6 commit de8cda8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libp2p-relay",
"version": "0.12.60",
"version": "0.12.62",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
25 changes: 10 additions & 15 deletions relay/src/pinner/nameOpsFileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ dotenv.config();
let db = null;
let isClosing = false;
const dbType = process.env.DB_TYPE || 'leveldb'; // Default to OrbitDB
const DB_RETRY_ATTEMPTS = 3;
const DB_RETRY_DELAY = 1000; // 1 second delay between retries
const DB_OPERATION_TIMEOUT = 5000; // 5 seconds timeout for operations

const queue = new PQueue({ concurrency: 5 }); // Adjust concurrency as needed

Expand All @@ -23,10 +20,6 @@ class OrbitDBInterface {

async open() {
const dbName = 'nameops';
const dbPath = './orbitdb/nameops';

// Clean up any stale lock files before opening
// await cleanupLockFiles(dbPath);

this.db = await this.orbitdb.open(dbName, {
type: 'documents',
Expand All @@ -38,6 +31,7 @@ class OrbitDBInterface {
}),
});
console.info(`Opened OrbitDB: ${dbName}`);
return this.db;
}

async put(doc) {
Expand All @@ -63,11 +57,9 @@ class OrbitDBInterface {
}

isOpen() {
return this.db &&
this.db._oplog &&
this.db._cache &&
this.db._cache._store &&
this.db._replicationStatus === 'replicating';
console.log("isOpen", this.db);
return this.db

}
}

Expand Down Expand Up @@ -100,13 +92,16 @@ class LevelDBInterface {
}

export async function getOrCreateDB(orbitdb) {
if (db && db.isOpen && db.isOpen()) {
console.log("getOrCreateDB", db);
if (db && db.isOpen()) {
return db;
}

if (dbType === 'orbitdb') {
db = new OrbitDBInterface(orbitdb);
await db.open();
const orbitdb = new OrbitDBInterface(orbitdb);
console.log("orbitdb", orbitdb);
db = await orbitdb.open();
console.log("db", db);
} else if (dbType === 'leveldb') {
db = new LevelDBInterface();
}
Expand Down
3 changes: 2 additions & 1 deletion relay/src/pinner/scanBlockchainForNameOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ async function pinIpfsContent(electrumClient, helia, nameOp, nameId, ipfsUrl) {
console.error(`Error parsing metadata content: ${cid}`, {
error: error.message,
});
throw error;
metadata = metadataContent;
console.log("metadataContent", metadataContent);
}

if (metadata.image) {
Expand Down
3 changes: 1 addition & 2 deletions relay/src/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { setupPubsub } from './pubsubHandler.js';
import TipWatcher from './pinner/tipWatcher.js';
import { generateAndSaveKeyPair } from './utils/keypair.js';


export const CONTENT_TOPIC =
process.env.CONTENT_TOPIC || '/doichain-nfc/1/message/proto';

Expand Down Expand Up @@ -103,7 +102,7 @@ setupPubsub(
CONTENT_TOPIC,
tipWatcher,
);
createHttpServer(helia, orbitdb, electrumClient, tipWatcher);
// createHttpServr(helia, orbitdb, electrumClient, tipWatcher);

const pinningService = new PinningService(helia, orbitdb, electrumClient);
let isScanning = false;
Expand Down
3 changes: 2 additions & 1 deletion relay/src/start-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import logger from './logger.js';
import os from 'os';
import dotenv from 'dotenv';
import telegramBot from './telegram-bot.js';
import { cleanupLockFiles } from './utils/dbUtils.js';

function formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
Expand All @@ -29,7 +30,7 @@ global.MAX_RESTARTS = Math.min(
global.restartCount = 0;
const RESTART_DELAY = 5000;
let lastCrashTime = 0;

cleanupLockFiles("./orbitdb/");
// Add lock file constants
const LOCK_FILE = resolve(__dirname, '../relay.lock');

Expand Down
24 changes: 19 additions & 5 deletions relay/src/utils/dbUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ import logger from '../logger.js';
* @param {string} dbPath - Path to the database directory
*/
export async function cleanupLockFiles(dbPath) {
console.log("cleanupLockFiles under", dbPath);
try {
const lockFilePath = path.join(dbPath, 'log', '_heads', 'LOCK');
if (fs.existsSync(lockFilePath)) {
logger.info(`Found stale lock file at ${lockFilePath}`);
fs.unlinkSync(lockFilePath);
logger.info(`Removed stale lock file at ${lockFilePath}`);
function findAndRemoveLocks(dirPath) {
if (!fs.existsSync(dirPath)) return;

const items = fs.readdirSync(dirPath);
for (const item of items) {
const fullPath = path.join(dirPath, item);
const stat = fs.statSync(fullPath);

if (stat.isDirectory()) {
findAndRemoveLocks(fullPath);
} else if (item === 'LOCK') {
logger.info(`Found lock file at ${fullPath}`);
fs.unlinkSync(fullPath);
logger.info(`Removed lock file at ${fullPath}`);
}
}
}

findAndRemoveLocks(dbPath);
} catch (error) {
logger.error(`Error cleaning up lock files: ${error.message}`);
}
Expand Down

0 comments on commit de8cda8

Please sign in to comment.