10 from vstf.common import message
11 from vstf.common import excepts
12 from vstf.rpc_frame_work import constant
14 LOG = logging.getLogger(__name__)
17 class RpcProxy(object):
18 def __init__(self, host,
22 """create a connection to rabbitmq,direct call and fan call supported.
25 # try to create connection of rabbitmq
27 self._connection = None
28 self._queue = str(uuid.uuid4())
29 self._consume_tag = None
35 self.url = 'amqp://' + self.user + ':' + self.passwd + '@' + self.srv + ':' + self.port + '/%2F'
37 self.connect(host, self.setup_vstf_producer)
38 except Exception as e:
39 LOG.error("create connection failed. e:%(e)s", {'e': e})
45 def connect(self, host, ok_callback):
46 """Create a Blocking connection to the rabbitmq-server
48 :param str host: the rabbitmq-server's host
49 :param obj ok_callback: if connect success than do this function
52 LOG.info("Connect to the server %s", host)
53 self._connection = pika.BlockingConnection(pika.URLParameters(self.url))
57 def setup_vstf_producer(self):
59 self.create_exchange(constant.exchange_d, constant.DIRECT)
63 def open_channel(self):
64 self._channel = self._connection.channel()
66 self._channel.confirm_delivery()
68 def create_exchange(self, name, type):
69 LOG.info("Create %s exchange: %s", type, name)
70 self._channel.exchange_declare(exchange=name, type=type)
72 def bind_queues(self):
73 LOG.info("Declare queue %s and bind it to exchange %s",
74 self._queue, constant.exchange_d)
75 self._channel.queue_declare(queue=self._queue, exclusive=True)
76 self._channel.queue_bind(exchange=constant.exchange_d, queue=self._queue)
78 def start_consumer(self):
79 LOG.info("Start response consumer")
80 self._consume_tag = self._channel.basic_consume(self.on_response,
84 def stop_consuming(self):
85 """Tell RabbitMQ that you would like to stop consuming by sending the
86 Basic.Cancel RPC command.
90 LOG.info('Sending a Basic.Cancel RPC command to RabbitMQ')
91 self._channel.basic_cancel(self._consume_tag)
95 def close_channel(self):
96 """Call to close the channel with RabbitMQ cleanly by issuing the
97 Channel.Close RPC command.
100 LOG.info('Closing the channel')
101 self._channel.close()
103 def close_connection(self):
104 """This method closes the connection to RabbitMQ."""
105 LOG.info('Closing connection')
106 self._connection.close()
109 self.stop_consuming()
110 self.close_connection()
112 def on_response(self, ch, method, props, body):
113 """this func reciver the msg"""
115 if self.corr_id == props.correlation_id:
116 self.response = json.loads(body)
117 LOG.debug("Proxy producer reciver the msg: head:%(h)s, body:%(b)s",
118 {'h': self.response.get('head'), 'b': self.response.get('body')})
120 LOG.warn("Proxy producer Drop the msg "
121 "because of the wrong correlation id, %s\n" % body)
123 def publish(self, target, corrid, body):
124 properties = pika.BasicProperties(reply_to=self._queue,
125 correlation_id=corrid)
126 LOG.debug("start to publish message to the exchange=%s, target=%s, msg=%s"
127 , constant.exchange_d, target, body)
128 return self._channel.basic_publish(exchange=constant.exchange_d,
131 properties=properties,
132 body=message.encode(body))
134 def call(self, msg, target='agent', timeout=constant.TIMEOUT):
135 """send msg to agent by id, this func will wait ack until timeout
136 :msg the msg to be sent
138 :timeout timeout of waiting response
142 queue = constant.queue_common + target
143 # the msg request and respone must be match by corr_id
144 self.corr_id = str(uuid.uuid4())
146 msg = message.add_context(msg, corrid=self.corr_id)
148 # send msg to the queue
150 ret = self.publish(queue, self.corr_id, msg)
151 except Exception as e:
152 LOG.error(message.dumpstrace())
153 raise excepts.ChannelDie
155 # if delivery msg failed. return error
156 # clean the msg in the queue
158 LOG.error("productor message delivery failed.")
159 return "Message can not be deliveryed, please check the connection of agent."
162 t_begin = time.time()
163 while self.response is None:
164 self._connection.process_data_events()
165 count = time.time() - t_begin
167 LOG.error("Command timeout!")
168 # flush the msg of the queue
169 self._channel.queue_purge(queue=queue)
170 # self.channel.basic_cancel()
173 msg_body = message.get_body(message.decode(self.response))
175 # deal with exceptions
177 and isinstance(msg_body, dict) \
178 and msg_body.has_key('exception'):
179 ename = str(msg_body['exception'].get('name'))
180 if hasattr(exceptions, ename):
181 e = getattr(exceptions, ename)()
183 class CallError(Exception):
187 e.message = str(msg_body['exception'].get('message'))
188 e.args = msg_body['exception'].get('args')
194 class Server(object):
195 def __init__(self, host=None,
199 super(Server, self).__init__()
200 # Default use salt's master ip as rabbit rpc server ip
202 raise Exception("Can not create rpc proxy because of the None rabbitmq server address.")
209 self.proxy = RpcProxy(host=host,
213 except Exception as e:
216 def call(self, msg, msg_id, timeout=constant.TIMEOUT):
217 """when you add a server listen to the rabbit
218 you must appoint which queue to be listen.
219 :@queue the queue name.
224 ret = self.proxy.call(msg, target=msg_id, timeout=timeout)
225 except excepts.ChannelDie:
226 # this may be the proxy die, try to reconnect to the rabbit
228 self.proxy = RpcProxy(host=self.host,
232 if self.proxy is None:
233 raise excepts.UnsolvableExit
237 # if retry happened except, throw to uplay
238 ret = self.proxy.call(msg, target=msg_id, timeout=timeout)
243 """when you want to send msg to all agent and no reply, use this func"""
244 LOG.warn("cast not support now.")
246 def make_msg(self, method, **kargs):
247 return {'method': method,