Skip to content

🎥 Make videos programmatically with React

stop main lambda function if error occurred and send webhook

Jonny Burger28452f21567c
diff --git a/packages/lambda/src/functions/helpers/concat-videos.ts b/packages/lambda/src/functions/helpers/concat-videos.ts
index c31c0c6491..1583021dc6 100644
--- a/packages/lambda/src/functions/helpers/concat-videos.ts
+++ b/packages/lambda/src/functions/helpers/concat-videos.ts
@@ -13,12 +13,16 @@ import type {AwsRegion} from '../../pricing/aws-regions';
import {
chunkKey,
CONCAT_FOLDER_TOKEN,
+ getErrorKeyPrefix,
REMOTION_CONCATED_TOKEN,
REMOTION_FILELIST_TOKEN,
+ rendersPrefix,
} from '../../shared/constants';
import type {LambdaCodec} from '../../shared/validate-lambda-codec';
+import {inspectErrors} from './inspect-errors';
import {lambdaLs, lambdaReadFile} from './io';
import {timer} from './timer';
+import type {EnhancedErrorInfo} from './write-lambda-error';
const getChunkDownloadOutputLocation = ({
outdir,
@@ -68,6 +72,7 @@ const getAllFilesS3 = ({
renderId,
region,
expectedBucketOwner,
+ onErrors,
}: {
bucket: string;
expectedFiles: number;
@@ -75,12 +80,13 @@ const getAllFilesS3 = ({
renderId: string;
region: AwsRegion;
expectedBucketOwner: string;
+ onErrors: (errors: EnhancedErrorInfo[]) => Promise<void>;
}): Promise<string[]> => {
const alreadyDownloading: {[key: string]: true} = {};
const downloaded: {[key: string]: true} = {};
const getFiles = async () => {
- const prefix = chunkKey(renderId);
+ const prefix = rendersPrefix(renderId);
const lsTimer = timer('Listing files');
const contents = await lambdaLs({
bucketName: bucket,
@@ -89,14 +95,19 @@ const getAllFilesS3 = ({
expectedBucketOwner,
});
lsTimer.end();
- return contents
- .filter((c) => c.Key?.startsWith(chunkKey(renderId)))
- .map((_) => _.Key as string);
+ return {
+ filesInBucket: contents
+ .filter((c) => c.Key?.startsWith(chunkKey(renderId)))
+ .map((_) => _.Key as string),
+ errorContents: contents.filter((c) =>
+ c.Key?.startsWith(getErrorKeyPrefix(renderId))
+ ),
+ };
};
return new Promise<string[]>((resolve, reject) => {
const loop = async () => {
- const filesInBucket = await getFiles();
+ const {filesInBucket, errorContents} = await getFiles();
const checkFinish = () => {
const areAllFilesDownloaded =
Object.keys(downloaded).length === expectedFiles;
@@ -116,6 +127,20 @@ const getAllFilesS3 = ({
};
console.log('Found ', filesInBucket);
+ const errors = (
+ await inspectErrors({
+ bucket,
+ contents: errorContents,
+ expectedBucketOwner,
+ region,
+ renderId,
+ })
+ ).filter((e) => e.isFatal);
+
+ if (errors.length > 0) {
+ await onErrors(errors);
+ // Will die here
+ }
filesInBucket.forEach(async (key) => {
if (alreadyDownloading[key]) {
@@ -145,12 +170,12 @@ const getAllFilesS3 = ({
Object.keys(alreadyDownloading).length === expectedFiles;
if (!areAllFilesDownloading) {
setTimeout(() => {
- loop();
+ loop().catch((err) => reject(err));
}, 100);
}
};
- loop();
+ loop().catch((err) => reject(err));
});
};
@@ -165,10 +190,12 @@ export const concatVideosS3 = async ({
expectedBucketOwner,
fps,
numberOfGifLoops,
+ onErrors,
}: {
bucket: string;
expectedFiles: number;
onProgress: (frames: number, encodingStart: number) => void;
+ onErrors: (errors: EnhancedErrorInfo[]) => Promise<void>;
numberOfFrames: number;
renderId: string;
region: AwsRegion;
@@ -192,6 +219,7 @@ export const concatVideosS3 = async ({
renderId,
region,
expectedBucketOwner,
+ onErrors,
});
const outfile = join(
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 8070ff258c..faf72267eb 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -49,6 +49,7 @@ import {inspectErrors} from './helpers/inspect-errors';
import {lambdaDeleteFile, lambdaLs, lambdaWriteFile} from './helpers/io';
import {timer} from './helpers/timer';
import {validateComposition} from './helpers/validate-composition';
+import type {EnhancedErrorInfo} from './helpers/write-lambda-error';
import {
getTmpDirStateIfENoSp,
writeLambdaError,
@@ -408,6 +409,35 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
});
};
+ const onErrors = async (errors: EnhancedErrorInfo[]) => {
+ console.log('Found Errors', errors);
+
+ if (params.webhook) {
+ console.log('Sending webhook with errors');
+ await invokeWebhook({
+ url: params.webhook.url,
+ secret: params.webhook.secret ?? null,
+ payload: {
+ type: 'error',
+ renderId: params.renderId,
+ expectedBucketOwner: options.expectedBucketOwner,
+ bucketName: params.bucketName,
+ errors: errors.slice(0, 5).map((e) => ({
+ message: e.message,
+ name: e.name as string,
+ stack: e.stack as string,
+ })),
+ },
+ });
+ } else {
+ console.log('No webhook specified');
+ }
+
+ throw new Error(
+ 'Stopping Lambda function because error occurred: ' + errors[0].stack
+ );
+ };
+
const fps = comp.fps / params.everyNthFrame;
const {outfile, cleanupChunksProm, encodingStart} = await concatVideosS3({
@@ -421,6 +451,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
expectedBucketOwner: options.expectedBucketOwner,
fps,
numberOfGifLoops: params.numberOfGifLoops,
+ onErrors,
});
if (!encodingStop) {
encodingStop = Date.now();