def listen_auth_logout(): exchange = 'auth' try: conn = pika.BlockingConnection( pika.ConnectionParameters(host=config.get_rabbit_server_url())) chan = conn.channel() chan.exchange_declare(exchange=exchange, exchange_type='fanout') result = chan.queue_declare(exclusive=True) queue_name = result.method.queue chan.queue_bind(exchange=exchange, queue=queue_name) def callback(ch, method, properties, body): event = json.loads(body.decode()) if event.get('type', 'None') == 'logout': security.invalidate_session(event.get('message')) chan.basic_consume(callback, queue=queue_name, no_ack=True) chan.start_consuming() except Exception as e: print(e) Timer(10.0, init).start()
def listenAuth(): EXCHANGE = "auth" try: connection = pika.BlockingConnection( pika.ConnectionParameters(host=config.get_rabbit_server_url())) channel = connection.channel() channel.exchange_declare(exchange=EXCHANGE, exchange_type='fanout') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange=EXCHANGE, queue=queue_name) def callback(ch, method, properties, body): event = json.body_to_dic(body.decode('utf-8')) if (len(validator.validateSchema(EVENT, event)) > 0): return if (event["type"] == "logout"): security.invalidateSession(event["message"]) print("RabbitMQ Auth conectado") channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming() except Exception: print("RabbitMQ Auth desconectado, intentando reconectar en 10'") threading.Timer(10.0, initAuth).start()
def sendArticleData(exchange, queue, referenceId, articleId, valid, stock, price): """ Envía eventos al Cart article-data : Es una validación solicitada por Cart para validar si el articulo puede incluirse en el cart @api {direct} cart/article-data Validación de Articulos @apiGroup RabbitMQ POST @apiDescription Enviá de mensajes article-exist desde cart. Valida articulos @apiSuccessExample {json} Mensaje { "type": "article-exist", "message" : { "referenceId": "{referenceId}", "articleId": "{articleId}", "valid": True|False, "stock": {stock}, "price": {price} } } """ connection = pika.BlockingConnection( pika.ConnectionParameters(host=config.get_rabbit_server_url())) channel = connection.channel() channel.exchange_declare(exchange=exchange, exchange_type='direct') channel.queue_declare(queue=queue) message = { "type": "article-data", "message": { "referenceId": referenceId, "articleId": articleId, "valid": valid, "stock": stock, "price": price, } } channel.basic_publish(exchange=exchange, routing_key=queue, body=json.dic_to_json(message)) connection.close() print( "RabbitMQ Cart POST article-data catalogId:%r , articleId:%r , valid:%r", referenceId, articleId, valid)
def listenAuth(): """ Básicamente eventos de logout enviados por auth. @api {fanout} auth/logout Logout @apiGroup RabbitMQ GET @apiDescription Escucha de mensajes logout desde auth. Invalida sesiones en cache. @apiExample {json} Mensaje { "type": "article-exist", "message" : "tokenId" } """ EXCHANGE = "auth" try: connection = pika.BlockingConnection( pika.ConnectionParameters(host=config.get_rabbit_server_url())) channel = connection.channel() channel.exchange_declare(exchange=EXCHANGE, exchange_type='fanout') result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.queue_bind(exchange=EXCHANGE, queue=queue_name) def callback(ch, method, properties, body): event = json.body_to_dic(body.decode('utf-8')) if (len(validator.validateSchema(EVENT, event)) > 0): return if (event["type"] == "logout"): security.invalidateSession(event["message"]) print("RabbitMQ Auth conectado") channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming() except Exception: print("RabbitMQ Auth desconectado, intentando reconectar en 10'") threading.Timer(10.0, initAuth).start()
def sendNewDiscount(exchange, queue, type, Discounts): # print("LLAMA A LA FUNCION") connection = pika.BlockingConnection( pika.ConnectionParameters(host=config.get_rabbit_server_url())) channel = connection.channel() channel.exchange_declare(exchange=exchange, exchange_type='fanout') channel.queue_declare(queue=queue) message = {"type": type, "message": Discounts} channel.basic_publish(exchange=exchange, routing_key=queue, body=json.dic_to_json(message)) # print("llega") print(" [x] Sent %r" % message) connection.close()
def listenCatalog(): EXCHANGE = "catalog" QUEUE = "catalog" try: connection = pika.BlockingConnection( pika.ConnectionParameters(host=config.get_rabbit_server_url())) channel = connection.channel() channel.exchange_declare(exchange=EXCHANGE, exchange_type='direct') channel.queue_declare(queue=QUEUE) channel.queue_bind(queue=QUEUE, exchange=EXCHANGE, routing_key=QUEUE) def callback(ch, method, properties, body): event = json.body_to_dic(body.decode('utf-8')) if (len(validator.validateSchema(EVENT_CALLBACK, event)) > 0): return if (event["type"] == "article-exist"): message = event["message"] if (len(validator.validateSchema(MSG_ARTICLE_EXIST, message)) > 0): return exchange = event["exchange"] queue = event["queue"] referenceId = message["referenceId"] articleId = message["articleId"] print( "RabbitMQ Catalog GET article-exist catalogId:%r , articleId:%r", referenceId, articleId) try: articleValidation.validateArticleExist(articleId) sendArticleValid(exchange, queue, referenceId, articleId, True) except Exception: sendArticleValid(exchange, queue, referenceId, articleId, False) if (event["type"] == "article-data"): message = event["message"] if (len(validator.validateSchema(MSG_ARTICLE_EXIST, message)) > 0): return exchange = event["exchange"] queue = event["queue"] referenceId = message["referenceId"] articleId = message["articleId"] print( "RabbitMQ Catalog GET article-data catalogId:%r , articleId:%r", referenceId, articleId) try: article = crud.getArticle(articleId) valid = ("enabled" in article and article["enabled"]) stock = article["stock"] price = article["price"] articleValidation.validateArticleExist(articleId) sendArticleData(exchange, queue, referenceId, articleId, valid, stock, price) except Exception: sendArticleData(exchange, queue, referenceId, articleId, False, 0, 0) print("RabbitMQ Catalog conectado") channel.basic_consume(callback, queue=QUEUE, consumer_tag=QUEUE, no_ack=True) channel.start_consuming() except Exception: traceback.print_exc() print("RabbitMQ Catalog desconectado, intentando reconectar en 10'") threading.Timer(10.0, initCatalog).start()
def listenCatalog(): """ article-exist : Es una validación solicitada por Cart para validar si el articulo puede incluirse en el cart @api {direct} catalog/article-exist Validación de Articulos @apiGroup RabbitMQ GET @apiDescription Escucha de mensajes article-exist desde cart. Valida articulos @apiExample {json} Mensaje { "type": "article-exist", "exchange" : "{Exchange name to reply}" "queue" : "{Queue name to reply}" "message" : { "referenceId": "{referenceId}", "articleId": "{articleId}", } """ """ article-data : Es una validación solicitada por Cart para validar si el articulo puede incluirse en el cart @api {direct} catalog/article-exist Validación de Articulos @apiGroup RabbitMQ GET @apiDescription Escucha de mensajes article-data desde cart. Valida articulos @apiExample {json} Mensaje { "type": "article-exist", "exchange" : "{Exchange name to reply}" "queue" : "{Queue name to reply}" "message" : { "referenceId": "{referenceId}", "articleId": "{articleId}", } """ EXCHANGE = "catalog" QUEUE = "catalog" try: connection = pika.BlockingConnection( pika.ConnectionParameters(host=config.get_rabbit_server_url())) channel = connection.channel() channel.exchange_declare(exchange=EXCHANGE, exchange_type='direct') channel.queue_declare(queue=QUEUE) channel.queue_bind(queue=QUEUE, exchange=EXCHANGE, routing_key=QUEUE) def callback(ch, method, properties, body): event = json.body_to_dic(body.decode('utf-8')) if (len(validator.validateSchema(EVENT_CALLBACK, event)) > 0): return if (event["type"] == "article-exist"): message = event["message"] if (len(validator.validateSchema(MSG_ARTICLE_EXIST, message)) > 0): return exchange = event["exchange"] queue = event["queue"] referenceId = message["referenceId"] articleId = message["articleId"] print( "RabbitMQ Catalog GET article-exist catalogId:%r , articleId:%r", referenceId, articleId) try: articleValidation.validateArticleExist(articleId) sendArticleValid(exchange, queue, referenceId, articleId, True) except Exception: sendArticleValid(exchange, queue, referenceId, articleId, False) if (event["type"] == "article-data"): message = event["message"] if (len(validator.validateSchema(MSG_ARTICLE_EXIST, message)) > 0): return exchange = event["exchange"] queue = event["queue"] referenceId = message["referenceId"] articleId = message["articleId"] print( "RabbitMQ Catalog GET article-data catalogId:%r , articleId:%r", referenceId, articleId) try: article = crud.getArticle(articleId) valid = ("enabled" in article and article["enabled"]) stock = article["stock"] price = article["price"] articleValidation.validateArticleExist(articleId) sendArticleData(exchange, queue, referenceId, articleId, valid, stock, price) except Exception: sendArticleData(exchange, queue, referenceId, articleId, False, 0, 0) print("RabbitMQ Catalog conectado") channel.basic_consume(callback, queue=QUEUE, consumer_tag=QUEUE, no_ack=True) channel.start_consuming() except Exception: traceback.print_exc() print("RabbitMQ Catalog desconectado, intentando reconectar en 10'") threading.Timer(10.0, initCatalog).start()