"""Module to handle transcoding_job status.""" from typing import Any from owsresponse import response from transcoding.constants import error, transcoding as transcoding_constants from transcoding.logic import ( asset_status, messaging, transcoding_order as transcoding_order_logic, ) from transcoding.models import transcoding_job as transcoding_job_model def _process_completed_status( transcoding_job_data: dict[str, Any], output_bucket: str | None, output_key: str | None, metadata: dict[str, Any] | None, ) -> response.Response: transcoding_job_id = transcoding_job_data["transcoding_job_id"] update_response = transcoding_job_model.update_transcoding_job_status( transcoding_job_id=transcoding_job_id, new_status=transcoding_constants.COMPLETED_STATUS, output_bucket=output_bucket, output_key=output_key, metadata=metadata, ) if not update_response: return update_response transcoding_order_id = transcoding_job_data["transcoding_order_id"] order_status_response = transcoding_order_logic.get_status(transcoding_order_id) if not order_status_response: return order_status_response order_status = order_status_response.message if order_status["status"] == transcoding_constants.COMPLETED_STATUS: order_jobs = order_status["transcoding_jobs"] result_assets = [] for order_job in order_jobs: result_assets.append( { "key": order_job["output_key"], "bucket": order_job["output_bucket"], "container": order_job["container"], "duration": order_job["duration"], "channels": order_job["channels"], "codec": order_job["codec"], "sample_rate": order_job["sample_rate"], "bit_rate": order_job["bit_rate"], "bit_depth": order_job["bit_depth"], } ) update_status_response = asset_status.update_asset_final_status( transcoding_job_id=transcoding_job_id, status=transcoding_constants.COMPLETED_STATUS, final_assets=result_assets, ) if not update_status_response: return update_status_response return response.Response({"status": error.SUCCESS_CODE}) def _process_failed_status( transcoding_job_data: dict[str, Any], status_description: str, ) -> response.Response: transcoding_job_id = transcoding_job_data["transcoding_job_id"] update_response = transcoding_job_model.update_transcoding_job_status( transcoding_job_id, transcoding_constants.ERROR_STATUS, status_description ) if not update_response: return update_response update_status_response = asset_status.update_asset_final_status( transcoding_job_id=transcoding_job_id, status=transcoding_constants.ERROR_STATUS, status_description=status_description, ) if not update_status_response: return update_status_response return response.Response({"status": error.SUCCESS_CODE}) def _process_retryable_error_status( transcoding_job_data: dict[str, Any], status_description: str ) -> response.Response: transcoding_job_id = transcoding_job_data["transcoding_job_id"] attempt = transcoding_job_data["attempt"] if attempt != transcoding_constants.MAX_ATTEMPT_COUNT: attempt += 1 update_counter_response = ( transcoding_job_model.update_transcoding_job_attempt_counter( transcoding_job_id, attempt ) ) if not update_counter_response: return update_counter_response resend_response = messaging.resend_transcoding_job_sqs_message( transcoding_job_id ) if not resend_response: return resend_response else: description = "All attempts failed. Last error: {description}".format( description=status_description ) update_response = transcoding_job_model.update_transcoding_job_status( transcoding_job_id, transcoding_constants.ERROR_STATUS, description ) if not update_response: return update_response update_status_response = asset_status.update_asset_final_status( transcoding_job_id=transcoding_job_id, status=transcoding_constants.ERROR_STATUS, status_description=description, ) if not update_status_response: return update_status_response return response.Response({"status": error.SUCCESS_CODE}) def process_transcoding_status( transcoding_job_id: int, status: str, status_description: str | None = None, output_bucket: str | None = None, output_key: str | None = None, metadata: dict[str, Any] | None = None, ) -> response.Response: """Update transcoding job status. Args: transcoding_job_id (int): Transcoding job unique id. status (str): Status of job. status_description (str): Status description output_bucket (str): Transcoded file S3 bucket. output_key (str): Transcoded file name. metadata (dict): Transcoded file metadata. Returns: response.Response: Message with success or error message. """ transcoding_job_response = transcoding_job_model.get_transcoding_job_by_id( transcoding_job_id ) if not transcoding_job_response: return transcoding_job_response if status in transcoding_constants.JOB_STATUS_COMPLETED: return _process_completed_status( transcoding_job_response.message, output_bucket, output_key, metadata, ) elif status == transcoding_constants.JOB_STATUS_FATAL_ERROR: return _process_failed_status( transcoding_job_response.message, status_description or "" ) elif status == transcoding_constants.JOB_STATUS_RETRYABLE_ERROR: return _process_retryable_error_status( transcoding_job_response.message, status_description or "" ) else: return response.create_error_response( "unknown_status", "Status {status} is unknown".format(status=status) )