fix: set asset status=ready after thumbnail completes

This commit is contained in:
Zac Gaetano 2026-05-15 21:26:22 -04:00
parent 10949bc460
commit 6aff3cabc0

View file

@ -11,7 +11,7 @@ export const thumbnailWorker = async (job) => {
const { assetId, proxyKey, outputKey } = job.data; const { assetId, proxyKey, outputKey } = job.data;
const tmpDir = tmpdir(); const tmpDir = tmpdir();
const inputPath = join(tmpDir, `thumb-input-${job.id}.mp4`); const inputPath = join(tmpDir, `thumb-input-${job.id}.mp4`);
const outputPath = join(tmpDir, `thumb-output-${job.id}.jpg`); const outputPath = join(tmpDir, `thumb-output-${job.id}.jpg`);
try { try {
@ -20,40 +20,38 @@ export const thumbnailWorker = async (job) => {
console.log(`[thumbnail] Downloading ${proxyKey} for asset ${assetId}`); console.log(`[thumbnail] Downloading ${proxyKey} for asset ${assetId}`);
await downloadFromS3(S3_BUCKET, proxyKey, inputPath); await downloadFromS3(S3_BUCKET, proxyKey, inputPath);
// Extract frame at 5 seconds // Extract frame at 5 seconds (or start if clip is short)
job.updateProgress(40); job.updateProgress(40);
console.log(`[thumbnail] Extracting frame for asset ${assetId}`); console.log(`[thumbnail] Extracting frame for asset ${assetId}`);
await extractFrameAtTime(inputPath, outputPath, '00:00:05'); await extractFrameAtTime(inputPath, outputPath, '00:00:05');
// Upload to S3 // Upload thumbnail to S3
job.updateProgress(70); job.updateProgress(70);
console.log(`[thumbnail] Uploading to ${outputKey}`); console.log(`[thumbnail] Uploading to ${outputKey}`);
await uploadToS3(S3_BUCKET, outputKey, outputPath); await uploadToS3(S3_BUCKET, outputKey, outputPath);
// Update database // Update asset: thumbnail key + mark ready
job.updateProgress(90); job.updateProgress(90);
console.log(`[thumbnail] Updating asset record for ${assetId}`);
await query( await query(
'UPDATE assets SET thumbnail_s3_key = $1 WHERE id = $2', `UPDATE assets
SET thumbnail_s3_key = $1, status = 'ready', updated_at = NOW()
WHERE id = $2`,
[outputKey, assetId] [outputKey, assetId]
); );
job.updateProgress(100); job.updateProgress(100);
console.log(`[thumbnail] Asset ${assetId} thumbnail complete`); console.log(`[thumbnail] Asset ${assetId} thumbnail complete → status=ready`);
return { assetId, outputKey }; return { assetId, outputKey };
} catch (error) { } catch (error) {
console.error(`[thumbnail] Error processing asset ${assetId}:`, error); console.error(`[thumbnail] Error processing asset ${assetId}:`, error);
// Don't set status=error just because thumbnail failed — asset is still usable
// Just log and let it be retried
throw error; throw error;
} finally { } finally {
// Cleanup await Promise.all([
try { unlink(inputPath).catch(() => {}),
await Promise.all([ unlink(outputPath).catch(() => {}),
unlink(inputPath).catch(() => {}), ]);
unlink(outputPath).catch(() => {}),
]);
} catch (err) {
console.error(`[thumbnail] Cleanup error for job ${job.id}:`, err);
}
} }
}; };