🎥 Make videos programmatically with React
Put `forgetLambdaInstance()` in a finally {} statement always
Jonny Burger61a99ba46217
diff --git a/packages/lambda/src/functions/compositions.ts b/packages/lambda/src/functions/compositions.tsindex edbc546325..3b88c8e94f 100644--- a/packages/lambda/src/functions/compositions.ts+++ b/packages/lambda/src/functions/compositions.ts@@ -5,7 +5,10 @@ import type {LambdaPayload} from '../defaults';import {LambdaRoutines} from '../defaults';import {decompressInputProps} from '../shared/compress-props';import {convertToServeUrl} from '../shared/convert-to-serve-url';-import {getBrowserInstance} from './helpers/get-browser-instance';+import {+ forgetBrowserEventLoop,+ getBrowserInstance,+} from './helpers/get-browser-instance';import {getCurrentRegionInFunction} from './helpers/get-current-region';type Options = {@@ -32,56 +35,59 @@ export const compositionsHandler = async ();}- const region = getCurrentRegionInFunction();+ try {+ const region = getCurrentRegionInFunction();- const browserInstancePromise = getBrowserInstance(- lambdaParams.logLevel,- false,- lambdaParams.chromiumOptions,- );- const bucketNamePromise = lambdaParams.bucketName- ? Promise.resolve(lambdaParams.bucketName)- : internalGetOrCreateBucket({- region,- enableFolderExpiry: null,- customCredentials: null,- }).then((b) => b.bucketName);-- const bucketName = await bucketNamePromise;- const serializedInputPropsWithCustomSchema = await decompressInputProps({- bucketName: await bucketNamePromise,- expectedBucketOwner: options.expectedBucketOwner,- region: getCurrentRegionInFunction(),- serialized: lambdaParams.inputProps,- propsType: 'input-props',- });+ const browserInstancePromise = getBrowserInstance(+ lambdaParams.logLevel,+ false,+ lambdaParams.chromiumOptions,+ );+ const bucketNamePromise = lambdaParams.bucketName+ ? Promise.resolve(lambdaParams.bucketName)+ : internalGetOrCreateBucket({+ region,+ enableFolderExpiry: null,+ customCredentials: null,+ }).then((b) => b.bucketName);- const realServeUrl = convertToServeUrl({- urlOrId: lambdaParams.serveUrl,- region,- bucketName,- });+ const bucketName = await bucketNamePromise;+ const serializedInputPropsWithCustomSchema = await decompressInputProps({+ bucketName: await bucketNamePromise,+ expectedBucketOwner: options.expectedBucketOwner,+ region: getCurrentRegionInFunction(),+ serialized: lambdaParams.inputProps,+ propsType: 'input-props',+ });- const compositions = await RenderInternals.internalGetCompositions({- serveUrlOrWebpackUrl: realServeUrl,- puppeteerInstance: (await browserInstancePromise).instance,- serializedInputPropsWithCustomSchema,- envVariables: lambdaParams.envVariables ?? {},- timeoutInMilliseconds: lambdaParams.timeoutInMilliseconds,- chromiumOptions: lambdaParams.chromiumOptions,- port: null,- server: undefined,- logLevel: lambdaParams.logLevel,- indent: false,- browserExecutable: null,- onBrowserLog: null,- offthreadVideoCacheSizeInBytes: lambdaParams.offthreadVideoCacheSizeInBytes,- });+ const realServeUrl = convertToServeUrl({+ urlOrId: lambdaParams.serveUrl,+ region,+ bucketName,+ });- (await browserInstancePromise).instance.forgetEventLoop();+ const compositions = await RenderInternals.internalGetCompositions({+ serveUrlOrWebpackUrl: realServeUrl,+ puppeteerInstance: (await browserInstancePromise).instance,+ serializedInputPropsWithCustomSchema,+ envVariables: lambdaParams.envVariables ?? {},+ timeoutInMilliseconds: lambdaParams.timeoutInMilliseconds,+ chromiumOptions: lambdaParams.chromiumOptions,+ port: null,+ server: undefined,+ logLevel: lambdaParams.logLevel,+ indent: false,+ browserExecutable: null,+ onBrowserLog: null,+ offthreadVideoCacheSizeInBytes:+ lambdaParams.offthreadVideoCacheSizeInBytes,+ });- return Promise.resolve({- compositions,- type: 'success' as const,- });+ return Promise.resolve({+ compositions,+ type: 'success' as const,+ });+ } finally {+ forgetBrowserEventLoop(lambdaParams.logLevel);+ }};diff --git a/packages/lambda/src/functions/helpers/get-browser-instance.ts b/packages/lambda/src/functions/helpers/get-browser-instance.tsindex 6a24616f43..5e1b7b4387 100644--- a/packages/lambda/src/functions/helpers/get-browser-instance.ts+++ b/packages/lambda/src/functions/helpers/get-browser-instance.ts@@ -43,6 +43,14 @@ const waitForLaunched = () => {});};+export const forgetBrowserEventLoop = (logLevel: LogLevel) => {+ RenderInternals.Log.verbose(+ {indent: false, logLevel},+ 'Keeping browser open for next invocation',+ );+ _browserInstance?.instance.forgetEventLoop();+};+export const getBrowserInstance = async (logLevel: LogLevel,indent: boolean,diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.tsindex 941458c152..bcf7ad07ee 100644--- a/packages/lambda/src/functions/launch.ts+++ b/packages/lambda/src/functions/launch.ts@@ -1,3 +1,4 @@+const log = require('why-is-node-running');/* eslint-disable @typescript-eslint/no-use-before-define */import {InvokeCommand} from '@aws-sdk/client-lambda';import type {LogOptions} from '@remotion/renderer';@@ -37,7 +38,10 @@ import {planFrameRanges} from './chunk-optimization/plan-frame-ranges';import {bestFramesPerLambdaParam} from './helpers/best-frames-per-lambda-param';import {getExpectedOutName} from './helpers/expected-out-name';import {findOutputFileInBucket} from './helpers/find-output-file-in-bucket';-import {getBrowserInstance} from './helpers/get-browser-instance';+import {+ forgetBrowserEventLoop,+ getBrowserInstance,+} from './helpers/get-browser-instance';import {getCurrentRegionInFunction} from './helpers/get-current-region';import {lambdaDeleteFile, lambdaWriteFile} from './helpers/io';import type {OnAllChunksAvailable} from './helpers/merge-chunks';@@ -364,7 +368,6 @@ const innerLaunchHandler = async ({);reqSend.end();- (await browserInstance).instance.forgetEventLoop();const fps = comp.fps / params.everyNthFrame;const postRenderData = await mergeChunksAndFinishRender({@@ -648,6 +651,7 @@ export const launchHandler = async (});RenderInternals.Log.error('Wrote error to S3');clearTimeout(webhookDueToTimeout);+if (params.webhook && !webhookInvoked) {try {await invokeWebhook({@@ -696,5 +700,7 @@ export const launchHandler = async (}throw err;+ } finally {+ forgetBrowserEventLoop(params.logLevel);}};diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.tsindex 4efdefbe9b..658f212f83 100644--- a/packages/lambda/src/functions/renderer.ts+++ b/packages/lambda/src/functions/renderer.ts@@ -18,7 +18,10 @@ import type {ChunkTimingData,ObjectChunkTimingData,} from './chunk-optimization/types';-import {getBrowserInstance} from './helpers/get-browser-instance';+import {+ forgetBrowserEventLoop,+ getBrowserInstance,+} from './helpers/get-browser-instance';import {executablePath} from './helpers/get-chromium-executable-path';import {getCurrentRegionInFunction} from './helpers/get-current-region';import {lambdaWriteFile} from './helpers/io';@@ -282,7 +285,6 @@ const renderHandler = async (customCredentials: null,}),]);- browserInstance.instance.forgetEventLoop();RenderInternals.Log.verbose({indent: false, logLevel: params.logLevel},'Done!',@@ -363,5 +365,7 @@ export const rendererHandler = async (}throw err;+ } finally {+ forgetBrowserEventLoop(params.logLevel);}};diff --git a/packages/lambda/src/functions/start.ts b/packages/lambda/src/functions/start.tsindex 32be912426..443bdf55b1 100644--- a/packages/lambda/src/functions/start.ts+++ b/packages/lambda/src/functions/start.ts@@ -81,7 +81,7 @@ export const startHandler = async (params: LambdaPayload, options: Options) => {jpegQuality: params.jpegQuality,maxRetries: params.maxRetries,privacy: params.privacy,- logLevel: params.logLevel ?? 'info',+ logLevel: params.logLevel,frameRange: params.frameRange,outName: params.outName,timeoutInMilliseconds: params.timeoutInMilliseconds,diff --git a/packages/lambda/src/functions/still.ts b/packages/lambda/src/functions/still.tsindex 127763e306..b74255d9b7 100644--- a/packages/lambda/src/functions/still.ts+++ b/packages/lambda/src/functions/still.ts@@ -30,7 +30,10 @@ import {getExpectedOutName,} from './helpers/expected-out-name';import {formatCostsInfo} from './helpers/format-costs-info';-import {getBrowserInstance} from './helpers/get-browser-instance';+import {+ forgetBrowserEventLoop,+ getBrowserInstance,+} from './helpers/get-browser-instance';import {executablePath} from './helpers/get-chromium-executable-path';import {getCurrentRegionInFunction} from './helpers/get-current-region';import {getOutputUrlFromMetadata} from './helpers/get-output-url-from-metadata';@@ -246,8 +249,6 @@ const innerStillHandler = async ({diskSizeInMb: MAX_EPHEMERAL_STORAGE_IN_MB,});- browserInstance.instance.forgetEventLoop();-return {type: 'success' as const,output: getOutputUrlFromMetadata(@@ -339,5 +340,11 @@ export const stillHandler = async (});return res;+ } finally {+ forgetBrowserEventLoop(+ options.params.type === LambdaRoutines.still+ ? options.params.logLevel+ : 'error',+ );}};