Skip to content

Commit

Permalink
update lock logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Sep 9, 2024
1 parent 16ae5b4 commit a2dd60e
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,37 @@ export class Database {
const ipfsDataLockId = generateLockId(this.ipfsDataSchemaName);
const priceDataLockId = generateLockId(this.priceDataSchemaName);

// Lock acquisition status
let chainDataLockAcquired = false;
let ipfsDataLockAcquired = false;
let priceDataLockAcquired = false;

try {
const chainDataLockAcquired = await acquireLockForSchema(chainDataLockId);
const ipfsDataLockAcquired = await acquireLockForSchema(ipfsDataLockId);
const priceDataLockAcquired = await acquireLockForSchema(priceDataLockId);

if (
chainDataLockAcquired &&
ipfsDataLockAcquired &&
priceDataLockAcquired
) {
return {
release: async () => {
chainDataLockAcquired = await acquireLockForSchema(chainDataLockId);
ipfsDataLockAcquired = await acquireLockForSchema(ipfsDataLockId);
priceDataLockAcquired = await acquireLockForSchema(priceDataLockId);

return {
release: async () => {
if (chainDataLockAcquired)
await releaseLockForSchema(chainDataLockId);
await releaseLockForSchema(ipfsDataLockId);
if (ipfsDataLockAcquired) await releaseLockForSchema(ipfsDataLockId);
if (priceDataLockAcquired)
await releaseLockForSchema(priceDataLockId);
client.release();
},
client,
};
}
client.release();
},
client,
};
} catch (error) {
this.#logger.error({ error }, "Failed to acquire write lock");
}
} finally {
// Ensure any acquired locks are released if they were not all acquired
if (chainDataLockAcquired) await releaseLockForSchema(chainDataLockId);
if (ipfsDataLockAcquired) await releaseLockForSchema(ipfsDataLockId);
if (priceDataLockAcquired) await releaseLockForSchema(priceDataLockId);

client.release();
client.release();
}

return null;
}
Expand Down

0 comments on commit a2dd60e

Please sign in to comment.