mirror of
https://github.com/stronk-dev/OrchestratorTracker.git
synced 2025-07-05 02:45:10 +02:00
Safe writing
This commit is contained in:
parent
a81ccb7165
commit
0f30cf4a27
@ -75,6 +75,7 @@ let jsonString = "";
|
||||
let lastLeaderboardCheck = 0;
|
||||
let lastStringify = 0;
|
||||
let isSynced = false;
|
||||
let storageLock = false;
|
||||
|
||||
/*
|
||||
* Function Skeletons
|
||||
@ -87,6 +88,25 @@ function sleep(ms) {
|
||||
});
|
||||
}
|
||||
|
||||
async function withStorageLock(fn) {
|
||||
while (storageLock) {
|
||||
await sleep(50); // Small delay before retry
|
||||
}
|
||||
storageLock = true;
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
storageLock = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function safeWrite(key, data) {
|
||||
const tempKey = `${key}.tmp`;
|
||||
await storage.setItem(tempKey, data); // Write to temp key
|
||||
await storage.removeItem(key); // Remove original key
|
||||
await storage.setItem(key, data); // Rename temp key to original
|
||||
}
|
||||
|
||||
// Process the task queue continuously
|
||||
async function processQueue() {
|
||||
while (true) {
|
||||
@ -141,7 +161,9 @@ async function getEnsDomain(addr) {
|
||||
ensObj.timestamp
|
||||
);
|
||||
ensDomainCache[addr] = ensObj;
|
||||
await storage.setItem("ensDomainCache", ensDomainCache);
|
||||
await withStorageLock(async () => {
|
||||
await safeWrite("ensDomainCache", ensDomainCache);
|
||||
});
|
||||
if (ensObj.domain) {
|
||||
// Update domain name
|
||||
return ensObj.domain;
|
||||
@ -383,7 +405,9 @@ async function onOrchUpdate(id, obj, tag, region, livepeer_regions) {
|
||||
newObj.instances[obj.resolv.resolvedTarget] = newInstance;
|
||||
newObj.regionalStats[tag] = newRegion;
|
||||
orchCache[id.toLowerCase()] = newObj;
|
||||
await storage.setItem("orchCache", orchCache);
|
||||
await withStorageLock(async () => {
|
||||
await safeWrite("orchCache", orchCache);
|
||||
});
|
||||
|
||||
// Update prometheus stats
|
||||
updatePrometheus(tag, obj.resolv.resolvedTarget, newObj);
|
||||
|
Loading…
x
Reference in New Issue
Block a user