Skip to content

🎥 Make videos programmatically with React

retries test

Jonny Burgerceedcb8d1fce
diff --git a/packages/lambda/remotionlambda.zip b/packages/lambda/remotionlambda.zip
index 3af52fc2ab..e5cc5b6ae5 100644
Binary files a/packages/lambda/remotionlambda.zip and b/packages/lambda/remotionlambda.zip differ
diff --git a/packages/lambda/src/cli/commands/render/progress.ts b/packages/lambda/src/cli/commands/render/progress.ts
index 8444b12117..20785cf4f3 100644
--- a/packages/lambda/src/cli/commands/render/progress.ts
+++ b/packages/lambda/src/cli/commands/render/progress.ts
@@ -1,6 +1,7 @@
import {CliInternals} from '@remotion/cli';
import {Internals} from 'remotion';
import {CleanupInfo, EncodingProgress, RenderProgress} from '../../../defaults';
+import {ChunkRetry} from '../../../functions/helpers/get-retry-stats';
import {EnhancedErrorInfo} from '../../../functions/helpers/write-lambda-error';
type LambdaInvokeProgress = {
@@ -24,7 +25,8 @@ export type MultiRenderProgress = {
const makeInvokeProgress = (
invokeProgress: LambdaInvokeProgress,
- totalSteps: number
+ totalSteps: number,
+ retriesInfo: ChunkRetry[]
) => {
const {lambdasInvoked, totalLambdas, doneIn} = invokeProgress;
const progress = doneIn
@@ -40,6 +42,7 @@ const makeInvokeProgress = (
doneIn === null
? `${Math.round(progress * 100)}%`
: CliInternals.chalk.gray(`${doneIn}ms`),
+ retriesInfo.length > 0 ? `(+${retriesInfo.length} retries)` : [],
].join(' ');
};
@@ -162,22 +165,24 @@ const makeErrors = (errors: EnhancedErrorInfo[]) => {
return null;
}
- return errors.map((err) => {
- const shortStack = `${err.stack.substr(0, 60)}...`;
- if (err.willRetry) {
- if (err.chunk === null) {
- return `Error while preparing render: ${shortStack}... will retry.`;
- }
+ return errors
+ .map((err) => {
+ const shortStack = `${err.stack.substr(0, 90)}...`;
+ if (err.willRetry) {
+ if (err.chunk === null) {
+ return `Error while preparing render (will retry): ${shortStack}`;
+ }
- return `Error in chunk ${err.chunk}: ${shortStack}... will retry.`;
- }
+ return `Error in chunk (will retry) ${err.chunk}: ${shortStack}`;
+ }
- if (err.chunk === null) {
- return `Error during preparation: ${shortStack}.`;
- }
+ if (err.chunk === null) {
+ return `Error during preparation: ${shortStack}.`;
+ }
- return `Error in chunk ${err.chunk}: ${shortStack}`;
- });
+ return `Error in chunk ${err.chunk}: ${shortStack}`;
+ })
+ .join('\n');
};
export const makeProgressString = ({
@@ -186,15 +191,17 @@ export const makeProgressString = ({
steps,
isDownloaded,
errors,
+ retriesInfo,
}: {
outName: string | null;
progress: MultiRenderProgress;
steps: number;
isDownloaded: boolean;
errors: EnhancedErrorInfo[];
+ retriesInfo: ChunkRetry[];
}) => {
return [
- makeInvokeProgress(progress.lambdaInvokeProgress, steps),
+ makeInvokeProgress(progress.lambdaInvokeProgress, steps, retriesInfo),
makeChunkProgress({
chunkProgress: progress.chunkProgress,
invokeProgress: progress.lambdaInvokeProgress,
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index 9902063039..e016d56501 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -94,6 +94,7 @@ export const renderCommand = async (args: string[]) => {
errors: status.errors,
steps: totalSteps,
isDownloaded: false,
+ retriesInfo: status.retriesInfo,
})
);
@@ -113,7 +114,8 @@ export const renderCommand = async (args: string[]) => {
progress: newProgress,
steps: totalSteps,
isDownloaded: false,
- errors: status.errors,
+ errors: newStatus.errors,
+ retriesInfo: newStatus.retriesInfo,
})
);
@@ -125,7 +127,8 @@ export const renderCommand = async (args: string[]) => {
progress: newProgress,
steps: totalSteps,
isDownloaded: false,
- errors: status.errors,
+ errors: newStatus.errors,
+ retriesInfo: newStatus.retriesInfo,
})
);
if (outName) {
@@ -141,7 +144,8 @@ export const renderCommand = async (args: string[]) => {
progress: newProgress,
steps: totalSteps,
isDownloaded: true,
- errors: status.errors,
+ errors: newStatus.errors,
+ retriesInfo: newStatus.retriesInfo,
})
);
Log.info();
diff --git a/packages/lambda/src/functions/helpers/create-post-render-data.ts b/packages/lambda/src/functions/helpers/create-post-render-data.ts
index f468dd19f9..10a75db255 100644
--- a/packages/lambda/src/functions/helpers/create-post-render-data.ts
+++ b/packages/lambda/src/functions/helpers/create-post-render-data.ts
@@ -11,6 +11,7 @@ import {calculateChunkTimes} from './calculate-chunk-times';
import {findOutputFileInBucket} from './find-output-file-in-bucket';
import {getFilesToDelete} from './get-files-to-delete';
import {getLambdasInvokedStats} from './get-lambdas-invoked-stats';
+import {getRetryStats} from './get-retry-stats';
import {EnhancedErrorInfo} from './write-lambda-error';
const OVERHEAD_TIME_PER_LAMBDA = 100;
@@ -79,6 +80,7 @@ export const createPostRenderData = ({
renderMetadata?.estimatedRenderLambdaInvokations ?? null,
renderMetadata.startedDate
);
+ const retriesInfo = getRetryStats({contents, renderId});
if (timeToInvokeLambdas === null) {
throw new Error('should have timing for all lambdas');
@@ -115,6 +117,7 @@ export const createPostRenderData = ({
type: 'absolute-time',
}),
timeToInvokeLambdas,
+ retriesInfo,
};
return data;
diff --git a/packages/lambda/src/functions/helpers/get-files-to-delete.ts b/packages/lambda/src/functions/helpers/get-files-to-delete.ts
index e076744643..d6113e8fd6 100644
--- a/packages/lambda/src/functions/helpers/get-files-to-delete.ts
+++ b/packages/lambda/src/functions/helpers/get-files-to-delete.ts
@@ -1,7 +1,7 @@
import {
chunkKeyForIndex,
encodingProgressKey,
- lambdaInitializedKey,
+ lambdaInitializedPrefix,
lambdaTimingsPrefixForChunk,
} from '../../shared/constants';
@@ -17,12 +17,6 @@ export const getFilesToDelete = ({
chunkCount: number;
renderId: string;
}): CleanupJob[] => {
- const initialized = new Array(chunkCount).fill(true).map((x, i) =>
- lambdaInitializedKey({
- chunk: i,
- renderId,
- })
- );
const chunks = new Array(chunkCount).fill(true).map((x, i) =>
chunkKeyForIndex({
index: i,
@@ -33,12 +27,10 @@ export const getFilesToDelete = ({
.fill(true)
.map((x, i) => lambdaTimingsPrefixForChunk(renderId, i));
return [
- ...initialized.map((i) => {
- return {
- name: i,
- type: 'exact' as const,
- };
- }),
+ {
+ name: lambdaInitializedPrefix(renderId),
+ type: 'prefix' as const,
+ },
...chunks.map((i) => {
return {
name: i,
diff --git a/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.ts b/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.ts
index c944d8ad7d..47a7a02909 100644
--- a/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.ts
+++ b/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.ts
@@ -1,6 +1,7 @@
import {_Object} from '@aws-sdk/client-s3';
import {RenderInternals} from '@remotion/renderer';
import {lambdaInitializedPrefix} from '../../shared/constants';
+import {parseLambdaInitializedKey} from '../../shared/parse-lambda-initialized-key';
export type LambdaInvokeStats = {
timeToInvokeLambdas: number | null;
@@ -14,9 +15,9 @@ export const getLambdasInvokedStats = (
estimatedRenderLambdaInvokations: number | null,
startDate: number | null
): LambdaInvokeStats => {
- const lambdasInvoked = contents.filter((c) =>
- c.Key?.startsWith(lambdaInitializedPrefix(renderId))
- );
+ const lambdasInvoked = contents
+ .filter((c) => c.Key?.startsWith(lambdaInitializedPrefix(renderId)))
+ .filter((c) => parseLambdaInitializedKey(c.Key as string).attempt === 1);
const allLambdasInvoked =
lambdasInvoked.length === estimatedRenderLambdaInvokations;
diff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.ts
index 48ea22ae53..6f76b6c5b4 100644
--- a/packages/lambda/src/functions/helpers/get-progress.ts
+++ b/packages/lambda/src/functions/helpers/get-progress.ts
@@ -18,6 +18,7 @@ import {getLambdasInvokedStats} from './get-lambdas-invoked-stats';
import {getOverallProgress} from './get-overall-progress';
import {getPostRenderData} from './get-post-render-data';
import {getRenderMetadata} from './get-render-metadata';
+import {getRetryStats} from './get-retry-stats';
import {getTimeToFinish} from './get-time-to-finish';
import {inspectErrors} from './inspect-errors';
import {lambdaLs} from './io';
@@ -77,6 +78,7 @@ export const getProgress = async ({
timeToFinishChunks: postRenderData.timeToRenderChunks,
timeToInvokeLambdas: postRenderData.timeToInvokeLambdas,
overallProgress: 1,
+ retriesInfo: postRenderData.retriesInfo,
};
}
@@ -161,6 +163,11 @@ export const getProgress = async ({
renderMetadata?.startedDate ?? null
);
+ const retriesInfo = getRetryStats({
+ contents,
+ renderId,
+ });
+
const finalEncodingStatus = getFinalEncodingStatus({
encodingStatus,
outputFileExists: Boolean(outputFile),
@@ -211,5 +218,6 @@ export const getProgress = async ({
: 0,
rendering: renderMetadata ? chunkCount / renderMetadata.totalChunks : 0,
}),
+ retriesInfo,
};
};
diff --git a/packages/lambda/src/functions/helpers/get-retry-stats.ts b/packages/lambda/src/functions/helpers/get-retry-stats.ts
new file mode 100644
index 0000000000..995fbade51
--- /dev/null
+++ b/packages/lambda/src/functions/helpers/get-retry-stats.ts
@@ -0,0 +1,30 @@
+import {_Object} from '@aws-sdk/client-s3';
+import {lambdaInitializedPrefix} from '../../shared/constants';
+import {parseLambdaInitializedKey} from '../../shared/parse-lambda-initialized-key';
+
+export type ChunkRetry = {
+ chunk: number;
+ attempt: number;
+ time: number;
+};
+
+export const getRetryStats = ({
+ contents,
+ renderId,
+}: {
+ contents: _Object[];
+ renderId: string;
+}): ChunkRetry[] => {
+ const retries = contents
+ .filter((c) => c.Key?.startsWith(lambdaInitializedPrefix(renderId)))
+ .filter((c) => parseLambdaInitializedKey(c.Key as string).attempt !== 1);
+
+ return retries.map((retry) => {
+ const parsed = parseLambdaInitializedKey(retry.Key as string);
+ return {
+ chunk: parsed.chunk,
+ attempt: parsed.attempt,
+ time: retry.LastModified?.getTime() as number,
+ };
+ });
+};
diff --git a/packages/lambda/src/functions/helpers/write-lambda-error.ts b/packages/lambda/src/functions/helpers/write-lambda-error.ts
index 008450dac1..87d868a635 100644
--- a/packages/lambda/src/functions/helpers/write-lambda-error.ts
+++ b/packages/lambda/src/functions/helpers/write-lambda-error.ts
@@ -1,5 +1,4 @@
-import {getErrorKeyPrefix} from '../../shared/constants';
-import {randomHash} from '../../shared/random-hash';
+import {getErrorFileName} from '../../shared/constants';
import {getCurrentRegionInFunction} from './get-current-region';
import {FileNameAndSize, getFolderFiles} from './get-files-in-folder';
import {lambdaWriteFile} from './io';
@@ -53,7 +52,11 @@ export const writeLambdaError = async ({
}) => {
await lambdaWriteFile({
bucketName,
- key: `${getErrorKeyPrefix(renderId)}${randomHash()}.txt`,
+ key: `${getErrorFileName({
+ renderId,
+ chunk: errorInfo.chunk,
+ attempt: errorInfo.attempt,
+ })}.txt`,
body: JSON.stringify(errorInfo),
region: getCurrentRegionInFunction(),
privacy: 'private',
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 706ebec6a7..de5f80aa30 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -106,6 +106,7 @@ const renderHandler = async (
key: lambdaInitializedKey({
renderId: params.renderId,
chunk: params.chunk,
+ attempt: params.attempt,
}),
region: getCurrentRegionInFunction(),
expectedBucketOwner: options.expectedBucketOwner,
@@ -232,20 +233,6 @@ export const rendererHandler = async (
'error while loading shared libraries: libnss3.so'
);
const willRetry = isBrowserError || params.retriesLeft > 0;
- if (willRetry) {
- const retryPayload: LambdaPayloads[LambdaRoutines.renderer] = {
- ...params,
- retriesLeft: params.retriesLeft - 1,
- };
- await getLambdaClient(getCurrentRegionInFunction()).send(
- new InvokeCommand({
- FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
- // @ts-expect-error
- Payload: JSON.stringify(retryPayload),
- InvocationType: 'Event',
- })
- );
- }
console.log('Error occurred');
console.log(err);
@@ -265,8 +252,22 @@ export const rendererHandler = async (
renderId: params.renderId,
expectedBucketOwner: options.expectedBucketOwner,
});
+ if (willRetry) {
+ const retryPayload: LambdaPayloads[LambdaRoutines.renderer] = {
+ ...params,
+ retriesLeft: params.retriesLeft - 1,
+ attempt: params.attempt + 1,
+ };
+ await getLambdaClient(getCurrentRegionInFunction()).send(
+ new InvokeCommand({
+ FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
+ // @ts-expect-error
+ Payload: JSON.stringify(retryPayload),
+ InvocationType: 'Event',
+ })
+ );
+ }
} finally {
- await closeBrowser();
if (params.saveBrowserLogs) {
await uploadBrowserLogs({
chunk: params.chunk,
@@ -278,5 +279,7 @@ export const rendererHandler = async (
renderId: params.renderId,
});
}
+
+ await closeBrowser();
}
};
diff --git a/packages/lambda/src/functions/still.ts b/packages/lambda/src/functions/still.ts
index 2a7502da00..30d7f9ac7a 100644
--- a/packages/lambda/src/functions/still.ts
+++ b/packages/lambda/src/functions/still.ts
@@ -167,6 +167,7 @@ export const stillHandler = async (
const retryPayload: LambdaPayloads[LambdaRoutines.still] = {
...params,
maxRetries: params.maxRetries - 1,
+ attempt: params.attempt + 1,
};
// TODO: Test retries by failing sometimes
const res = await getLambdaClient(getCurrentRegionInFunction()).send(
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index fd33642a78..7bc0f6b180 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -5,6 +5,7 @@ import {
ProResProfile,
VideoConfig,
} from 'remotion';
+import {ChunkRetry} from '../functions/helpers/get-retry-stats';
import {EnhancedErrorInfo} from '../functions/helpers/write-lambda-error';
import {AwsRegion} from '../pricing/aws-regions';
import {getFileExtensionFromCodec} from './get-file-extension-from-codec';
@@ -37,10 +38,13 @@ export const lambdaInitializedPrefix = (renderId: string) =>
export const lambdaInitializedKey = ({
renderId,
chunk,
+ attempt,
}: {
+ attempt: number;
renderId: string;
chunk: number;
-}) => `${lambdaInitializedPrefix(renderId)}-${chunk}.txt`;
+}) =>
+ `${lambdaInitializedPrefix(renderId)}-chunk:${chunk}-attempt:${attempt}.txt`;
export const lambdaTimingsPrefix = (renderId: string) =>
`${rendersPrefix(renderId)}/lambda-timings/chunk:`;
@@ -87,6 +91,17 @@ export const chunkKeyForIndex = ({
export const getErrorKeyPrefix = (renderId: string) =>
`${rendersPrefix(renderId)}/errors/`;
+
+export const getErrorFileName = ({
+ renderId,
+ chunk,
+ attempt,
+}: {
+ renderId: string;
+ chunk: number | null;
+ attempt: number;
+}) => getErrorKeyPrefix(renderId) + ':chunk-' + chunk + ':attempt-' + attempt;
+
export const optimizationProfile = (siteId: string, compositionId: string) =>
`optimization-profiles/${siteId}/${compositionId}/optimization-profile`;
export const getSitesKey = (siteId: string) => `sites/${siteId}`;
@@ -285,6 +300,7 @@ export type PostRenderData = {
timeToCleanUp: number;
timeToRenderChunks: number;
timeToInvokeLambdas: number;
+ retriesInfo: ChunkRetry[];
};
export type CostsInfo = {
@@ -319,6 +335,7 @@ export type RenderProgress = {
timeToFinishChunks: number | null;
timeToInvokeLambdas: number | null;
overallProgress: number;
+ retriesInfo: ChunkRetry[];
};
export type Privacy = 'public' | 'private';
diff --git a/packages/lambda/src/shared/parse-lambda-initialized-key.ts b/packages/lambda/src/shared/parse-lambda-initialized-key.ts
new file mode 100644
index 0000000000..9da7f8a7e4
--- /dev/null
+++ b/packages/lambda/src/shared/parse-lambda-initialized-key.ts
@@ -0,0 +1,16 @@
+export const parseLambdaInitializedKey = (key: string) => {
+ const match = key.match(
+ /^renders\/(.*)\/lambda-initialized-chunk:([0-9]+)-attempt:([0-9]+).txt$/
+ );
+ if (!match) {
+ throw new Error(
+ `Cannot parse filename ${key} into timing information. Malformed data.`
+ );
+ }
+
+ return {
+ renderId: match[1],
+ chunk: Number(match[2]),
+ attempt: Number(match[3]),
+ };
+};
diff --git a/packages/lambda/src/test/lambda-timings-key.test.ts b/packages/lambda/src/test/lambda-timings-key.test.ts
new file mode 100644
index 0000000000..61271f5155
--- /dev/null
+++ b/packages/lambda/src/test/lambda-timings-key.test.ts
@@ -0,0 +1,15 @@
+import {lambdaInitializedKey} from '../shared/constants';
+import {parseLambdaInitializedKey} from '../shared/parse-lambda-initialized-key';
+
+test('Lambda timinings key', () => {
+ const key = lambdaInitializedKey({
+ attempt: 1,
+ chunk: 1,
+ renderId: 'abcdef',
+ });
+ expect(parseLambdaInitializedKey(key)).toEqual({
+ attempt: 1,
+ chunk: 1,
+ renderId: 'abcdef',
+ });
+});