def _send(self, request, status, **details): """ Send a status update. :param request: The received (json) request. :type request: Document :param status: The status to send ('accepted'|'rejected') :type status: str """ address = request.replyto if not address: return try: producer = Producer(self.url) producer.authenticator = self.authenticator producer.open() try: producer.send( address, sn=request.sn, data=request.data, status=status, timestamp=timestamp(), **details) finally: producer.close() except Exception: log.exception('send (%s), failed', status)
def _producer(plugin): """ Get a configured producer. :param plugin: A plugin. :type plugin: gofer.agent.plugin.Plugin :return: A producer. :rtype: Producer """ producer = Producer(plugin.url) producer.authenticator = plugin.authenticator return producer
def _send(self, reply=None, queue=None): """ Send the request using the specified policy object and generated serial number. :param reply: The AMQP reply address. :type reply: str :param queue: The reply queue for synchronous calls. :type queue: Queue """ with Producer(self._policy.url) as producer: producer.authenticator = self._policy.authenticator producer.send( self._policy.address, self._policy.ttl, # body sn=self.sn, replyto=reply, request=self._request, data=self._policy.data) log.debug('sent (%s): %s', self._policy.address, self._request) if queue is None: # no reply expected return self._sn with Reader(queue, self._policy.url) as reader: reader.authenticator = self._policy.authenticator policy = self._policy return policy.get_reply(self.sn, reader)
def producer_consumer(self, address): print 'using producer/consumer' class TestCon(Consumer): def __init__(self, url): queue = Queue(address.queue) Consumer.__init__(self, queue, url=url) self.received = 0 def dispatch(self, document): self.received += 1 print '%d/%d - %s' % (self.received, N, document) if self.received == N: self.stop() c = TestCon(self.url) c.start() with Producer(url=self.url) as p: for x in range(0, N): print '#%d - sent: %s' % (x, address) p.send(str(address)) c.join() print 'end'
def producer_consumer(self, address): print('using producer/consumer') class TestCon(Consumer): def __init__(self, url): queue = Queue(address.queue) Consumer.__init__(self, queue, url=url) self.received = 0 def dispatch(self, document): self.received += 1 print('{}/{} - {}'.format(self.received, N, document)) if self.received == N: self.stop() c = TestCon(self.url) c.start() with Producer(url=self.url) as p: for x in range(0, N): print('#{} - sent: {}'.format(x, address)) p.send(str(address)) c.join() print('end')
def send(self): delay = int(HEARTBEAT) address = 'amq.topic/heartbeat' if plugin.uuid: with Producer(plugin.url) as p: body = dict(uuid=plugin.uuid, next=delay) p.send(address, ttl=delay, heartbeat=body) return plugin.uuid
def producer_reader(self, address): print 'using producer/reader' with Producer(url=self.url) as p: for x in range(0, N): print '#%d - sent: %s' % (x, address) p.send(str(address)) received = 0 queue = Queue(address.queue) with Reader(queue, url=self.url) as r: while received < N: m, d = r.next() if m is None: break m.ack() print '#%d - received: %s' % (received, d) received += 1 print 'end'
def producer_reader(self, address): print('using producer/reader') with Producer(url=self.url) as p: for x in range(0, N): print('#{} - sent: {}'.format(x, address)) p.send(str(address)) received = 0 queue = Queue(address.queue) with Reader(queue, url=self.url) as r: while received < N: m, d = r.next() if m is None: break m.ack() print('#{} - received: {}'.format(received, d)) received += 1 print('end')
def _send(self, reply=None, queue=None): """ Send the request using the specified policy object and generated serial number. :param reply: The AMQP reply address. :type reply: str :param queue: The reply queue for synchronous calls. :type queue: Queue """ producer = Producer(self._policy.url) producer.authenticator = self._policy.authenticator producer.open() try: producer.send( self._policy.address, self._policy.ttl, # body sn=self.sn, replyto=reply, request=self._request, secret=self._policy.secret, pam=self._policy.pam, data=self._policy.data) finally: producer.close() log.debug('sent (%s): %s', self._policy.address, self._request) if queue is None: # no reply expected return self._sn reader = Reader(queue, self._policy.url) reader.authenticator = self._policy.authenticator reader.open() try: policy = self._policy return policy.get_reply(self.sn, reader) finally: reader.close()