0

I have a list of Job IDs to check their status. So, I'm simply looping through all the Job IDs to get their status on Media Convert.

function get_aws_job_id_status($job_id)
{
    $result = [];
    $client = \App::make('aws')->createClient('MediaConvert', [

        // 'profile' => 'default',
        // 'version' => '2017-08-29',
        'region'  => 'region',
        'endpoint' => "endpoint"
    ]);
    try {
        $result = $client->getJob([
            'Id' => $job_id,
        ]);
        return $result;
    } catch (AwsException $e) {
        return $result;
    }
}

I'm using the above function inside the loop to get the status. Referred to AWS Docs and Stackoverflow, but still, when I don't find a record for the given Job ID, it returns "NotFoundException" error that is not going in catch block and breaking the loop. Is there any way to handle that exception so I can continue the loop?

Vivek Choudhary
  • 634
  • 8
  • 14

1 Answers1

0

I believe you need to call Aws\MediaConvert\Exception\MediaConvertException and catch for MediaConvert specific errors. I don't see any of your use statements but I assume the code would look something like the following.

Note I am catching for all MediaConvert client errors, but I believe you could specifically call out the NotFoundException by doing Aws\MediaConvert\Exception\MediaConvertException\NotFoundException

use Aws\MediaConvert\MediaConvertClient;
use Aws\Exception\AwsException;
use Aws\MediaConvert\Exception\MediaConvertException as MediaConvertError;


function get_aws_job_id_status($job_id)
{
    $result = [];
    $client = \App::make('aws')->createClient('MediaConvert', [

        // 'profile' => 'default',
        // 'version' => '2017-08-29',
        'region'  => 'region',
        'endpoint' => "endpoint"
    ]);
    try {
        $result = $client->getJob([
            'Id' => $job_id,
        ]);
        return $result;
    } catch (MediaConvertError $e) {
    
           /*Oh no, the job id provided ca not be found.
             Let us log the job id and the message and return it back up to the main application
             Note this assumes the main application is iterating through a list of JobIDs and 
             can handle this and log that job id was not found and will not have the normal Job
             JSON structure. 
           */
        $error = array("Id"=>$job_id, "Message"=>"Job Id Not found");
        $result = json_encode($error);
        return $result;
    }
}

Also keep in mind that if you are polling for job status's you may be throttled if your list grows too big. You would need to catch for a TooManyRequestsException [1] and try the poll with a back off threshold [2].

The most scalable solution is to use CloudWatch Events and track jobs based the STATUS_UPDATE, COMPLETE and ERROR status. [3]

[1] https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.MediaConvert.Exception.MediaConvertException.html
[2] https://docs.aws.amazon.com/general/latest/gr/api-retries.html
[3] https://docs.aws.amazon.com/mediaconvert/latest/ug/monitoring-overview.html

jjohn
  • 233
  • 1
  • 3