Resume the preview rebuild where a restart left it.

The job only lived in memory, so every deploy discarded hours of proxy work and
the next run started at the first photo. Mirror progress to the storage root
after each photo and pick the pending ids back up on boot.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-28 06:45:37 +00:00
parent 32616d5404
commit c7b01b31cb
3 changed files with 74 additions and 9 deletions
+52 -5
View File
@@ -974,6 +974,55 @@ route('PUT', '/api/admin/storage', async ({ req, res }) => {
let previewRebuild = null;
/** Progress file, so a restart resumes a run instead of starting it over. */
function previewRebuildFile() {
return path.join(storageRoot(), 'preview-rebuild.json');
}
function savePreviewRebuild(state) {
try {
fs.writeFileSync(previewRebuildFile(), JSON.stringify(state));
} catch {
/* Progress is a convenience; a read-only storage root must not stop a run. */
}
}
/**
* Runs the job and mirrors its progress to disk after every photo. The proxy
* rebuild takes hours on a full library, so losing it to a deploy means
* starting from the first photo again.
*/
function beginPreviewRebuild(targets) {
const job = startPreviewRebuild({
targets,
regenerate: (photo) => regenerateRawPreview(photo.id, photo, photoOwner(photo.id) ?? ''),
onProgress: (snapshot, pending) =>
savePreviewRebuild({ ...snapshot, pending: pending.map((photo) => photo.id) }),
});
savePreviewRebuild({ ...job.snapshot(), pending: targets.map((photo) => photo.id) });
void job.finished.then(() => savePreviewRebuild({ ...job.snapshot(), pending: [] }));
previewRebuild = job;
return job;
}
/** Restarts a run that a restart interrupted, skipping finished photos. */
function resumePreviewRebuild() {
let saved = null;
try {
saved = JSON.parse(fs.readFileSync(previewRebuildFile(), 'utf8'));
} catch {
return;
}
if (saved?.state !== 'running' || !Array.isArray(saved.pending) || saved.pending.length === 0) {
return;
}
const byId = new Map(listPhotos().map((photo) => [photo.id, photo]));
const targets = saved.pending.map((id) => byId.get(id)).filter(Boolean);
if (targets.length === 0) return;
console.log(` previews: resuming rebuild, ${targets.length} photo(s) left`);
beginPreviewRebuild(targets);
}
function previewRebuildStatus() {
return (
previewRebuild?.snapshot() ?? {
@@ -1005,11 +1054,8 @@ route('POST', '/api/admin/previews/rebuild', async ({ req, res }) => {
const current = previewRebuildStatus();
if (current.state === 'running') return json(res, 202, { rebuild: current });
const targets = previewRebuildTargets(listPhotos());
previewRebuild = startPreviewRebuild({
targets,
regenerate: (photo) => regenerateRawPreview(photo.id, photo, photoOwner(photo.id) ?? ''),
});
json(res, 202, { rebuild: previewRebuild.snapshot() });
const rebuild = beginPreviewRebuild(targets);
json(res, 202, { rebuild: rebuild.snapshot() });
});
route('GET', '/api/quota', async ({ req, res }) => {
@@ -2531,6 +2577,7 @@ server.listen(PORT, HOST, async () => {
);
console.log(` users: ${users.count()} account(s)${users.count() === 0 ? ' — first signup becomes admin' : ''}`);
if (!raw.libraw) console.log(' note: install libraw-wasm for full raw demosaic (embedded previews still work)');
resumePreviewRebuild();
});
setInterval(() => {