"""Lambda sr-delivery-tiktok-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 from .constants import EVENTS_BATCH_SIZE # 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 _process_events_batch_to_sfn(batch, exc_names): sfn.throttle(sfn_client, len(batch)) for ev in batch: exc_name, started = sfn.execute_sfn(sfn_client, ev) if started: exc_names['started'].append(exc_name) else: exc_names['skipped'].append(exc_name) return exc_names def handler(raw_events, context): """Lambda entry point.""" try: all_events = input_events.decode_events(raw_events) # Use generator for eligible events eligible_events = get_eligible_events(all_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 # lots 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) batch = [] total_eligible = 0 for event in eligible_events: batch.append(event) total_eligible += 1 if len(batch) == EVENTS_BATCH_SIZE: exc_names = _process_events_batch_to_sfn(batch, exc_names) batch = [] # Process any remaining events if batch: exc_names = _process_events_batch_to_sfn(batch, exc_names) 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