def run_server():
    """Runs the Tornado Server and begins Kafka consumption"""
    if topic_check.topic_exists("TURNSTILE_SUMMARY") is False:
        logger.fatal(
            "Ensure that the KSQL Command has run successfully before running the web server!"
        )
        exit(1)
    if topic_check.topic_pattern_match("stations.table") is False:
        logger.fatal(
            "Ensure that Faust Streaming is running successfully before running the web server!"
        )
        exit(1)

    weather_model = Weather()
    lines = Lines()

    application = tornado.web.Application(
        [(r"/", MainHandler, {"weather": weather_model, "lines": lines})]
    )
    application.listen(8888)

    # Build kafka consumers
    consumers = [
        KafkaConsumer(
            "org.chicago.cta.weather.v1",
            weather_model.process_message,
            offset_earliest=True,
        ),
        KafkaConsumer(
            "org.chicago.cta.stations.table.v1",
            lines.process_message,
            offset_earliest=True,
            is_avro=False,
        ),
        KafkaConsumer(
            "^org.chicago.cta.stations.arrivals.",
            lines.process_message,
            offset_earliest=True,
        ),
        KafkaConsumer(
            "TURNSTILE_SUMMARY",
            lines.process_message,
            offset_earliest=True,
            is_avro=False,
        ),
    ]

    try:
        logger.info(
            "Open a web browser to http://localhost:8888 to see the Transit Status Page"
        )
        for consumer in consumers:
            tornado.ioloop.IOLoop.current().spawn_callback(consumer.consume)

        tornado.ioloop.IOLoop.current().start()
    except KeyboardInterrupt as e:
        logger.info("shutting down server")
        tornado.ioloop.IOLoop.current().stop()
        for consumer in consumers:
            consumer.close()
Esempio n. 2
0
def run_server():
    """Runs the Tornado Server and begins Kafka consumption"""
    if topic_check.topic_exists(config.TOPIC_TURNSTILE_SUMMARY) is False:
        logger.fatal("Ensure that the KSQL Command has run successfully!")
        exit(1)

    if topic_check.topic_pattern_match("stations.table") is False:
        logger.fatal("Ensure that Faust Streaming is running successfully!")
        exit(1)

    weather_model = Weather()
    lines = Lines()

    application = tornado.web.Application([(r"/", MainHandler, {
        "weather": weather_model,
        "lines": lines
    })])
    application.listen(config.WEB_SERVER_PORT)

    # Build kafka consumers
    consumers = [
        KafkaConsumer(
            config.TOPIC_WEATHER,
            weather_model.process_message,
            offset_earliest=True,
        ),
        KafkaConsumer(
            config.TOPIC_FAUST_TABLE,
            lines.process_message,
            offset_earliest=True,
            is_avro=False,
        ),
        KafkaConsumer(
            config.TOPIC_STATIONS,
            lines.process_message,
            offset_earliest=True,
        ),
        KafkaConsumer(
            config.TOPIC_TURNSTILE_SUMMARY,
            lines.process_message,
            offset_earliest=True,
            is_avro=False,
        ),
    ]

    try:
        logger.info(
            f"Open a web browser to http://localhost:{config.WEB_SERVER_PORT} to see the Transit Status Page"
        )
        for consumer in consumers:
            tornado.ioloop.IOLoop.current().spawn_callback(consumer.consume)

        tornado.ioloop.IOLoop.current().start()
    except KeyboardInterrupt as e:
        logger.info("shutting down server")
        tornado.ioloop.IOLoop.current().stop()
        for consumer in consumers:
            consumer.close()
    except Exception as ex:
        logger.fatal(f"server.py error: {ex}")
        exit(1)