Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda`: Cleanup serialized props even in case of a timeout

Jonny Burger65245b5dc1d8
diff --git a/packages/lambda/src/functions/helpers/cleanup-props.ts b/packages/lambda/src/functions/helpers/cleanup-props.ts
new file mode 100644
index 0000000000..c69dc6a6e8
--- /dev/null
+++ b/packages/lambda/src/functions/helpers/cleanup-props.ts
@@ -0,0 +1,28 @@
+import type {SerializedInputProps} from '../../defaults';
+import {
+ cleanupSerializedInputProps,
+ cleanupSerializedResolvedProps,
+} from '../../shared/cleanup-serialized-input-props';
+import {getCurrentRegionInFunction} from './get-current-region';
+
+export const cleanupProps = ({
+ serializedResolvedProps,
+ inputProps,
+}: {
+ serializedResolvedProps: SerializedInputProps;
+ inputProps: SerializedInputProps;
+}): Promise<[number, number]> => {
+ const cleanupSerializedInputPropsProm = cleanupSerializedInputProps({
+ region: getCurrentRegionInFunction(),
+ serialized: inputProps,
+ });
+ const cleanupResolvedInputPropsProm = cleanupSerializedResolvedProps({
+ region: getCurrentRegionInFunction(),
+ serialized: serializedResolvedProps,
+ });
+
+ return Promise.all([
+ cleanupSerializedInputPropsProm,
+ cleanupResolvedInputPropsProm,
+ ]);
+};
diff --git a/packages/lambda/src/functions/helpers/merge-chunks.ts b/packages/lambda/src/functions/helpers/merge-chunks.ts
index be43fb347a..ae8535abc4 100644
--- a/packages/lambda/src/functions/helpers/merge-chunks.ts
+++ b/packages/lambda/src/functions/helpers/merge-chunks.ts
@@ -1,10 +1,6 @@
import type {AudioCodec, LogLevel} from '@remotion/renderer';
import fs from 'fs';
import type {CustomCredentials} from '../../shared/aws-clients';
-import {
- cleanupSerializedInputProps,
- cleanupSerializedResolvedProps,
-} from '../../shared/cleanup-serialized-input-props';
import type {
PostRenderData,
Privacy,
@@ -13,6 +9,7 @@ import type {
} from '../../shared/constants';
import type {DownloadBehavior} from '../../shared/content-disposition-header';
import type {LambdaCodec} from '../../shared/validate-lambda-codec';
+import {cleanupProps} from './cleanup-props';
import {concatVideos} from './concat-videos';
import {createPostRenderData} from './create-post-render-data';
import {getCurrentRegionInFunction} from './get-current-region';
@@ -104,15 +101,9 @@ export const mergeChunksAndFinishRender = async (options: {
errors: options.overallProgress.get().errors,
});
- const cleanupSerializedInputPropsProm = cleanupSerializedInputProps({
- bucketName: options.bucketName,
- region: getCurrentRegionInFunction(),
- serialized: options.inputProps,
- });
- const cleanupResolvedInputPropsProm = cleanupSerializedResolvedProps({
- bucketName: options.bucketName,
- region: getCurrentRegionInFunction(),
- serialized: options.serializedResolvedProps,
+ const cleanupProm = cleanupProps({
+ inputProps: options.inputProps,
+ serializedResolvedProps: options.serializedResolvedProps,
});
const {url: outputUrl} = getOutputUrlFromMetadata(
@@ -126,12 +117,7 @@ export const mergeChunksAndFinishRender = async (options: {
memorySizeInMb: Number(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE),
renderMetadata: options.renderMetadata,
errorExplanations,
- timeToDelete: (
- await Promise.all([
- cleanupSerializedInputPropsProm,
- cleanupResolvedInputPropsProm,
- ])
- ).reduce((a, b) => Math.max(a, b), 0),
+ timeToDelete: (await cleanupProm).reduce((a, b) => Math.max(a, b), 0),
outputFile: {
url: outputUrl,
},
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 2cefaea203..c2c1c8efcc 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -32,6 +32,7 @@ import {validateOutname} from '../shared/validate-outname';
import {validatePrivacy} from '../shared/validate-privacy';
import {planFrameRanges} from './chunk-optimization/plan-frame-ranges';
import {bestFramesPerLambdaParam} from './helpers/best-frames-per-lambda-param';
+import {cleanupProps} from './helpers/cleanup-props';
import {getExpectedOutName} from './helpers/expected-out-name';
import {findOutputFileInBucket} from './helpers/find-output-file-in-bucket';
import {
@@ -56,11 +57,13 @@ const innerLaunchHandler = async ({
params,
options,
overallProgress,
+ registerCleanupTask,
}: {
functionName: string;
params: LambdaPayload;
options: Options;
overallProgress: OverallProgressHelper;
+ registerCleanupTask: (cleanupTask: CleanupTask) => void;
}): Promise<PostRenderData> => {
if (params.type !== LambdaRoutines.launch) {
throw new Error('Expected launch type');
@@ -201,6 +204,13 @@ const innerLaunchHandler = async ({
needsToUpload,
});
+ registerCleanupTask(() => {
+ return cleanupProps({
+ serializedResolvedProps,
+ inputProps: params.inputProps,
+ });
+ });
+
const fps = comp.fps / params.everyNthFrame;
// If for 150 functions, we stream every frame, we DDos ourselves.
@@ -379,6 +389,8 @@ const innerLaunchHandler = async ({
return postRenderData;
};
+type CleanupTask = () => Promise<unknown>;
+
export const launchHandler = async (
params: LambdaPayload,
options: Options,
@@ -398,6 +410,12 @@ export const launchHandler = async (
logLevel: params.logLevel,
};
+ const cleanupTasks: CleanupTask[] = [];
+
+ const registerCleanupTask = (task: CleanupTask) => {
+ cleanupTasks.push(task);
+ };
+
const onTimeout = async () => {
RenderInternals.Log.error(
{indent: false, logLevel: params.logLevel},
@@ -412,6 +430,21 @@ export const launchHandler = async (
return;
}
+ Promise.all(cleanupTasks)
+ .then(() => {
+ RenderInternals.Log.info(
+ {indent: false, logLevel: params.logLevel},
+ 'Ran cleanup tasks',
+ );
+ })
+ .catch((err) => {
+ RenderInternals.Log.error(
+ {indent: false, logLevel: params.logLevel},
+ 'Failed to run cleanup tasks:',
+ err,
+ );
+ });
+
try {
await invokeWebhook(
{
@@ -489,6 +522,7 @@ export const launchHandler = async (
params,
options,
overallProgress,
+ registerCleanupTask,
});
clearTimeout(webhookDueToTimeout);
diff --git a/packages/lambda/src/functions/still.ts b/packages/lambda/src/functions/still.ts
index a425ad4d6b..9dd2a18df1 100644
--- a/packages/lambda/src/functions/still.ts
+++ b/packages/lambda/src/functions/still.ts
@@ -253,7 +253,6 @@ const innerStillHandler = async ({
await Promise.all([
fs.promises.rm(outputPath, {recursive: true}),
cleanupSerializedInputProps({
- bucketName,
region: getCurrentRegionInFunction(),
serialized: lambdaParams.inputProps,
}),
diff --git a/packages/lambda/src/shared/cleanup-serialized-input-props.ts b/packages/lambda/src/shared/cleanup-serialized-input-props.ts
index d1ffc2fbc6..cbb909ac7c 100644
--- a/packages/lambda/src/shared/cleanup-serialized-input-props.ts
+++ b/packages/lambda/src/shared/cleanup-serialized-input-props.ts
@@ -6,11 +6,9 @@ import {inputPropsKey, resolvedPropsKey} from './constants';
export const cleanupSerializedInputProps = async ({
serialized,
region,
- bucketName,
}: {
serialized: SerializedInputProps;
region: AwsRegion;
- bucketName: string;
}): Promise<number> => {
if (serialized.type === 'payload') {
return 0;
@@ -18,7 +16,7 @@ export const cleanupSerializedInputProps = async ({
const time = Date.now();
await lambdaDeleteFile({
- bucketName,
+ bucketName: serialized.bucketName,
key: inputPropsKey(serialized.hash),
region,
customCredentials: null,
@@ -30,11 +28,9 @@ export const cleanupSerializedInputProps = async ({
export const cleanupSerializedResolvedProps = async ({
serialized,
region,
- bucketName,
}: {
serialized: SerializedInputProps;
region: AwsRegion;
- bucketName: string;
}): Promise<number> => {
if (serialized.type === 'payload') {
return 0;
@@ -42,7 +38,7 @@ export const cleanupSerializedResolvedProps = async ({
const time = Date.now();
await lambdaDeleteFile({
- bucketName,
+ bucketName: serialized.bucketName,
key: resolvedPropsKey(serialized.hash),
region,
customCredentials: null,
diff --git a/packages/lambda/src/shared/compress-props.ts b/packages/lambda/src/shared/compress-props.ts
index 5f3ea2c85f..b29c2130e3 100644
--- a/packages/lambda/src/shared/compress-props.ts
+++ b/packages/lambda/src/shared/compress-props.ts
@@ -100,6 +100,7 @@ export const compressInputProps = async ({
return {
type: 'bucket-url',
hash,
+ bucketName,
};
}
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 6f4cb7d707..108805e62d 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -106,10 +106,6 @@ export const customOutName = (
};
};
-export const defaultPropsKey = (hash: string) => {
- return `default-props/${hash}.json`;
-};
-
export const inputPropsKey = (hash: string) => {
return `input-props/${hash}.json`;
};
@@ -153,6 +149,7 @@ export type SerializedInputProps =
| {
type: 'bucket-url';
hash: string;
+ bucketName: string;
}
| {
type: 'payload';