"""Lambda sr-fanout function module.""" import datetime import sentry_sdk from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration from .common import logger from .connectors import ows_sound_recordings import config # initialize sentry 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(event, context): """Lambda entry point.""" try: data_modified_at = event['timestamp'] data_type = event['label'] data_id = event['id'] # verify input data if data_type not in ('Vendor', 'SubAccount'): raise Exception(f'Unexpected data type of "{data_type}"') dt = datetime.datetime.fromisoformat(data_modified_at) if dt.tzinfo != datetime.timezone.utc: raise Exception(f'Timestamp tzinfo "{dt.tzinfo}" is not UTC') # match modify data until no records left total_updates = 0 batch_size = config.BATCH_SIZE while True: num_updates = ows_sound_recordings.touch_sound_recordings( data_id, data_type, data_modified_at, batch_size ) total_updates += num_updates if num_updates < batch_size: break return { 'total_updates': total_updates } except Exception as e: logger.exception(str(e)) raise e