def test_message(self): data = {'1': 2} msg = Message(**data) self.assertEquals(msg.serialize(), json.dumps(data)) msg = Message.load_from_string(json.dumps(data)) self.assertEquals(msg.serialize(), json.dumps(data))
def _handle_recv_back(self, msg): # do the message and send the result if self.debug: #logger.debug('Message received from the broker') target = timed()(self._handle_commands) else: target = self._handle_commands duration = -1 try: res = target(Message.load_from_string(msg[0])) if self.debug: duration, res = res res = json.dumps(res) # we're working with strings if isinstance(res, unicode): res = res.encode('utf8') except Exception, e: exc_type, exc_value, exc_traceback = sys.exc_info() exc = traceback.format_tb(exc_traceback) exc.insert(0, str(e)) res = {'error': {'agent_id': self.pid, 'error': '\n'.join(exc)}} logger.error(res)
def _handle_recv_back(self, msg): # do the message and send the result if self.debug: logger.debug('Message received') target = timed()(self.target) else: target = self.target duration = -1 # results are sent with a PID:OK: or a PID:ERROR prefix try: with self.timer.run_message(): res = target(Message.load_from_string(msg[0])) # did we timout ? if self.timer.timed_out: # let's dump the last for line in self.timer.last_dump: logger.error(line) if self.debug: duration, res = res # we're working with strings if isinstance(res, unicode): res = res.encode('utf8') res = '%d:OK:%s' % (self.pid, res) except Exception, e: exc_type, exc_value, exc_traceback = sys.exc_info() exc = traceback.format_tb(exc_traceback) exc.insert(0, str(e)) res = '%d:ERROR:%s' % (self.pid, '\n'.join(exc)) logger.error(res)
def _handle_recv_back(self, msg): # do the message and send the result if self.debug: target = timed()(self._handle_commands) else: target = self._handle_commands duration = -1 broker_id = msg[2] if len(msg) == 7: client_id = msg[4] else: client_id = None data = msg[-1] try: res = target(Message.load_from_string(data)) if self.debug: duration, res = res res['hostname'] = get_hostname() res = json.dumps(res) # we're working with strings if isinstance(res, unicode): res = res.encode('utf8') except Exception, e: exc_type, exc_value, exc_traceback = sys.exc_info() exc = traceback.format_tb(exc_traceback) exc.insert(0, str(e)) res = {'error': {'agent_id': self.pid, 'error': '\n'.join(exc)}} logger.error(res)