diff --git a/src/stonks-daemon.cjs b/src/stonks-daemon.cjs index 13fc955..746f9b6 100644 --- a/src/stonks-daemon.cjs +++ b/src/stonks-daemon.cjs @@ -164,10 +164,7 @@ const eveSurfaceDocument = defineDocumentType({ schema: passThroughSchema, global: true, }); -const cultCache = CultCache.builder() - .withRegistry(defineDocumentRegistry(requestEventDocument, marketSnapshotDocument, eveSurfaceDocument)) - .withGenericStore(new SingleFileMessagePackBackingStore(cultCachePath)) - .build(); +let cultCache = createCultCache(); let version = 0; let latestSnapshot = pendingSnapshot("Stonks starting"); @@ -179,7 +176,7 @@ main().catch((error) => { }); async function main() { - await cultCache.pullAllBackingStores(); + await pullCultCacheOrQuarantine(); loadRecentRequestsFromCultCache(); const server = http.createServer(handleHttp); server.on("upgrade", handleUpgrade); @@ -193,6 +190,30 @@ async function main() { }, intervalMs); } +function createCultCache() { + return CultCache.builder() + .withRegistry(defineDocumentRegistry(requestEventDocument, marketSnapshotDocument, eveSurfaceDocument)) + .withGenericStore(new SingleFileMessagePackBackingStore(cultCachePath)) + .build(); +} + +async function pullCultCacheOrQuarantine() { + try { + await cultCache.pullAllBackingStores(); + } catch (error) { + if (!fs.existsSync(cultCachePath)) { + throw error; + } + + const corruptPath = `${cultCachePath}.corrupt-${new Date().toISOString().replace(/[:.]/g, "-")}`; + fs.renameSync(cultCachePath, corruptPath); + console.error(`Stonks CultCache state was unreadable and has been quarantined: ${corruptPath}`); + console.error(`Stonks CultCache read error: ${error.message}`); + cultCache = createCultCache(); + await cultCache.pullAllBackingStores(); + } +} + async function refresh() { latestSnapshot = await marketSnapshot(); currentState = buildState(latestSnapshot);