async def callback_wrapper(msg): # try executing the callback and log if exception occurs try: # decode message msg = Message.decode_raw(msg.data, message_schema) # temporarily copy shared storage, so callback cannot perform invalid changes shared_storage = self.shared_storage.copy() # execute callback if len(signature(callback_function).parameters) == 5: # include kubernetes_client await callback_function(msg, self.nats_client, shared_storage, self._logger, self.kubernetes_client) else: await callback_function(msg, self.nats_client, shared_storage, self._logger) # check whether the shared storage is still valid and set it if that is the case if not validate_json(shared_storage, self._schema): raise ValueError( "Invalid change in shared storage") self.shared_storage = shared_storage # buffer the current shared storage in redis self.redis_client.set_shared_storage( self.shared_storage) except Exception as e: await self._logger.error(traceback.format_exc())
async def callback_wrapper(msg): # try executing the callback and log if exception occurs try: # decode message and copy raw message to preserve the response channel name raw_message = msg msg = Message.decode_raw(msg.data, message_schema) # temporarily copy shared storage, so callback cannot perform invalid changes shared_storage = self.shared_storage.copy() # execute callback response = await callback_function( msg, self.nats_client, shared_storage, self._logger) # check whether the shared storage is still valid and set it if that is the case if not validate_json(shared_storage, self._schema): raise ValueError( "Invalid change in shared storage") self.shared_storage = shared_storage # buffer the current shared storage in redis self.redis_client.set_shared_storage( self.shared_storage) # send the response via NATS await self.nats_client.send_message( raw_message.reply, response) except Exception as e: await self._logger.error(traceback.format_exc())
async def callback(msg): print("Got message") print(msg) self.assertEquals(Message.decode_raw(msg.data, MessageSchemas.TEST_MESSAGE).encode_json(), { "sender_ID": "User", "time_sent": "2020-07-06", "data": { "testData": "This is a test" } } ) print("Is equal") raise ValueError("TEST")
async def callback(msg): print("Got message") loop = self._asyncioTestLoop raw_message = msg msg = Message.decode_raw(msg.data, MessageSchemas.TEST_MESSAGE) print(msg) self.assertEqual(msg.encode_json(), { "sender_ID": "User", "origin_ID": "User", "message_type": "test_message", "time_sent": "2020-07-06", "data": { "testData": "This is a test" } }) await nats.send_message(raw_message.reply, msg)
async def request_message(self, topic, message, schema, timeout=1): """ Sends a request to a channel and returns the response. Args: topic (string): channel name to publish to message (object): message to send schema (dict): Schema of the expected response timeout (int, optional): Timeout that limits how long to wait for a response. Defaults to 1. Returns: object: returns the response. """ message = message.encode_raw() result = await self.nc.request(topic, message, timeout) return Message.decode_raw(result.data, schema)