Skip to content

Commit

Permalink
feat: check and log mediaconvert job status
Browse files Browse the repository at this point in the history
  • Loading branch information
oshinongit committed Sep 30, 2024
1 parent df4ce7f commit 163c2c2
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions src/pipelines/aws/aws-pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ECSClient, RunTaskCommand } from '@aws-sdk/client-ecs';
import {
CreateJobCommand,
CreateJobCommandOutput,
GetJobCommand,
MediaConvertClient
} from '@aws-sdk/client-mediaconvert';
import {
Expand Down Expand Up @@ -40,6 +42,7 @@ export default class AWSPipeline implements Pipeline {
private mediaConvert: MediaConvertClient;
private ecs: ECSClient;
private static readonly MAX_WAIT_TIME = 28800; //Max wait time for AWS resources is 28800 seconds (8 hours).
private static readonly MEDIACONVERT_CHECK_JOB_INTERVAL_S = 15; //Check status of mediaconvert job interval in seconds.

constructor(configuration: AWSPipelineConfiguration) {
this.configuration = configuration;
Expand Down Expand Up @@ -84,8 +87,52 @@ export default class AWSPipeline implements Pipeline {
}
}

async waitForObjectInS3(S3Bucket: string, S3Key: string): Promise<boolean> {
async mediaConvertJobStatus(
mediaConvert: MediaConvertClient,
jobId: string
): Promise<string | undefined> {
try {
const command = new GetJobCommand({ Id: jobId });
const response = await mediaConvert.send(command);
return response.Job?.Status || undefined;
} catch (error) {
logger.error(`Error getting job ${jobId}: \n Error: ${error}`);
return undefined;
}
}

checkJobStatusWithInterval(
mediaConvert: MediaConvertClient,
jobId: string,
interval: number
) {
const intervalId = setInterval(async () => {
const status: string | undefined = await this.mediaConvertJobStatus(
mediaConvert,
jobId
);

if (status === 'COMPLETED' || 'ERROR' || 'CANCELED') {
clearInterval(intervalId);
} else {
logger.debug(`Job ${jobId} status: ${status}. Waiting...`);
}
}, interval);
}

async waitForObjectInS3(
S3Bucket: string,
S3Key: string,
jobId?: string
): Promise<boolean> {
try {
if (jobId) {
this.checkJobStatusWithInterval(
this.mediaConvert,
jobId,
AWSPipeline.MEDIACONVERT_CHECK_JOB_INTERVAL_S
);
}
await waitUntilObjectExists(
{ client: this.s3, maxWaitTime: AWSPipeline.MAX_WAIT_TIME },
{ Bucket: S3Bucket, Key: S3Key }
Expand Down Expand Up @@ -230,13 +277,14 @@ export default class AWSPipeline implements Pipeline {

// Transcode
logger.info('Transcoding ' + inputFilename + ' to ' + outputURI + '...');
let createJobResponse: CreateJobCommandOutput;
try {
const accelerationSettings = this.configuration.accelerationMode
? {
AccelerationSettings: { Mode: this.configuration.accelerationMode }
}
: {};
await this.mediaConvert.send(
createJobResponse = await this.mediaConvert.send(
new CreateJobCommand({
Role: this.configuration.mediaConvertRole,
Settings: settings,
Expand All @@ -249,10 +297,10 @@ export default class AWSPipeline implements Pipeline {
);
throw error;
}

const s3Status = await this.waitForObjectInS3(
outputBucket,
`${outputFolder}/${outputObject}`
`${outputFolder}/${outputObject}`,
createJobResponse.Job?.Id
);
if (!s3Status) return '';
await this.probeMetadata(outputBucket, outputFolder, outputObject);
Expand Down

0 comments on commit 163c2c2

Please sign in to comment.