Skip to content

🎥 Make videos programmatically with React

handling of low resources

Jonny Burger38377a56df6a
diff --git a/packages/lambda/src/functions/helpers/get-browser-instance.ts b/packages/lambda/src/functions/helpers/get-browser-instance.ts
index 164cc0142f..1e0cb38570 100644
--- a/packages/lambda/src/functions/helpers/get-browser-instance.ts
+++ b/packages/lambda/src/functions/helpers/get-browser-instance.ts
@@ -58,3 +58,12 @@ export const closeBrowser = async () => {
await Promise.all(pages.map((p) => p.close()));
}
};
+
+export const quitBrowser = async () => {
+ if (!_browserInstance) {
+ return;
+ }
+
+ await _browserInstance.close();
+ _browserInstance = null;
+};
diff --git a/packages/lambda/src/functions/helpers/is-enosp-err.ts b/packages/lambda/src/functions/helpers/is-enosp-err.ts
index 336ca95c15..8d7b810775 100644
--- a/packages/lambda/src/functions/helpers/is-enosp-err.ts
+++ b/packages/lambda/src/functions/helpers/is-enosp-err.ts
@@ -1,3 +1,10 @@
export const errorIsOutOfSpaceError = (err: string) => {
- return err.includes('ENOSPC') || err.includes('No space left on device');
+ return (
+ err.includes('ENOSPC') ||
+ err.toLowerCase().includes('no space left on device')
+ );
+};
+
+export const isErrInsufficientResourcesErr = (err: string) => {
+ return err.includes('net::ERR_INSUFFICIENT_RESOURCES');
};
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 2a36fa298f..af700a3833 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -1,5 +1,6 @@
import {InvokeCommand} from '@aws-sdk/client-lambda';
import fs from 'fs';
+import pRetry from 'p-retry';
import {Internals} from 'remotion';
import {getLambdaClient} from '../shared/aws-clients';
import {chunk} from '../shared/chunk';
@@ -24,10 +25,16 @@ import {
writeOptimization,
} from './chunk-optimization/s3-optimization-file';
import {writeTimingProfile} from './chunk-optimization/write-profile';
+import {deleteTmpDir} from './helpers/clean-tmpdir';
import {concatVideosS3} from './helpers/concat-videos';
-import {closeBrowser, getBrowserInstance} from './helpers/get-browser-instance';
+import {
+ closeBrowser,
+ getBrowserInstance,
+ quitBrowser,
+} from './helpers/get-browser-instance';
import {getCurrentRegion} from './helpers/get-current-region';
import {lambdaWriteFile} from './helpers/io';
+import {isErrInsufficientResourcesErr} from './helpers/is-enosp-err';
import {timer} from './helpers/timer';
import {validateComposition} from './helpers/validate-composition';
import {
@@ -49,7 +56,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
throw new Error('Pass chunkSize');
}
- const [browserInstance, optimization] = await Promise.all([
+ const [, optimization] = await Promise.all([
getBrowserInstance(),
getOptimization({
bucketName: params.bucketName,
@@ -60,27 +67,52 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
}),
]);
- const comp = await validateComposition({
- serveUrl: params.serveUrl,
- composition: params.composition,
- browserInstance,
- inputProps: params.inputProps,
- onError: ({err}) => {
- writeLambdaError({
- bucketName: params.bucketName,
- errorInfo: {
- chunk: null,
- frame: null,
- isFatal: false,
- stack: err.stack as string,
- type: 'browser',
- tmpDir: getTmpDirStateIfENoSp(err.stack as string),
+ const comp = await pRetry(
+ async () =>
+ validateComposition({
+ serveUrl: params.serveUrl,
+ composition: params.composition,
+ browserInstance: await getBrowserInstance(),
+ inputProps: params.inputProps,
+ onError: ({err}) => {
+ writeLambdaError({
+ bucketName: params.bucketName,
+ errorInfo: {
+ chunk: null,
+ frame: null,
+ isFatal: false,
+ stack: err.stack as string,
+ type: 'browser',
+ tmpDir: getTmpDirStateIfENoSp(err.stack as string),
+ },
+ expectedBucketOwner: options.expectedBucketOwner,
+ renderId: params.renderId,
+ });
},
- expectedBucketOwner: options.expectedBucketOwner,
- renderId: params.renderId,
- });
- },
- });
+ }),
+ {
+ retries: 1,
+ onFailedAttempt: async (err) => {
+ if (isErrInsufficientResourcesErr(err.message)) {
+ await writeLambdaError({
+ bucketName: params.bucketName,
+ errorInfo: {
+ chunk: null,
+ frame: null,
+ isFatal: false,
+ stack: err.stack as string,
+ tmpDir: null,
+ type: 'stitcher',
+ },
+ expectedBucketOwner: options.expectedBucketOwner,
+ renderId: params.renderId,
+ });
+ await quitBrowser();
+ deleteTmpDir();
+ }
+ },
+ }
+ );
Internals.validateDurationInFrames(
comp.durationInFrames,
'passed to <Component />'