Merge in RR/mini_queue from storage_debugging to master
Squashed commit of the following:
commit 34b1353ebbf73c53891689be0fb533e857818b40
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Feb 16 13:47:18 2022 +0100
flask debug mode
commit a3726653ac7603c1b4f35b53745d010066a4eb2a
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Feb 16 13:36:15 2022 +0100
running flask in seperate process
commit ec8dcc858cd03a30f009749dcb3ec40786dde0a3
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Feb 16 13:25:42 2022 +0100
added integrity checks
commit e4180478190c80d8ce2d83aadb0990563f56f497
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Wed Feb 16 12:55:41 2022 +0100
refactoring; added debugging void callback
72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
import logging
|
|
from multiprocessing import Process
|
|
import pika
|
|
from waitress import serve
|
|
from flask import Flask, request, jsonify
|
|
|
|
from mini_queue.utils.config import CONFIG
|
|
from mini_queue.utils.rabbitmq import make_channel, declare_queue, make_callback, read_connection_params
|
|
|
|
|
|
def start_integrity_checks_webserver(mode="debug"):
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/ready", methods=["GET"])
|
|
def ready():
|
|
resp = jsonify("OK")
|
|
resp.status_code = 200
|
|
return resp
|
|
|
|
@app.route("/health", methods=["GET"])
|
|
def healthy():
|
|
resp = jsonify("OK")
|
|
resp.status_code = 200
|
|
return resp
|
|
|
|
if mode == "debug":
|
|
app.run(host=CONFIG.webserver.host, port=CONFIG.webserver.port, debug=True)
|
|
elif mode == "production":
|
|
serve(app, host=CONFIG.webserver.host, port=CONFIG.webserver.port)
|
|
|
|
|
|
def main():
|
|
|
|
logging.info("Starting mini-queue...")
|
|
|
|
parameters = read_connection_params()
|
|
connection = pika.BlockingConnection(parameters)
|
|
channel = make_channel(connection)
|
|
declare_queue(channel, CONFIG.rabbitmq.queues.input)
|
|
|
|
logging.info("Starting webserver...")
|
|
|
|
p = Process(target=start_integrity_checks_webserver, args=("debug", ))
|
|
p.start()
|
|
|
|
while True:
|
|
try:
|
|
channel.basic_consume(
|
|
queue=CONFIG.rabbitmq.queues.input, auto_ack=False, on_message_callback=make_callback("dummy")
|
|
)
|
|
logging.info(" [*] Waiting for messages. To exit press CTRL+C")
|
|
channel.start_consuming()
|
|
|
|
except pika.exceptions.ConnectionClosedByBroker as err:
|
|
logging.info(f"Caught a channel error: {err}, retrying...")
|
|
continue
|
|
except pika.exceptions.AMQPChannelError as err:
|
|
logging.critical(f"Caught a channel error: {err}, stopping...")
|
|
break
|
|
except pika.exceptions.AMQPConnectionError:
|
|
logging.info("Connection was closed, retrying...")
|
|
continue
|
|
|
|
p.join()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
logging_level = CONFIG.service.logging_level
|
|
logging.basicConfig(level=logging_level)
|
|
main()
|