Skip to content

πŸŽ₯ Make videos programmatically with React

modified render functions to handle errors

Matt McGillivray8dcff4107412
diff --git a/packages/cloudrun/src/api/deploy-service.ts b/packages/cloudrun/src/api/deploy-service.ts
index 7006e620f9..9a48e84f15 100644
--- a/packages/cloudrun/src/api/deploy-service.ts
+++ b/packages/cloudrun/src/api/deploy-service.ts
@@ -8,8 +8,8 @@ import {getCloudRunClient} from './helpers/get-cloud-run-client';
export type DeployServiceInput = {
remotionVersion: string;
- memory: string;
- cpu: string;
+ memory?: string;
+ cpu?: string;
projectID: string;
region: string;
redeploy: boolean;
@@ -33,7 +33,6 @@ export type DeployServiceOutput = {
export const deployService = async (
options: DeployServiceInput
): Promise<DeployServiceOutput> => {
- // ToDo: This needs to allow for defaults for use of the node API without CLI
validateGcpRegion(options.region);
validateProjectID(options.projectID);
validateRemotionVersion(options.remotionVersion);
diff --git a/packages/cloudrun/src/api/helpers/parse-cloud-run-url.ts b/packages/cloudrun/src/api/helpers/parse-cloud-run-url.ts
deleted file mode 100644
index 4c63a67b8c..0000000000
--- a/packages/cloudrun/src/api/helpers/parse-cloud-run-url.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import type {GcpRegion} from '../../client';
-import {GCP_REGION_SHORTHAND} from '../../pricing/gcp-regions';
-
-type CloudRunInfo = {
- region: GcpRegion;
- projectHash: string;
- serviceName: string;
-};
-
-export const parseCloudRunUrl = (url: string): CloudRunInfo => {
- const match = url.match(/https:\/\/(.*)-(.*)-(.*).run.app/);
- if (!match) {
- throw new Error(`Could not parse Cloud Run URL: ${url}`);
- }
-
- const shorthand = Object.keys(GCP_REGION_SHORTHAND).find(
- (key) => GCP_REGION_SHORTHAND[key as GcpRegion] === match[3]
- );
-
- if (!shorthand) {
- throw new Error('Could not find region shorthand for URL: ' + url);
- }
-
- return {
- serviceName: match[1],
- projectHash: match[2],
- region: shorthand as GcpRegion,
- };
-};
diff --git a/packages/cloudrun/src/api/render-media-on-cloudrun.ts b/packages/cloudrun/src/api/render-media-on-cloudrun.ts
index c99a082c57..2513eea537 100644
--- a/packages/cloudrun/src/api/render-media-on-cloudrun.ts
+++ b/packages/cloudrun/src/api/render-media-on-cloudrun.ts
@@ -4,7 +4,6 @@ import type {CloudrunCodec} from '../shared/validate-gcp-codec';
import {validateCloudrunCodec} from '../shared/validate-gcp-codec';
import {validateServeUrl} from '../shared/validate-serveurl';
import {getAuthClientForUrl} from './helpers/get-auth-client-for-url';
-import {parseCloudRunUrl} from './helpers/parse-cloud-run-url';
export type RenderMediaOnCloudrunInput = {
authenticatedRequest: boolean;
@@ -31,12 +30,6 @@ export type RenderMediaOnCloudrunOutput = {
error: any;
};
-export type RenderMediaOnCloudrunErrOutput = {
- message: string;
- error: any;
- status: string;
-};
-
/**
* @description Triggers a render on a GCP Cloud Run service given a composition and a Cloud Run URL.
* @see [Documentation](https://remotion.dev/docs/lambda/renderMediaOnGcp)
@@ -64,13 +57,11 @@ export const renderMediaOnCloudrun = async ({
outputBucket,
outputFile,
updateRenderProgress,
-}: RenderMediaOnCloudrunInput): Promise<any> => {
+}: RenderMediaOnCloudrunInput): Promise<RenderMediaOnCloudrunOutput> => {
const actualCodec = validateCloudrunCodec(codec);
validateServeUrl(serveUrl);
validateCloudRunUrl(cloudRunUrl);
- const cloudRunInfo = parseCloudRunUrl(cloudRunUrl);
-
// todo: allow serviceName to be passed in, and fetch the cloud run URL based on the name
const data = {
@@ -86,8 +77,7 @@ export const renderMediaOnCloudrun = async ({
if (authenticatedRequest) {
const client = await getAuthClientForUrl(cloudRunUrl);
- const dataPromise = new Promise((resolve, reject) => {
- let response = {};
+ const authenticatedDataPromise = new Promise((resolve, reject) => {
client
.request({
url: cloudRunUrl,
@@ -101,7 +91,7 @@ export const renderMediaOnCloudrun = async ({
stream.on('data', (chunk: Buffer) => {
const chunkResponse = JSON.parse(chunk.toString());
if (chunkResponse.response) {
- response = chunkResponse.response;
+ authenticatedResponse = chunkResponse.response;
} else if (chunkResponse.onProgress) {
updateRenderProgress?.(chunkResponse.onProgress);
}
@@ -117,48 +107,29 @@ export const renderMediaOnCloudrun = async ({
});
});
- try {
- const response = await dataPromise;
- return response;
- } catch (e) {
- return {
- // TODO: How do we get the project ID?
- message: `Cloud Run Service failed. View logs at https://console.cloud.google.com/run/detail/${cloudRunInfo.region}/${cloudRunInfo.serviceName}/logs?project={PROJECT_ID}`,
- error: e,
- status: 'error',
- };
- }
- } else {
- const dataPromise = new Promise((resolve, reject) => {
- let response = {};
- got.stream
- .post(cloudRunUrl, {json: data})
- .on('data', (chunk) => {
- const chunkResponse = JSON.parse(chunk.toString());
- if (chunkResponse.response) {
- response = chunkResponse.response;
- } else if (chunkResponse.onProgress) {
- updateRenderProgress?.(chunkResponse.onProgress);
- }
- })
- .on('end', () => {
- resolve(response);
- })
- .on('error', (error) => {
- reject(error);
- });
- });
-
- try {
- const response = await dataPromise;
- return response;
- } catch (e) {
- return {
- // TODO: How do we get the project ID?
- message: `Cloud Run Service failed. View logs at https://console.cloud.google.com/run/detail/${cloudRunInfo.region}/${cloudRunInfo.serviceName}/logs?project={PROJECT_ID}`,
- error: e,
- status: 'error',
- };
- }
+ let authenticatedResponse = await authenticatedDataPromise;
+ return authenticatedResponse as RenderMediaOnCloudrunOutput;
}
+
+ const dataPromise = new Promise((resolve, reject) => {
+ got.stream
+ .post(cloudRunUrl, {json: data})
+ .on('data', (chunk) => {
+ const chunkResponse = JSON.parse(chunk.toString());
+ if (chunkResponse.response) {
+ response = chunkResponse.response;
+ } else if (chunkResponse.onProgress) {
+ updateRenderProgress?.(chunkResponse.onProgress);
+ }
+ })
+ .on('end', () => {
+ resolve(response);
+ })
+ .on('error', (error) => {
+ reject(error);
+ });
+ });
+
+ let response = await dataPromise;
+ return response as RenderMediaOnCloudrunOutput;
};
diff --git a/packages/cloudrun/src/api/render-still-on-cloudrun.ts b/packages/cloudrun/src/api/render-still-on-cloudrun.ts
index 8011820e90..ce559d3be8 100644
--- a/packages/cloudrun/src/api/render-still-on-cloudrun.ts
+++ b/packages/cloudrun/src/api/render-still-on-cloudrun.ts
@@ -2,7 +2,6 @@ import got from 'got';
import {validateCloudRunUrl} from '../shared/validate-cloudrun-url';
import {validateServeUrl} from '../shared/validate-serveurl';
import {getAuthClientForUrl} from './helpers/get-auth-client-for-url';
-import {parseCloudRunUrl} from './helpers/parse-cloud-run-url';
export type RenderStillOnCloudrunInput = {
authenticatedRequest: boolean;
@@ -26,12 +25,6 @@ export type RenderStillOnCloudrunOutput = {
error: any;
};
-export type RenderStillOnCloudrunErrOutput = {
- message: string;
- error: any;
- status: string;
-};
-
/**
* @description Triggers a render on a GCP Cloud Run service given a composition and a Cloud Run URL.
* @see [Documentation](https://remotion.dev/docs/cloudrun/renderStillOnCloudrun)
@@ -56,14 +49,10 @@ export const renderStillOnCloudrun = async ({
inputProps,
outputBucket,
outputFile,
-}: RenderStillOnCloudrunInput): Promise<
- RenderStillOnCloudrunOutput | RenderStillOnCloudrunErrOutput
-> => {
+}: RenderStillOnCloudrunInput): Promise<RenderStillOnCloudrunOutput> => {
validateServeUrl(serveUrl);
validateCloudRunUrl(cloudRunUrl);
- const cloudRunInfo = parseCloudRunUrl(cloudRunUrl);
-
// todo: allow serviceName to be passed in, and fetch the cloud run URL based on the name
const data = {
@@ -78,34 +67,16 @@ export const renderStillOnCloudrun = async ({
if (authenticatedRequest) {
const client = await getAuthClientForUrl(cloudRunUrl);
- try {
- const response = await client.request({
- url: cloudRunUrl,
- method: 'POST',
- data,
- });
- return response.data as RenderStillOnCloudrunOutput;
- } catch (e) {
- return {
- // TODO: How do we get the project ID?
- message: `Cloud Run Service failed. View logs at https://console.cloud.google.com/run/detail/${cloudRunInfo.region}/${cloudRunInfo.serviceName}/logs?project={PROJECT_ID}`,
- error: e,
- status: 'error',
- };
- }
- } else {
- try {
- const response: RenderStillOnCloudrunOutput = await got
- .post(cloudRunUrl, {json: data})
- .json();
- return response;
- } catch (e) {
- return {
- // TODO: How do we get the project ID?
- message: `Cloud Run Service failed. View logs at https://console.cloud.google.com/run/detail/${cloudRunInfo.region}/${cloudRunInfo.serviceName}/logs?project={PROJECT_ID}`,
- error: e,
- status: 'error',
- };
- }
+ const authenticatedResponse = await client.request({
+ url: cloudRunUrl,
+ method: 'POST',
+ data,
+ });
+ return authenticatedResponse.data as RenderStillOnCloudrunOutput;
}
+
+ const response: RenderStillOnCloudrunOutput = await got
+ .post(cloudRunUrl, {json: data})
+ .json();
+ return response;
};
diff --git a/packages/cloudrun/src/cli/commands/render/renderMedia.ts b/packages/cloudrun/src/cli/commands/render/renderMedia.ts
index b8a018cd8e..482e4cc482 100644
--- a/packages/cloudrun/src/cli/commands/render/renderMedia.ts
+++ b/packages/cloudrun/src/cli/commands/render/renderMedia.ts
@@ -1,8 +1,5 @@
import {CliInternals} from '@remotion/cli';
-import type {
- RenderMediaOnCloudrunErrOutput,
- RenderMediaOnCloudrunOutput,
-} from '../../../api/render-media-on-cloudrun';
+import type {RenderMediaOnCloudrunOutput} from '../../../api/render-media-on-cloudrun';
import {renderMediaOnCloudrun} from '../../../api/render-media-on-cloudrun';
import type {CloudrunCodec} from '../../../shared/validate-gcp-codec';
// import {validateMaxRetries} from '../../../shared/validate-retries';
@@ -87,22 +84,16 @@ Sending request to Cloud Run:
outputFile: outName,
updateRenderProgress,
});
+ renderProgress.doneIn = Date.now() - renderStart;
+ updateProgress();
- if (res.status === 'error') {
- const err = res as RenderMediaOnCloudrunErrOutput;
- Log.error(CliInternals.chalk.red(err.message));
- throw err.error;
- } else {
- renderProgress.doneIn = Date.now() - renderStart;
- updateProgress();
-
- const success = res as RenderMediaOnCloudrunOutput;
- Log.info(`
+ const success = res as RenderMediaOnCloudrunOutput;
+ Log.info(`
`);
- Log.info(
- CliInternals.chalk.blueBright(
- `
+ Log.info(
+ CliInternals.chalk.blueBright(
+ `
🀘 Rendered on Cloud Run! 🀘
Public URL = ${success.publicUrl}
@@ -111,7 +102,6 @@ Sending request to Cloud Run:
Bucket Name = ${success.bucketName}
Render ID = ${success.renderId}
`.trim()
- )
- );
- }
+ )
+ );
};
diff --git a/packages/cloudrun/src/cli/commands/render/renderStill.ts b/packages/cloudrun/src/cli/commands/render/renderStill.ts
index fd28165eb8..da449f2d61 100644
--- a/packages/cloudrun/src/cli/commands/render/renderStill.ts
+++ b/packages/cloudrun/src/cli/commands/render/renderStill.ts
@@ -1,10 +1,6 @@
import {CliInternals} from '@remotion/cli';
-import type {
- RenderStillOnCloudrunErrOutput,
- RenderStillOnCloudrunOutput,
-} from '../../../api/render-still-on-cloudrun';
+import type {RenderStillOnCloudrunOutput} from '../../../api/render-still-on-cloudrun';
import {renderStillOnCloudrun} from '../../../api/render-still-on-cloudrun';
-// import {validateMaxRetries} from '../../../shared/validate-retries';
import {Log} from '../../log';
import {renderArgsCheck} from './helpers/renderArgsCheck';
@@ -70,22 +66,16 @@ Sending request to Cloud Run:
outputBucket,
outputFile: outName,
});
+ doneIn = Date.now() - renderStart;
+ updateProgress();
- if (res.status === 'error') {
- const err = res as RenderStillOnCloudrunErrOutput;
- Log.error(CliInternals.chalk.red(err.message));
- throw err.error;
- } else {
- doneIn = Date.now() - renderStart;
- updateProgress();
-
- const success = res as RenderStillOnCloudrunOutput;
- Log.info(`
+ const success = res as RenderStillOnCloudrunOutput;
+ Log.info(`
`);
- Log.info(
- CliInternals.chalk.blueBright(
- `
+ Log.info(
+ CliInternals.chalk.blueBright(
+ `
🀘 Rendered on Cloud Run! 🀘
Public URL = ${success.publicUrl}
@@ -94,7 +84,6 @@ Sending request to Cloud Run:
Bucket Name = ${success.bucketName}
Render ID = ${success.renderId}
`.trim()
- )
- );
- }
+ )
+ );
};
diff --git a/packages/cloudrun/src/test/parse-cloud-run.test.ts b/packages/cloudrun/src/test/parse-cloud-run.test.ts
deleted file mode 100644
index bff5af6405..0000000000
--- a/packages/cloudrun/src/test/parse-cloud-run.test.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import {expect, test} from 'vitest';
-import {parseCloudRunUrl} from '../api/helpers/parse-cloud-run-url';
-
-test('Should parse Cloud Run URL correctly', () => {
- const result = parseCloudRunUrl(
- 'https://remotion-consumer-2iigzhq5wa-ue.a.run.app'
- );
-
- expect(result).toEqual({
- region: 'us-east1',
- projectHash: '2iigzhq5wa',
- serviceName: 'remotion-consumer',
- });
-});