feat(media): one library for several relays — films in S3, records and live pins in Postgres
FileMediaStore ties a library to one disk on one machine. SHADE_MEDIA_S3_URL + SHADE_MEDIA_PG_URL (or `media serve --s3 --pg`) put sealed bytes in any S3 bucket and the records every instance must agree on in Postgres. - ObjectMediaStore: each upload streams as multipart to its own object key; the commit is an INSERT whose primary key admits one record per id, so racing uploads on any instances are decided once and the loser deletes its object. Delete drops the record first. sweep() reclaims unreferenced objects and dead multipart uploads past a 24 h grace. - S3Bucket: Shade's own SigV4 client. Bun 1.3.5's S3Client buffers a whole upload (512 MiB in, 543 MiB RSS), end(error) does not abort, and slice(0, n).stream() returned the whole object. Here: at most three parts in memory (512 MiB through, 11 MiB RSS), payload-hashed signatures, AbortMultipartUpload, ranged reads checked for length. - PostgresMediaIndex, PostgresLivePinStore, createSharedMediaBackend in @shade/storage-postgres. LivePinStore.put now refuses a key change (LivePinTakenError) in every store, atomically in Postgres. - The container logs a backend it cannot use and runs on without media. Verified: SigV4 reproduces AWS's published example signature; SeaweedFS with signature checking accepts it and refuses a wrong secret (403); Postgres 16: racing inserts over 8 pools (1 of 24 wins), racing pins, BIGINT sizes; two relays over one backend; CLI relays behind a round-robin proxy: upload, bit-identical download, Chromium playback seeked to 24 s with ranges served by both; the bucket holds no plaintext, name or MP4 marker. Suites: media 87, storage-postgres 18, server 55, cli 15, all green. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -313,12 +313,22 @@ if (vaultDisabled) {
|
||||
// both. Same rule as the vault: a media library that lives in RAM loses every
|
||||
// film on the next redeploy, so no directory means no media, said loudly.
|
||||
// Uploads and live channels are closed to everyone but SHADE_MEDIA_OWNERS.
|
||||
// Several instances share one library with SHADE_MEDIA_S3_URL (sealed bytes
|
||||
// in a bucket) + SHADE_MEDIA_PG_URL (records and live pins in Postgres).
|
||||
let mediaService: import('@shade/media/server').MediaService | null = null;
|
||||
const mediaS3 = process.env.SHADE_MEDIA_S3_URL;
|
||||
const mediaPg = process.env.SHADE_MEDIA_PG_URL ?? process.env.SHADE_PREKEY_PG_URL;
|
||||
if (process.env.SHADE_DISABLE_MEDIA === '1') {
|
||||
logger.info('Media disabled (SHADE_DISABLE_MEDIA=1)');
|
||||
} else if (!process.env.SHADE_MEDIA_DIR) {
|
||||
} else if (mediaS3 !== undefined && mediaPg === undefined) {
|
||||
logger.error(
|
||||
'Media not enabled — SHADE_MEDIA_S3_URL needs SHADE_MEDIA_PG_URL: the bucket holds the bytes, ' +
|
||||
'Postgres the records every instance must agree on.',
|
||||
);
|
||||
} else if (!process.env.SHADE_MEDIA_DIR && mediaS3 === undefined) {
|
||||
logger.warn(
|
||||
'Media not enabled — set SHADE_MEDIA_DIR to a directory on a persistent volume. ' +
|
||||
'Media not enabled — set SHADE_MEDIA_DIR to a directory on a persistent volume ' +
|
||||
'(or SHADE_MEDIA_S3_URL + SHADE_MEDIA_PG_URL for a library shared by several instances). ' +
|
||||
'There is no in-memory fallback on purpose: a library that forgets on redeploy is not a library.',
|
||||
);
|
||||
} else {
|
||||
@@ -330,13 +340,42 @@ if (process.env.SHADE_DISABLE_MEDIA === '1') {
|
||||
'Add owner keys (`shade media whoami`), comma-separated.',
|
||||
);
|
||||
}
|
||||
mediaService = createMediaService({ dir: process.env.SHADE_MEDIA_DIR, owners });
|
||||
app.route('/', mediaService.app);
|
||||
logger.info('Media enabled', {
|
||||
routes: ['/v1/media/:id', '/v1/live/:channelId', '/watch/'],
|
||||
dir: process.env.SHADE_MEDIA_DIR,
|
||||
owners: owners === 'any' ? 'any' : owners.length,
|
||||
});
|
||||
let where: string = process.env.SHADE_MEDIA_DIR ?? '';
|
||||
let backend: import('@shade/storage-postgres').SharedMediaBackend | null = null;
|
||||
if (mediaS3 !== undefined) {
|
||||
const { createSharedMediaBackend } = await import('@shade/storage-postgres');
|
||||
try {
|
||||
backend = await createSharedMediaBackend({ s3Url: mediaS3, pgUrl: mediaPg! });
|
||||
} catch (e) {
|
||||
// The rest of the server is still worth running; media says why it is off.
|
||||
logger.error('Media not enabled — the shared backend (SHADE_MEDIA_S3_URL / SHADE_MEDIA_PG_URL) failed', {
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
});
|
||||
}
|
||||
}
|
||||
if (backend !== null) {
|
||||
mediaService = createMediaService({ owners, store: backend.store, pins: backend.pins });
|
||||
where = backend.describe;
|
||||
// Reclaim what crashed uploads left in the bucket. Every instance may
|
||||
// run it; the grace period spares uploads still in flight anywhere.
|
||||
const sweep = () =>
|
||||
backend.sweep().then(
|
||||
(n) => (n.objects + n.uploads > 0 ? logger.info('Media sweep', n) : undefined),
|
||||
(e: unknown) => logger.warn('Media sweep failed', { error: String(e) }),
|
||||
);
|
||||
setTimeout(sweep, 60_000).unref();
|
||||
setInterval(sweep, 6 * 3600_000).unref();
|
||||
} else if (mediaS3 === undefined) {
|
||||
mediaService = createMediaService({ dir: process.env.SHADE_MEDIA_DIR!, owners });
|
||||
}
|
||||
if (mediaService !== null) {
|
||||
app.route('/', mediaService.app);
|
||||
logger.info('Media enabled', {
|
||||
routes: ['/v1/media/:id', '/v1/live/:channelId', '/watch/'],
|
||||
store: where,
|
||||
owners: owners === 'any' ? 'any' : owners.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Optional: Observer + Dashboard ──────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user