"""Lambda sr-delivery-youtube-filter function module.""" import os from .common import logger from .common.connectors.sfn import sfn_client import sentry_sdk from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration import config from src.delivery_eligibility import get_eligible_events from src.utils import input_events import src.utils.step_function as sfn # Initialize Sentry sentry_dsn = os.environ.get('SENTRY_DSN', config.secrets_manager_client.get_cred('SENTRY_DSN')) if sentry_dsn: logger.info('Initializing with sentry') sentry_sdk.init( sentry_dsn, integrations=[AwsLambdaIntegration()] ) else: logger.info('Initializing without sentry') def handler(raw_events, context): """Lambda entry point.""" try: all_events = input_events.decode_events(raw_events) eligible_events = get_eligible_events(all_events) logger.info(f'Input events: {len(all_events)}. Eligible events: {len(eligible_events)}') # For now, we will execute the delivery SFN for each eligible event. exc_names: dict[str, list] = { 'skipped': [], 'started': [] } # iterate over output_events in batch sizes of 10 # to avoid max concurrency issues with SFN # This is needed if we are handling a product with # lot of tracks on it (e.g. a product with a number of # tracks > SFN_DELIVERY_MAX_RUNNING would never # make it past the throttle without batching) sfn.throttle(sfn_client, len(eligible_events)) for event in eligible_events: (exc_name, started) = sfn.execute_sfn(sfn_client, event) if started: exc_names['started'].append(exc_name) else: exc_names['skipped'].append(exc_name) summary = { 'num_input_records': len(all_events), 'sfn_executions': exc_names } logger.info(summary) return summary except Exception as e: logger.exception(str(e)) raise e