import json from datetime import datetime from typing import List, Tuple import boto3 import config from json_logger import logger client = None def get_client(): """Get boto3 eventbridge client.""" global client if not client: client = boto3.client("events", region_name=config.AWS_DEFAULT_REGION) return client def put_event( instance_id: str, updated_playlists: List[str], ranks_market_codes: Tuple[List[str], List[str]] or None ) -> bool or None: """Put eventbridge message. Args: instance_id: Current job run ID. updated_playlists: Updated playlist ID list. ranks_market_codes: Market codes which ranks updated, known (with market in streams) and unknown, None if not updated. Returns: Event sent successfully or not, None if disabled. """ if not config.AWS_EVENT_BUS_NAME: return None response = get_client().put_events( Entries=[ { "Detail": json.dumps( { "topic": "apollo/apollo-new-music-friday-tracks-job/event:playlist/updated", "type": "event", "tags": ["nmf", "playlists", "tracks", "ranks"], "event": {"type": "updated"}, "entity": {"type": "playlist", "id": updated_playlists}, "source": { "project": "apollo", "service": "apollo-new-music-friday-tracks-job", "id": instance_id, }, "data": { "playlists": updated_playlists, "ranks": ( {"known": ranks_market_codes[0], "unknown": ranks_market_codes[1]} if ranks_market_codes else None ), }, "created": datetime.utcnow().isoformat(), } ), "DetailType": "new-music-friday-data", "Source": "apollo-new-music-friday-tracks-job", "EventBusName": config.AWS_EVENT_BUS_NAME, } ] ) if "FailedEntryCount" in response and response["FailedEntryCount"] > 0: logger.error("Eventbridge message sending failed.") return False return True