Quarantine unreadable Stonks cache on startup

This commit is contained in:
Meta Vens 2026-06-15 20:50:56 +01:00
commit 6873a10de0

View file

@ -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);