import logging, json import utils, rabbitmq, island logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # rabbit config exchange = utils.env('EXCHANGE', 'octopus') queue_name = utils.env('QUEUE', 'island_file_gen') routing_key = utils.env('INPUT_RK', 'file_generate') rabbit_conn = rabbitmq.get_connection() rabbitmq.create_exchange(exchange, rabbit_conn); rabbitmq.create_queue(queue_name, exchange, routing_key, rabbit_conn) def file_generate_callback(ch, method, properties, body): logger.info("Generating file [%s]" % body) p_body = json.loads(body) map_gen_id = p_body['map_gen_id'] file_map_id = p_body['file_map_id'] try: gen_id = island.generate_file(map_gen_id, file_map_id) logger.info("map_gen_file_id: %s", gen_id) except Exception as e: logger.error(e) rabbitmq.send_to_retry("Failed", properties) rabbitmq.consumer(queue_name, file_generate_callback, rabbit_conn)
@app.route("/log", methods=['POST']) async def log(request): logger.info("received => [%s]" % request.body) return text('OK') @app.route("/rabbitmq/<exchange>/<routing_key>", methods=['POST']) async def log(request, exchange, routing_key): rabbitmq.publish(exchange, routing_key, request.body) return text('OK') @app.route("/health") async def health(request): return text('OK') if __name__ == "__main__": import utils APP_PORT = int(utils.env("APP_PORT", 8000)) APP_WORKERS = int(utils.env("APP_WORKERS", 1)) exchange = utils.env('EXCHANGE') if exchange: rabbitmq.create_exchange(exchange) app.run(host="0.0.0.0", port=APP_PORT, workers=APP_WORKERS)
import json, logging import rabbitmq, api_client exchange = 'seeder' queue_name = 'restart-app' conn = rabbitmq.get_connection() rabbitmq.create_exchange(exchange, conn) rabbitmq.create_queue(queue_name, exchange, 'seeded', conn) def callback(ch, method, properties, body): p_body = json.loads(body) api_client.k8s_bounce_app(p_body['app']) rabbitmq.consumer(queue_name, callback, conn)