diff --git a/src/main.py b/src/main.py index 1fc8f94..2b72054 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +import asyncio import json import logging @@ -16,6 +17,8 @@ from settings import KafkaSettings, OtelSettings, SchipholApiSettings logger = logging.getLogger(__name__) +POLL_INTERVAL_SECONDS = 300 # 5 minutes between Polls + async def poll(session, stream_engine, api_settings, kafka_settings, tracer) -> int: """Fetch all Flights from the Schiphol API and publish a Snapshot for each to Kafka. @@ -93,10 +96,23 @@ async def start(): } ) - total_flights = await poll(session, stream_engine, api_settings, kafka_settings, tracer) - logger.info(f"Published {total_flights} flights to Kafka topic '{kafka_settings.topic}'") - - await stream_engine.stop() + try: + while True: + with tracer.start_as_current_span("poll_cycle") as span: + try: + total_flights = await poll( + session, stream_engine, api_settings, kafka_settings, tracer + ) + span.set_attribute("flights.published", total_flights) + logger.info( + f"Published {total_flights} flights to Kafka topic '{kafka_settings.topic}'" + ) + except Exception: + span.set_attribute("flights.published", 0) + logger.exception("Poll failed — will retry after %ds", POLL_INTERVAL_SECONDS) + await asyncio.sleep(POLL_INTERVAL_SECONDS) + finally: + await stream_engine.stop() async def shutdown(loop):