I need comments on the below code:
Thread.new {EM.run do
IpamAgent::Amqp.run
end}
module IpamAgent
class Amqp
class << self
def run
begin
$connection = AMQP.connect(RMQ_CONFIGURATIONS)
$connection.on_tcp_connection_loss do |conn, settings|
Rails.logger.info "<<<<<<<<<<<<<<<< [network failure] Trying to reconnect...>>>>>>>>>>>>>>>>>>>>>>>>"
conn.reconnect(false, 2)
end
Rails.logger.info "<<<<<<<<<<<<<<<<<<<<<<<<AMQP listening>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
worker = IpamAgent::MessageHandler.new
worker.start
Rails.logger.info "<<<<<<<<<<<<<<<<<<<<<<<<Message handler started>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
rescue Exception => e
Rails.logger.info "<<<<<<<<<<<<<<<<<<<<<<<<Message handler Exception>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
Rails.logger.info "[error] Could not handle event of type #{e.inspect}"
Rails.logger.info "<<<<<<<<<<<<<<<<<<<<<<<<Message handler Exception>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
end
end
end
end
end
module IpamAgent
class MessageHandler
attr_accessor :ns_exchange, :sc_exchange, :ns_queue, :sc_queue, :service_location, :service_version, :service_name, :message_sequence_id, :presto_exchange, :presto_queue
def initialize
# Profile the code
@ns_exchange = CONFIGURATIONS["ns_exchange"]
@sc_exchange = CONFIGURATIONS["sc_exchange"]
@presto_exchange = CONFIGURATIONS["presto_exchange"]
@ns_queue = CONFIGURATIONS["ns_queue"]
@sc_queue = CONFIGURATIONS["sc_queue"]
@presto_queue = CONFIGURATIONS["presto_queue"]
end
# Create the channels, exchanges and queues
def start
ch1 = AMQP::Channel.new($connection)
ch2 = AMQP::Channel.new($connection)
ch3 = AMQP::Channel.new($connection)
@ns_x = ch1.direct(ns_exchange, :durable => true)
@ns_queue = ch1.queue(ns_queue, :auto_delete => false)
@ns_queue.bind(@ns_x, :routing_key => @ns_queue.name).subscribe(:ack => true, &method(:handle_ns_message))
@sc_x = ch2.topic(sc_exchange, :durable => true)
@sc_queue = ch2.queue(sc_queue, :auto_delete => false)
@sc_queue.bind(@sc_x, :routing_key => "#").subscribe(:ack => true, &method(:handle_sc_message))
@presto_x = ch3.direct(presto_exchange, :durable => true)
@presto_queue = ch3.queue(presto_queue, :auto_delete => false)
@presto_queue.bind(@presto_x, :routing_key => @presto_queue.name).subscribe(:ack => true, &method(:handle_presto_message))
end
# Handle the messages from Network service component
def handle_ns_message(headers, payload)
message_headers = JSON.parse(headers.to_json)["headers"]
payload = eval(payload)
headers.ack
Rails.logger.info ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>MESSAGE FROM NS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
Rails.logger.info message_headers
Rails.logger.info payload
Rails.logger.info ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>MESSAGE FROM NS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
tenant_detail = IpamAgent::TenantDetail.where(service_instance: payload["orgId"]).first
if(payload && payload.keys.include?(:responseCode))
new_tenant_detail = IpamAgent::TenantDetail.create(message: ({"header" => message_headers, "payload" => payload}), status: "waiting_for_sc", service_instance: payload["orgId"])
if tenant_detail && tenant_detail.service_group
publish_sgid_to_presto(tenant_detail)
else
get_sgid_from_sc(new_tenant_detail)
end
else
Rails.logger.info("Payload: #{payload}, routing key is #{message_headers}")
end
end
# Retrieve the Service Group ID from Service controller
def get_sgid_from_sc(tenant_detail)
message = tenant_detail.get_sc_message
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>PUBLISHING TO SC<<<<<<<<<<<<<<<<<<<<<<<")
Rails.logger.info(message)
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>PUBLISHING TO SC<<<<<<<<<<<<<<<<<<<<<<<")
@sc_x.publish(message.last, :routing_key => @sc_queue.name, :headers => message.first, :mandatory => true)
end
# Handle the messages from Service controller
def handle_sc_message(headers, payload)
message_headers = JSON.parse(headers.to_json)
headers.ack
payload = eval(payload)
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>MESSAGE FROM SC<<<<<<<<<<<<<<<<<<<<<<<")
Rails.logger.info(message_headers)
Rails.logger.info(payload)
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>MESSAGE FROM SC<<<<<<<<<<<<<<<<<<<<<<<")
if(payload && payload["serviceInstanceGroupId"])
tenant_detail = IpamAgent::TenantDetail.find_or_save(payload)
publish_sgid_to_presto(tenant_detail)
end
end
# Shovel the NS request to PMP with Service Group ID
def publish_sgid_to_presto(tenant_detail)
tenant_details = TenantDetail.where(service_instance: tenant_detail.service_instance, status: "waiting_for_sc")
tenant_details.each do |sc|
sc.update(status: "success")
message = sc.get_pmp_message
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>PUBLISHING TO PRESTO<<<<<<<<<<<<<<<<<<<<<<<")
Rails.logger.info(message.first)
Rails.logger.info(message.last)
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>PUBLISHING TO PRESTO<<<<<<<<<<<<<<<<<<<<<<<")
@presto_x.publish(message.last, :routing_key => @presto_queue.name, :headers => message.first, :mandatory => true)
end
end
# Receive the message from PMP presto and publsih it to Network Service
def handle_presto_message(headers, payload)
message_headers = JSON.parse(headers.to_json)
payload = eval(payload)
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>MESSAGE FROM PRESTO<<<<<<<<<<<<<<<<<<<<<<<")
Rails.logger.info(headers.to_json)
Rails.logger.info(payload)
Rails.logger.info(">>>>>>>>>>>>>>>>>>>>MESSAGE FROM PRESTO<<<<<<<<<<<<<<<<<<<<<<<")
headers.ack
@ns_x.publish(payload, :routing_key =>@ns_queue.name, :headers => headers, :mandatory => true) if (message_headers && payload)
end
end
end