Skip to content

🎥 Make videos programmatically with React

handle timeouts

Jonny Burger49cf43f11105
diff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.ts
index 162b54b423..45a58af9cc 100644
--- a/packages/lambda/src/functions/helpers/get-progress.ts
+++ b/packages/lambda/src/functions/helpers/get-progress.ts
@@ -1,3 +1,4 @@
+import {Internals} from 'remotion';
import {AwsRegion} from '../..';
import {
chunkKey,
@@ -22,6 +23,7 @@ import {getRetryStats} from './get-retry-stats';
import {getTimeToFinish} from './get-time-to-finish';
import {inspectErrors} from './inspect-errors';
import {lambdaLs} from './io';
+import {EnhancedErrorInfo} from './write-lambda-error';
export const getProgress = async ({
bucketName,
@@ -29,12 +31,14 @@ export const getProgress = async ({
expectedBucketOwner,
region,
memorySizeInMb,
+ timeoutInMiliseconds,
}: {
bucketName: string;
renderId: string;
expectedBucketOwner: string;
region: AwsRegion;
memorySizeInMb: number;
+ timeoutInMiliseconds: number;
}): Promise<RenderProgress> => {
const postRenderData = await getPostRenderData({
bucketName,
@@ -177,6 +181,30 @@ export const getProgress = async ({
const chunkCount = outputFile
? renderMetadata?.totalChunks ?? 0
: chunks.length;
+
+ // We add a 20 second buffer for it, since AWS timeshifts can be quite a lot. Once it's 20sec over the limit, we consider it timed out
+ const isBeyondTimeout =
+ Date.now() > Date.now() + timeoutInMiliseconds + 20000;
+
+ const allErrors: EnhancedErrorInfo[] = [
+ isBeyondTimeout
+ ? ({
+ attempt: 1,
+ chunk: null,
+ explanation: `The main function timed out after ${timeoutInMiliseconds}ms. Consider increasing the timeout of your function.`,
+ frame: null,
+ isFatal: true,
+ s3Location: '',
+ stack: new Error().stack,
+ tmpDir: null,
+ totalAttempts: 1,
+ type: 'stitcher',
+ willRetry: false,
+ } as EnhancedErrorInfo)
+ : null,
+ ...errorExplanations,
+ ].filter(Internals.truthy);
+
return {
chunks: chunkCount,
done: false,
@@ -187,10 +215,8 @@ export const getProgress = async ({
bucket: bucketName,
outputFile: outputFile?.url ?? null,
timeToFinish,
- errors: errorExplanations,
- fatalErrorEncountered: errorExplanations.some(
- (f) => f.isFatal && !f.willRetry
- ),
+ errors: allErrors,
+ fatalErrorEncountered: allErrors.some((f) => f.isFatal && !f.willRetry),
currentTime: Date.now(),
renderSize,
lambdasInvoked: lambdasInvokedStats.lambdasInvoked,
diff --git a/packages/lambda/src/functions/index.ts b/packages/lambda/src/functions/index.ts
index 3fa63020a8..97f398af47 100644
--- a/packages/lambda/src/functions/index.ts
+++ b/packages/lambda/src/functions/index.ts
@@ -18,8 +18,10 @@ import {stillHandler} from './still';
export const handler = async <T extends LambdaRoutines>(
params: LambdaPayload,
- context: {invokedFunctionArn: string}
+ context: {invokedFunctionArn: string; getRemainingTimeInMillis: () => number}
): Promise<LambdaReturnValues[T]> => {
+ const timeoutInMiliseconds = context.getRemainingTimeInMillis();
+
if (!context || !context.invokedFunctionArn) {
throw new Error(
'Lambda function unexpectedly does not have context.invokedFunctionArn'
@@ -59,7 +61,10 @@ export const handler = async <T extends LambdaRoutines>(
printCloudwatchHelper(LambdaRoutines.status, {
renderId: params.renderId,
});
- return progressHandler(params, {expectedBucketOwner: currentUserId});
+ return progressHandler(params, {
+ expectedBucketOwner: currentUserId,
+ timeoutInMiliseconds,
+ });
}
if (params.type === LambdaRoutines.fire) {
diff --git a/packages/lambda/src/functions/progress.ts b/packages/lambda/src/functions/progress.ts
index 241c2d2f85..5c9bd2087f 100644
--- a/packages/lambda/src/functions/progress.ts
+++ b/packages/lambda/src/functions/progress.ts
@@ -8,6 +8,7 @@ import {getProgress} from './helpers/get-progress';
type Options = {
expectedBucketOwner: string;
+ timeoutInMiliseconds: number;
};
export const progressHandler = async (
@@ -24,5 +25,6 @@ export const progressHandler = async (
expectedBucketOwner: options.expectedBucketOwner,
region: getCurrentRegionInFunction(),
memorySizeInMb: Number(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE),
+ timeoutInMiliseconds: options.timeoutInMiliseconds,
});
};
diff --git a/packages/lambda/src/test/handlers.test.ts b/packages/lambda/src/test/handlers.test.ts
index ced5c914cc..f634c2b5d6 100644
--- a/packages/lambda/src/test/handlers.test.ts
+++ b/packages/lambda/src/test/handlers.test.ts
@@ -10,7 +10,7 @@ test('Info handler should return version', async () => {
{
type: LambdaRoutines.info,
},
- {invokedFunctionArn: '::::::'}
+ {invokedFunctionArn: '::::::', getRemainingTimeInMillis: () => 1000}
);
expect(
diff --git a/packages/lambda/src/test/integration/handlers.test.ts b/packages/lambda/src/test/integration/handlers.test.ts
index 36ce34756b..33627a16c3 100644
--- a/packages/lambda/src/test/integration/handlers.test.ts
+++ b/packages/lambda/src/test/integration/handlers.test.ts
@@ -1,5 +1,5 @@
import {CURRENT_VERSION, LambdaRoutines} from '../../defaults';
-import {handler} from '../../functions/index';
+import {handler} from '../../functions/_index';
test('Call function locally', async () => {
expect(
@@ -7,6 +7,7 @@ test('Call function locally', async () => {
{type: LambdaRoutines.info},
{
invokedFunctionArn: 'arn',
+ getRemainingTimeInMillis: () => 1000,
}
)
).toEqual({version: CURRENT_VERSION});