Clean up any broken staged downloads

This commit is contained in:
Alan Grainger
2026-06-06 21:14:36 +12:00
parent b0e0fe0f95
commit b40383e72e
3 changed files with 93 additions and 2 deletions
+4 -1
View File
@@ -13,7 +13,7 @@ import {
} from './immich'
import crypto from 'crypto'
import { assetBuffer } from './stream/asset'
import { downloadAssets } from './stream/download'
import { downloadAssets, sweepStaleStagingDirs } from './stream/download'
import dayjs from 'dayjs'
import { NextFunction, Request, Response } from 'express-serve-static-core'
import { Asset, AssetType, ImageSize, KeyType } from './types'
@@ -282,4 +282,7 @@ process.on('SIGTERM', () => {
const port = Number(process.env.IPP_PORT) || 3000
const server = app.listen(port, () => {
console.log(dayjs().format() + ' Server started on port ' + port)
// Clean up any zip-download staging dirs left behind by a previous
// run that crashed before its finally block could run
sweepStaleStagingDirs().catch(e => console.error('sweepStaleStagingDirs failed:', e))
})
+26 -1
View File
@@ -16,6 +16,8 @@ import { getFilename } from '../gallery/filename'
import { createLimiter } from '../utils/limiter'
import { createIdleTimeoutStream } from '../utils/idleTimeoutStream'
const STAGING_DIR_PREFIX = 'ipp-zip-'
/**
* Download all assets in a share as a zip file.
*/
@@ -23,6 +25,29 @@ export async function downloadAll (res: Response, share: SharedLink) {
await downloadAssets(res, share, share.assets)
}
/**
* Delete staging directories left over from a prior run that crashed before
* its `finally` block could clean up. Intended to run once at startup.
*
* A live download holds its staging dir open for the duration of the zip;
* `maxAgeMs` should comfortably exceed any realistic download time so we
* never delete a dir from a still-running download in another worker.
*/
export async function sweepStaleStagingDirs (maxAgeMs = 60 * 60 * 1000) {
const root = tmpdir()
const cutoff = Date.now() - maxAgeMs
const entries = await fs.readdir(root).catch(() => [] as string[])
for (const name of entries) {
if (!name.startsWith(STAGING_DIR_PREFIX)) continue
const path = join(root, name)
const stat = await fs.stat(path).catch(() => null)
if (!stat || stat.mtimeMs >= cutoff) continue
await fs.rm(path, { recursive: true, force: true }).catch(e => {
log.warn(`Failed to sweep stale staging dir ${path}: ${e instanceof Error ? e.message : String(e)}`)
})
}
}
type StagedAsset = { tempfile: string, asset: Asset, endpoint: ImageEndpoint }
type Failure = { asset: Asset, url: string, status?: number, error?: unknown }
type StageOutcome = StagedAsset | { failure: Failure } | null
@@ -70,7 +95,7 @@ export async function downloadAssets (res: Response, share: SharedLink, assets:
archive.pipe(res)
const options: StagingOptions = {
stagingDir: await fs.mkdtemp(join(tmpdir(), 'ipp-zip-')),
stagingDir: await fs.mkdtemp(join(tmpdir(), STAGING_DIR_PREFIX)),
concurrency: Math.max(1, getConfigOption('ipp.downloadFromImmichConcurrencyLimit', 8) as number),
maxAttempts: 3,
headerTimeoutMs: 20_000,