def __init__(self, queue): super(AgentReceiver, self).__init__() self.queue = queue self.driver = NginxDriver(logger=logger) self.node_id = node_id self.connect()
def __init__(self,queue): super(AgentReceiver,self).__init__() self.queue = queue self.driver = NginxDriver(logger=logger) self.node_id = node_id self.connect()
class AgentReceiver(threading.Thread): def __init__(self, queue): super(AgentReceiver, self).__init__() self.queue = queue self.driver = NginxDriver(logger=logger) self.node_id = node_id self.connect() def connect(self): parameters = pika.ConnectionParameters( virtual_host=settings.virtual_host, credentials=pika.PlainCredentials(settings.username, settings.password), frame_max=settings.frame_max_size, host=settings.rabbitmq_server) select_connection.POLLER_TYPE = 'epoll' self.connection_agent = select_connection.SelectConnection( parameters=parameters, on_open_callback=self.on_connected) def run(self): self.connection_agent.ioloop.start() def on_connected(self, connection): connection.channel(self.on_channel_open) def on_channel_open(self, channel): self.channel_agent = channel self.channel_agent.exchange_declare(exchange='loadbalance.agent', type='fanout', durable=True, callback=self.on_exchange_declared) def on_exchange_declared(self, exchange_): self.channel_agent.queue_declare(durable=False, exclusive=True, callback=self.on_queue_declared) def on_queue_declared(self, result): self.queue_name = result.method.queue self.channel_agent.queue_bind(exchange='loadbalance.agent', queue=self.queue_name, callback=self.on_queue_bind) def on_queue_bind(self, frame): self.channel_agent.basic_consume(self.handle_delivery, queue=self.queue_name) def handle_delivery(self, ch, method, header, body): body = simplejson.loads(body) task_node_id = body['task_node_id'] # task's node id message_type = body['message_type'] # message type command = body['command'] # command msg_id = body['message_id'] #logger.debug("agent got:%s" % msg_id) # task message if message_type == "task": if task_node_id == self.node_id: body['message_type'] = "work_report" if command == "addhost": ret, why = self.driver.add_host(body) # remove server content from original message body['content'].pop('server') if ret: body['failed'] = False else: body['failed'] = True body['why'] = why if command == "delhost": ret, why = self.driver.delete_host(body) if ret: body['failed'] = False else: body['failed'] = True body['why'] = why if command == 'restart': ret, why = self.driver.restart_node() if ret: body['failed'] = False else: body['failed'] = True body['why'] = why # broadcast message if message_type == "cast": if command == "host_amount": pass ch.basic_ack(delivery_tag=method.delivery_tag) body['return_node_id'] = self.node_id self.queue.put_nowait(simplejson.dumps(body))
class AgentReceiver(threading.Thread): def __init__(self,queue): super(AgentReceiver,self).__init__() self.queue = queue self.driver = NginxDriver(logger=logger) self.node_id = node_id self.connect() def connect(self): parameters = pika.ConnectionParameters(virtual_host=settings.virtual_host, credentials=pika.PlainCredentials(settings.username,settings.password), frame_max=settings.frame_max_size, host=settings.rabbitmq_server) select_connection.POLLER_TYPE = 'epoll' self.connection_agent = select_connection.SelectConnection(parameters=parameters, on_open_callback=self.on_connected) def run(self): self.connection_agent.ioloop.start() def on_connected(self,connection): connection.channel(self.on_channel_open) def on_channel_open(self,channel): self.channel_agent = channel self.channel_agent.exchange_declare(exchange='loadbalance.agent',type='fanout',durable=True, callback=self.on_exchange_declared) def on_exchange_declared(self,exchange_): self.channel_agent.queue_declare(durable=False, exclusive=True, callback=self.on_queue_declared) def on_queue_declared(self,result): self.queue_name = result.method.queue self.channel_agent.queue_bind(exchange='loadbalance.agent',queue=self.queue_name, callback=self.on_queue_bind) def on_queue_bind(self,frame): self.channel_agent.basic_consume(self.handle_delivery, queue=self.queue_name) def handle_delivery(self, ch, method, header, body): body = simplejson.loads(body) task_node_id = body['task_node_id'] # task's node id message_type = body['message_type'] # message type command = body['command'] # command msg_id = body['message_id'] #logger.debug("agent got:%s" % msg_id) # task message if message_type == "task": if task_node_id == self.node_id: body['message_type'] = "work_report" if command == "addhost": ret,why = self.driver.add_host(body) # remove server content from original message body['content'].pop('server') if ret: body['failed'] = False else: body['failed'] = True body['why'] = why if command == "delhost": ret,why = self.driver.delete_host(body) if ret: body['failed'] = False else: body['failed'] = True body['why'] = why if command == 'restart': ret,why = self.driver.restart_node() if ret: body['failed'] = False else: body['failed'] = True body['why'] = why # broadcast message if message_type == "cast": if command == "host_amount": pass ch.basic_ack(delivery_tag = method.delivery_tag) body['return_node_id'] = self.node_id self.queue.put_nowait(simplejson.dumps(body))