def receive_error(): amqp_setup.check_setup() queue_name = "Error_Music" amqp_setup.channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) amqp_setup.channel.start_consuming()
def consume(): amqp_setup.check_setup() queue_name = 'Activity_Log' amqp_setup.channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) amqp_setup.channel.start_consuming()
def send_hotel_facilities(booking_details): item_name = booking_details['item_name'] guest_name = booking_details['name'] booking_id = booking_details['booking_id'] email = booking_details['email'] item_id = booking_details['item_id'] order_datetime = booking_details['order_datetime'][:10] + " " + booking_details['order_datetime'][11:19] f_order = { "item_id": item_id, "order_datetime": order_datetime } add_fb_result = invoke_http(cart_URL + "/add_fb/" + booking_id, method='POST', json=f_order) if add_fb_result['code'] not in range(200, 300): return { 'code': 404, 'status': 'failed to add facility booking' } print("add_fb_result", add_fb_result) email_details = { "email": email, "guest_name": guest_name, "quantity": add_fb_result["data"]['rs_quantity'], "price": add_fb_result["data"]['price'], "item_name" : item_name, "date_time": add_fb_result["data"]["order_datetime"], "order_id": add_fb_result["data"]["order_id"], "booking_id": add_fb_result["data"]["booking_id"], "type": "facility" } print('email_details:', email_details) message = json.dumps(email_details) amqp_setup.check_setup() print('\n\n-----Publishing the (facility notification) message with routing_key=facility.notification-----') amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="facility.notification", body=message, properties=pika.BasicProperties(delivery_mode=2)) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) # - reply from the invocation is not used; # continue even if this invocation fails print("\nOrder status ({:d}) published to the RabbitMQ Exchange:".format( add_fb_result['code']), message) return { "code": 201, "booking_result": { "email": email, "guest_name": guest_name, "status": "success" } }
def receiveActivityLog(): amqp_setup.check_setup() queue_name = 'Activity_Log' # set up a consumer and start to wait for coming messages amqp_setup.channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) amqp_setup.channel.start_consuming() # an implicit loop waiting to receive messages;
def receiveNotification(): amqp_setup.check_setup() queue_name = 'Error_Service' # set up a consumer and start to wait for coming messages amqp_setup.channel.basic_consume( queue=queue_name, on_message_callback=callback, auto_ack=True) # an implicit loop waiting to receive messages; amqp_setup.channel.start_consuming()
def receiveOrderLog(): amqp_setup.check_setup() queue_name = 'Log' amqp_setup.channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) amqp_setup.channel.start_consuming()
def receiveNotifications(): amqp_setup.check_setup() #queue_name = "User" and "Admin" # set up a consumer and start to wait for coming messages amqp_setup.channel.basic_consume(queue="Admin", on_message_callback=callback, auto_ack=True) amqp_setup.channel.basic_consume(queue="User", on_message_callback=callback, auto_ack=True) amqp_setup.channel.start_consuming( ) # an implicit loop waiting to receive messages;
def checkTiming(data): order_id = str(data["order_id"]) print('\n-----Invoking Cart microservice-----') order_result = invoke_http( cart_URL + "/order/" + order_id, method='GET') code = order_result['code'] if order_result['code'] not in range(200, 300): return { 'code': 404, 'status': 'failed to get order' } delivered = order_result["data"]["rs_delivered_status"] if not delivered: email_details = { "email": data['email'], "guest_name": data['guest_name'], "quantity": order_result["data"]['rs_quantity'], "price": order_result["data"]['price'], "item_name" : data["item_name"], "date_time": order_result["data"]["order_datetime"], "order_id": order_result["data"]["order_id"], "booking_id": order_result["data"]["booking_id"], "type": "delay" } update_discount = invoke_http(booking_URL + "/" + str(order_result["data"]["booking_id"]), method='PUT') print('email_details:', email_details) message = json.dumps(email_details) amqp_setup.check_setup() print('\n\n-----Publishing the (delay notification) message with routing_key=delay.notification-----') amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="delay.notification", body=message, properties=pika.BasicProperties(delivery_mode=2)) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) # - reply from the invocation is not used; # continue even if this invocation fails print("\nOrder status ({:d}) published to the RabbitMQ Exchange:".format( code), message)
def processFulfillOrder(order): # 2. Get the order info # Invoke the order microservice print('\n-----Invoking order microservice-----') oid = order['oid'] order_result = invoke_http(order_URL + '/' + str(oid), method='PUT', json=order) print('order_result:', order_result) amqp_setup.check_setup() code = order_result["code"] if code not in range(200, 300): # Inform the error microservice print('\n\n-----Order microservice fails-----') message = 'order microservice fail' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="order.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) # 7. Return error return { "code": 500, "data": { "order_result": order_result }, "message": "Order retrival fail." } else: # help me chek if this is correct pid = order_result['data']['pid'] order_result = invoke_http(product_URL + '/fulfill/' + str(pid), method='PUT') message = 'Order microservice success' amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="order.info", body=message) pid = order_result['data']['pid'] product_result = invoke_http(product_URL + '/fulfill/' + str(pid), method='PUT') code = product_result["code"] if code not in range(200, 300): print('\n\n-----Product microservice fails-----') message = 'product microservice fail' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="product.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) # 7. Return error return { "code": 500, "data": { "product_result": product_result }, "message": "Stock update fail." } else: message = 'product microservice success' amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="product.info", body=message) return product_result
def processCheckOrderBiz(bid): print('\n\n-----Invoking product microservice-----') # 2. get pid based on bid product_result = invoke_http((product_URL + '/business/' + bid), method="GET") print("product:", product_result, '\n') # Check the shipping result; # if a failure, send it to the error microservice. code = product_result["code"] message = json.dumps(product_result) amqp_setup.check_setup() #amqp_setup.check_setup() if code not in range(200, 300): # Inform the error microservice #print('\n\n-----Invoking error microservice as shipping fails-----') #print('\n\n-----Publishing the (product error)') message = str(bid) + 'Product microservice fail' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="product.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) # - reply from the invocation is not used; # continue even if this invocation fails #print("\nProduct status ({:d}) published to the RabbitMQ Exchange:".format( # code), product_result) # 7. Return error return { "code": 500, "data": { "product_result": product_result }, "message": "Product retrival fail." } else: # 4. Record new order # record the activity log anyway #print('\n\n-----Invoking activity_log microservice-----') #print('\n\n-----Publishing the (product info) message with routing_key=product.info-----') message = str(bid) + 'Product microservice success' amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="product.info", body=message) # 3. Get the order info base on pid # Invoke the order microservice product_result_list = product_result['data']['products'] #print(product_result_list) print('\n-----Invoking order microservice-----') final_result_list = list() for product in product_result_list: pid = product['pid'] order_result = invoke_http(order_URL + '/product/' + str(pid)) #print('order_result:', order_result) # Check the order result; if a failure, print error. code = order_result["code"] if code not in range(200, 300): if code != 404: # Return error print( '\n\n-----Publishing the (order error) message with routing_key=order.error-----' ) # invoke_http(error_URL, method="POST", json=shipping_result) message = json.dumps(order_result) message = str(pid) + 'Order microservice fail' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="order.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) #print("\nOder status ({:d}) published to the RabbitMQ Exchange:".format( # code), order_result) else: # 4. Confirm success #print('\n\n-----Publishing the (order info) message-----') message = str(pid) + 'Order microservice success' amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="order.info", body=message) #print(order_result_list) # 5. Send PID order to product # Invoke the product microservice #print('\n\n-----Invoking customer microservice-----') #print(product) orders = order_result['data']['order'] #print(orders) for order in orders: #print(order) cid = order['cid'] customer_result = invoke_http( (customer_URL + '/location/' + str(cid)), method="GET") #print("customer:", customer_result, '\n') # Check the customer result; # if a failure, send it to the error microservice. code = customer_result["code"] if code not in range(200, 300): # Inform the error microservice #print('\n\n-----Publishing the (customer error)') #message = json.dumps(customer_result) message = str(cid) + 'Customer microservice fail' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="customer.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) print( "\nCustomer status ({:d}) published to the RabbitMQ Exchange:" .format(code), customer_result) # 7. Return error return { "code": 400, "data": { "order_result": order_result, "shipping_result": product_result, "customer_result": customer_result }, "message": "Customer retrival fail." } else: #print('\n\n-----Publishing the (order info) message-----') message = str(cid) + 'Customer microservice success' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="customer.info", body=message) final_result_list.append({ 'pname': product['pname'], 'imgname': product['imgname'], 'dStatus': order['dStatus'], 'oid': order['oid'], 'group_oid': order['group_oid'], 'datetime': order['datetime'], 'oStatus': order['oStatus'], 'quantity': order['quantity'], 'dStatus': order['dStatus'], 'address': customer_result['data']['address'] }) sorted_finallist = sorted(final_result_list, key=lambda k: k['group_oid']) # 7. Return order return {"code": 201, "data": {"required_info": final_result_list}}
def processPlaceOrder(order): # 2. Send the order info {cart items} # Invoke the order microservice print("\n-----Invoking order microservice-----") order_result = invoke_http("http://localhost:5002/order", method="POST", json=order) print("order_result:", order_result) # Check the order result; if a failure, send it to the error microservice code = order_result["code"] message = json.dumps(order_result) amqp_setup.check_setup() if code not in range(200, 300): # Inform the error microservice #print('\n\n-----Invoking error microservice as order fails-----') print( '\n\n-----Publishing the (order error) message with routing_key=order.error-----' ) # invoke_http(error_URL, method="POST", json=order_result) amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="order.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) # - reply from the invocation is not used; # continue even if this invocation fails print( "\nOrder status ({:d}) published to the RabbitMQ Exchange:".format( code), order_result) # 7. Return error return { "code": 500, "data": { "order_result": order_result }, "message": "Order creation failure sent for error handling." } # Notice that we are publishing to "Activity Log" only when there is no error in order creation. # In http version, we first invoked "Activity Log" and then checked for error. # Since the "Activity Log" binds to the queue using '#' => any routing_key would be matched # and a message sent to “Error” queue can be received by “Activity Log” too. else: # 4. Record new order # record the activity log anyway #print('\n\n-----Invoking activity_log microservice-----') print( '\n\n-----Publishing the (order info) message with routing_key=order.info-----' ) # invoke_http(activity_log_URL, method="POST", json=order_result) amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="order.info", body=message) print("\nOrder published to RabbitMQ Exchange.\n") # 4. Record new order # record the activity log anyway # print("\n\n-----Invoking activity_log microservice-----") # invoke_http(activity_log_URL, method="POST", json=order_result) # print("\nnOrder sent to activity log. \n") #- reply from the invocation is not used; # continue even if this invocation fails # Check the order result; if a failure, send it to the error microservice. # code = order_result["code"] # if code not in range(200,300): # # Inform the error microservice # print("\n\n-----Invoking error microservice as order fails-----") # invoke_http(error_URL, method="POST", json=order_result) # #- reply from the invocation is not used; # #continue even if this invocation fails # print("Order status ({:d}) sent to the error microservice:".format(code), order_result) # # 7. Return error # return { # "code" : 500, # "data" : {"order_result" : order_result}, # "message" : "Order creation failure sent for error handling" # } # else: print("\n\nOrder Successfully Placed\n") # 5. Send new order to shipping # Invoke the shipping record microservice # Check the shipping result; # if a failure, send it to the error microservice. # Inform the error microservice # 7. Return error # 7. Return created order, shipping record return { "code": 201, "data": { "order_result": order_result }, "message": "Order Succesfully Placed" }
def processPlaceOrder(order): # Invoke the order microservice #print('\n-----Invoking order microservice-----') #print(order) order_result = invoke_http(order_URL, method='POST', json=order) print('order_result:', order_result) amqp_setup.check_setup() # Check the order result; if a failure, send it to the error microservice. code = order_result["code"] if code not in range(200, 300): # Inform the error microservice #print('\n\n-----Invoking error microservice as order fails-----') print( '\n\n-----Publishing the (order error) message with order.error-----' ) # invoke_http(error_URL, method="POST", json=order_result) message = 'order microservice fail' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="order.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) # - reply from the invocation is not used; # continue even if this invocation fails print( "\nOrder status ({:d}) published to the RabbitMQ Exchange:".format( code), order_result) # 7. Return error return { "code": 500, "data": { "order_result": order_result }, "message": "Order creation failure." } # Notice that we are publishing to "Activity Log" only when there is no error in order creation. # In http version, we first invoked "Activity Log" and then checked for error. # Since the "Activity Log" binds to the queue using '#' => any routing_key would be matched # and a message sent to “Error” queue can be received by “Activity Log” too. else: # 4. Record new order # record the activity log anyway print( '\n\n-----Publishing the (order info) message with routing_key=order.info-----' ) message = 'Order microservice success' amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="order.info", body=message) #print('\n\n-----Invoking activity_log microservice-----') print('\n\n-----order placed-----') return order_result
# function to publish a sent message to the exchange @app.route('/message/send/<int:room_id>&<string:user_id>&<string:content>', methods=['POST']) def send_message(room_id, user_id, content): # NOTE: currently we are treating queue_name as the routing key (see amqp_setup), might want to modify later exchange_name = str(room_id) + "_roomchat" queue_name = user_id + "_queue" try: amqp_setup.send_message(exchange_name, queue_name, content) code = 200 message = "Message successfully sent." except Exception as e: code = 500 message = "An error occurred while sending the message. " + str(e) return jsonify({ "code": code, "data": { "exchange_name": exchange_name, "queue_name": queue_name, "content": content }, "message": message }), code if __name__ == "__main__": app.run(port=5003, debug=True) amqp_setup.check_setup() # to make sure connection and channel are running
def processCheckOrderCust(cid): # 2. Get the order info # Invoke the order microservice #print('\n-----Invoking order microservice-----') order_result = invoke_http(order_URL + '/customer/' + str(cid), method='GET') #print('order_result:', order_result) code = order_result["code"] message = json.dumps(order_result) amqp_setup.check_setup() final_result_list = list() if code not in range(200, 300): # Inform the error microservice #print('\n\n-----Order microservice fails-----') message = str(cid) + 'Order microservice fails' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="order.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) #print("\nOrder status ({:d}) published to the RabbitMQ Exchange:".format( # code), order_result) # 7. Return error return { "code": 500, "data": { "order_result": order_result }, "message": "Order retrival fail." } else: #print('\n\n-----Publishing the (product info) message with routing_key=order.info-----') message = str(cid) + 'Order microservice success' amqp_setup.channel.basic_publish(exchange=amqp_setup.exchangename, routing_key="order.info", body=message) order_result_list = order_result['data']['order'] #print(order_result_list) # Invoke the shipping record microservice #print('\n\n-----Invoking product microservice-----') for order in order_result_list: print(order) pid = order['pid'] product_result = invoke_http(product_URL + '/' + str(pid), method="GET") #print("product:", product_result, '\n') # Check the shipping result; # if a failure, send it to the error microservice. code = product_result["code"] if code not in range(200, 300): # Inform the error microservice #print('\n\n-----Invoking error microservice as shipping fails-----') #print('\n\n-----product error') message = str(pid) + 'Product microservice fail' message = json.dumps(product_result) amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="product.error", body=message, properties=pika.BasicProperties(delivery_mode=2)) #print("\nProduct status ({:d}) published to the RabbitMQ Exchange:".format( # code), product_result) else: # 4. confirm success #print('\n\n-----Publishing the (product info) message-----') message = str(pid) + 'Product microservice success' amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="product.info", body=message) final_result_list.append({ 'pname': product_result['data']['pname'], 'oid': order['oid'], 'imgname': product_result['data']['imgname'], 'dStatus': order['dStatus'], 'datetime': order['datetime'], 'oStatus': order['oStatus'], 'quantity': order['quantity'], 'dStatus': order['dStatus'] }) #print(final_result_list) # 7. Return created order, shipping record return {"code": 201, "data": {"required_info": final_result_list}}
def processOrderRS(booking_details): print('\n-----Putting guest details-----') booking_id = booking_details['booking_id'] email = booking_details['email'] guest_name = booking_details['guest_name'] room_service_orders = booking_details['room_service_orders'] for order in room_service_orders: rs_order = { "item_id": order['item_id'], "rs_quantity": order['rs_quantity'], "price": order['item_price'] } print('\n-----Invoking order microservice-----') add_rs_result = invoke_http(cart_URL + "/add_rs/" + booking_id, method='POST', json=rs_order) if add_rs_result['code'] not in range(200, 300): return {'code': 404, 'status': 'failed to add room service'} # Invoke the order microservice print('add_rs_result:', add_rs_result) # Check the order result; if a failure, send it to the error microservice. code = add_rs_result["code"] email_details = { "email": email, "guest_name": guest_name, "quantity": order['rs_quantity'], "price": order['item_price'], "item_name": order["item_name"], "date_time": add_rs_result["data"]["order_datetime"], "order_id": add_rs_result["data"]["order_id"], "booking_id": add_rs_result["data"]["booking_id"], "type": "room_service" } print('add_rs_result:', email_details) message = json.dumps(email_details) amqp_setup.check_setup() print( '\n\n-----Publishing the (order notification) message with routing_key=order.notification-----' ) amqp_setup.channel.basic_publish( exchange=amqp_setup.exchangename, routing_key="order.notification", body=message, properties=pika.BasicProperties(delivery_mode=2)) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) # - reply from the invocation is not used; # continue even if this invocation fails print( "\nOrder status ({:d}) published to the RabbitMQ Exchange:".format( code), message) delay_content = { "order_id": add_rs_result["data"]["order_id"], "email": email, "guest_name": guest_name, "item_name": order["item_name"], } delay_message = json.dumps(delay_content) order_time = [ int(s) for s in order['waiting_time'].split() if s.isdigit() ] waiting_time = order_time[0] * 60000 print("waiting_time:", str(waiting_time)) # waiting_time = 5 * 60000 # amqp_setup.check_setup() print( '\n\n-----Publishing the (error service) message with routing_key=order.notification-----' ) amqp_setup.channel.basic_publish( exchange=amqp_setup.delay_exchangename, routing_key="order.error_service", body=delay_message, properties=pika.BasicProperties(delivery_mode=2, headers={"x-delay": waiting_time})) # make message persistent within the matching queues until it is received by some receiver # (the matching queues have to exist and be durable and bound to the exchange) # - reply from the invocation is not used; # continue even if this invocation fails print( "\nOrder status ({:d}) published to the RabbitMQ Exchange:".format( code), delay_message) return { "code": 201, "booking_result": { "email": email, "guest_name": guest_name, "status": "success" } }