Files
splatoon3.ink/app/data/archive.mjs
2026-11-11 22:29:35 -08:00

59 lines
2.0 KiB
JavaScript

// Data snapshots for the archive (storage().archive). The archive is a record on the side: a
// snapshot that cannot be written is a warning, never a failed update.
import * as Sentry from '@sentry/core';
import pLimit from 'p-limit';
import prefixedConsole from '../common/prefixedConsole.mjs';
import { storage } from '../common/storage/index.mjs';
/**
* Write one snapshot now.
* @param {{ key: string, text: string, name: string }} snapshot Archive key, content, and data file name
* @param {{ warn: (...args: unknown[]) => void }} [console]
* @returns {Promise<boolean>} Whether it was written
*/
export async function archiveSnapshot({ key, text, name }, console = prefixedConsole('Archive')) {
try {
await storage().archive.writeText(key, text);
return true;
} catch (e) {
console.warn(`Could not archive ${name}:`, e);
Sentry.captureException(e);
return false;
}
}
const plural = (count, noun) => `${count} ${noun}${count === 1 ? '' : 's'}`;
/**
* Write the snapshots a run held back (see RunLimits' deferArchive), and forget them.
* @param {import('./RunLimits.mjs').default} limits
* @param {{ info: (...args: unknown[]) => void, warn: (...args: unknown[]) => void }} [console]
* @returns {Promise<{ archived: number, failed: number }>} How many were written and not
*/
export async function flushArchive(limits, console = prefixedConsole('Archive')) {
let snapshots = [...limits.heldSnapshots.values()];
limits.heldSnapshots.clear();
if (!snapshots.length) {
return { archived: 0, failed: 0 };
}
console.info(`Uploading ${plural(snapshots.length, 'snapshot')}...`);
let limit = pLimit(4);
let results = await Promise.all(snapshots.map(snapshot => limit(() => archiveSnapshot(snapshot, console))));
let archived = results.filter(Boolean).length;
let failed = results.length - archived;
if (failed) {
console.warn(`Archived ${archived} of ${plural(results.length, 'snapshot')}, ${failed} failed`);
} else {
console.info(`Archived ${plural(archived, 'snapshot')}`);
}
return { archived, failed };
}