"""Lambda function module.""" from typing import Dict import uuid import boto3 import config from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.schemas.state_machine_schema import ( StateMachineSchema ) logger = logging_utils.get_logger(config.app_logger) s3_client = boto3.client('s3') def handler(event: Dict, context: object) -> Dict: """Lambda entrypoint.""" # If the execution fails on parse_ddex we have no context data # parse_ddex happens before creating the ingestion lock so we can skip this if not event.get('key') and not event.get('bucket'): logger.info('Context not found in event payload.') return event state_machine_data = StateMachineSchema().load(event) correlation_id = state_machine_data.correlation_id or str(uuid.uuid4()) state_machine_data.correlation_id = correlation_id logging_utils.update_logger_correlation_id(logger, correlation_id) logging_utils.update_logger_with_message_ids( logger, state_machine_data.message_id, state_machine_data.message_thread_id, state_machine_data.execution_name ) upc = state_machine_data.product.upc lockfile_key = f'{config.INGESTION_LOCK_S3_PATH}{upc}.lock' # Delete lockfile logger.info(f'Deleting lockfile: {lockfile_key}') s3_client.delete_object( Bucket=config.INGESTION_LOCK_S3_BUCKET, Key=lockfile_key ) if state_machine_data.product.display_upc: display_upc = state_machine_data.product.display_upc lockfile_key = f'{config.INGESTION_LOCK_S3_PATH}{display_upc}.lock' # Delete lockfile logger.info(f'Deleting lockfile: {lockfile_key}') s3_client.delete_object( Bucket=config.INGESTION_LOCK_S3_BUCKET, Key=lockfile_key ) return StateMachineSchema().dump(state_machine_data)