def test_message_auto_decode_cache(self): body = 'Hello World', message = Message(body=body, channel=None) self.assertEqual(body, message.body) message._body = 'invalidate' self.assertEqual(body, message.body)
def test_message_auto_decode_cache(self): message = Message(body=self.message, channel=None) self.assertEqual(self.message, message.body) message._body = 'invalidate' self.assertEqual(self.message, message.body)
def handleMessage(self): global tokens, keys, clients print("INCOMING **************** ") if (self.data.startswith("start process:")): streamKey = self.data.split("start process:") if (len(streamKey) != 2): self.sendMessage("No streamKey provided") return else: streamKey = streamKey[1] self.sendMessage("Starting Video Processing") self.state = "active" tokens[streamKey] = self self.sendMessage("...") message = Message.create(mainChannel_out, streamKey) message.publish("gst-launcher") self.sendMessage("!") else: data = self.data.split(":") if (len(data) != 2): self.sendMessage("No streamKey provided") return streamKey = data[1] task = data[0] print("trying to create Task") try: msg = {} msg['streamKey'] = streamKey msg['task'] = task msg = json.dumps(msg) message = Message.create(mainChannel_out, msg) ## channel, queue-name message.publish(streamKey) except Exception as e: self.sendMessage("Error: " + str(e))
def on_amqpstorm_message_received(amqpstormMessage: amqpstorm.Message): # Create custom message object and call cbf of subscription on_message_received( Message.from_amqpstorm_message(amqpstormMessage)) # Acknowledge the message amqpstormMessage.ack()
def __call__(self, message): """Process the RPC Payload. :param Message message: :return: """ try: processed_message = self.handle_message(message) except Exception as e: app_logger.error('Something went wrong: {0}'.format(e)) properties = {'correlation_id': message.correlation_id} error_message = "Error Type: {0}, with message: {1}".format( e.__class__.__name__, e.message) message_data = json.loads(message.body) processed_message = response_message(message_data["provenance"], status="error", status_messsage=error_message) response = Message.create( message.channel, str( json.dumps(processed_message, indent=4, separators=(',', ': '))), properties) response.publish(message.reply_to) message.reject(requeue=False) else: properties = {'correlation_id': message.correlation_id} response = Message.create(message.channel, processed_message, properties) response.publish(message.reply_to) message.ack()
def test_message_json(self): body = '{"key": "value"}' message = Message(body=body, channel=None) result = message.json() self.assertIsInstance(result, dict) self.assertEqual(result['key'], 'value')
def test_to_dict(self): body = b'Hello World' message = Message(body=body, properties={'key': 'value'}, method={'key': 'value'}, channel=None) result = message.to_dict() self.assertIsInstance(result, dict) self.assertEqual(result['body'], body)
def test_to_tuple(self): body = b'Hello World' message = Message(body=body, properties={'key': 'value'}, method={'key': 'value'}, channel=None) body, channel, method, properties = message.to_tuple() self.assertEqual(body, body) self.assertIsInstance(method, dict) self.assertIsInstance(properties, dict) self.assertIsNone(channel)
def test_message_to_tuple(self): body = self.message message = Message(body=body, properties={'key': 'value'}, method={'key': 'value'}, channel=None) body, channel, method, properties = message.to_tuple() self.assertEqual(body, body) self.assertIsInstance(method, dict) self.assertIsInstance(properties, dict) self.assertIsNone(channel)
def __call__(self, message: amqpstorm.Message): """Process the Payload. :param Message message: :return: """ try: message.ack() message = json.loads(message.body) self.message_handler.handle(message) except json.JSONDecodeError: self.logger.error("Expected JSON Message") except Exception as error: self.logger.error(error.__repr__())
def test_message_to_dict(self): body = self.message properties = {'key': 'value'} method = {b'alternative': 'value'} message = Message(body=body, properties=properties, method=method, channel=None) result = message.to_dict() self.assertIsInstance(result, dict) self.assertEqual(result['body'], body) self.assertEqual(result['properties'], properties) self.assertEqual(result['method'], method)
def test_message_content_encoding_custom_value(self): content_encoding = 'gzip' message = Message.create(None, '') message.content_encoding = content_encoding self.assertEqual(content_encoding, message.content_encoding)
def test_message_reply_to_custom_value(self): reply_to = str(uuid.uuid4()) message = Message.create(None, '') message.reply_to = reply_to self.assertEqual(reply_to, message.reply_to)
def test_message_correlation_id_custom_value(self): correlation_id = str(uuid.uuid4()) message = Message.create(None, '') message.correlation_id = correlation_id self.assertEqual(correlation_id, message.correlation_id)
def test_functional_publish_and_change_app_id(self): self.channel.confirm_deliveries() self.channel.queue.declare(self.queue_name) message = Message.create(self.channel, body=self.message) message.app_id = 'travis-ci' message.publish(self.queue_name) message = self.channel.basic.get(self.queue_name, no_ack=True) # Check original app_id self.assertEqual(message.app_id, 'travis-ci') # Assign Property app_id app_id = 'travis-ci-2'.encode('utf-8') message.app_id = app_id # Check that it was set correctly. self.assertEqual(message.properties['app_id'], app_id) # Get Property Correlation Id correlation_id = message.correlation_id # Publish Message message.publish(routing_key=self.queue_name) # Sleep for 0.01s to make sure RabbitMQ has time to catch up. time.sleep(0.01) payload = self.channel.basic.get(self.queue_name, no_ack=True) self.assertEqual(payload.app_id, app_id.decode('utf-8')) self.assertEqual(payload.correlation_id, correlation_id) self.assertIsInstance(payload.properties['app_id'], str) self.assertIsInstance(payload.properties['correlation_id'], str)
def publish_message(channel, body, queue, expiration="600"): # Create the message with a expiration (time to live). message = Message.create(channel, body, properties={"expiration": expiration}) # Publish the message to a queue. message.publish(queue)
def _publish_mappings(self, message: Message, accessionID: str, datasetID: str) -> None: """Publish message with dataset to accession ID mapping.""" properties = { "content_type": "application/json", "headers": {}, "correlation_id": message.correlation_id, "delivery_mode": 2, } try: channel = self.connection.channel() # type: ignore mappings_trigger = {"type": "mapping", "dataset_id": datasetID, "accession_ids": [accessionID]} mappings_msg = json.dumps(mappings_trigger) ValidateJSON(load_schema("dataset-mapping")).validate(json.loads(mappings_msg)) mapping = Message.create(channel, mappings_msg, properties) mapping.publish(environ.get("MAPPINGS_QUEUE", "mappings"), exchange=environ.get("BROKER_EXCHANGE", "sda")) channel.close() LOG.info( f"Sent the message to mappings queue to set dataset ID {datasetID} for file" f"with accessionID {accessionID}." ) except ValidationError: LOG.error("Could not validate the ingestion mappings message. Not properly formatted.") raise Exception("Could not validate the ingestion mappings message. Not properly formatted.")
def test_message_expiration_custom_value(self): expiration = '2000' message = Message.create(None, '') message.expiration = expiration self.assertEqual(expiration, message.expiration)
def send_request(self, routing_key, message, callback=False): # Create the Message object. log( "rabbitmq-producer:send_request", "Got message: {0} with routing_key: {1}".format( message, routing_key)) message = Message.create(rabbit_consumer.channel, json.dumps(message)) if callback: message.reply_to = rabbit_consumer.callback_queue rabbit_consumer.update_queue(message.correlation_id) # Create an entry in our local dictionary, using the automatically # generated correlation_id as our key. message.properties['message_type'] = routing_key # Publish the RPC request. log("rabbitmq-producer:send_request", "Publishing message..") message.publish(routing_key=routing_key, exchange='Core') # Return the Unique ID used to identify the request. log("rabbitmq-producer:send_request", "Got correlation_id: {0}".format(str(message.correlation_id))) return message.correlation_id
def test_message_content_type_custom_value(self): content_type = 'application/json' message = Message.create(None, '') message.content_type = content_type self.assertEqual(content_type, message.content_type)
def test_message_timestamp_custom_value(self): dt = datetime.now() message = Message.create(None, '') message.timestamp = dt self.assertEqual(dt, message.timestamp)
def test_message_app_id_custom_value(self): app_id = 'my-app' message = Message.create(None, '') message.app_id = app_id self.assertEqual(app_id, message.app_id)
def test_message_delivery_mode_two(self): delivery_mode = 2 message = Message.create(None, '') message.delivery_mode = delivery_mode self.assertEqual(delivery_mode, message.delivery_mode)
def test_message_user_id_custom_value(self): user_id = 'guest' message = Message.create(None, '') message.user_id = user_id self.assertEqual(user_id, message.user_id)
def test_message_priority_three(self): priority = 3 message = Message.create(None, '') message.priority = priority self.assertEqual(priority, message.priority)
def handleMessage(self): if (self.data.startswith("start process:")): if (self.pState == True): self.sendMessage("Already started") return streamKey = self.data.split("start process:") if (len(streamKey) != 2): print("No stream key provided!") self.sendMessage("No streamKey provided") return self.sendMessage("Starting Video Processing") #idetector = threading.Thread(target=process_stream) self.pState = True self.streamKey = str(streamKey[1]) tokens[streamKey[1]] = self print(streamKey[1]) mainchannelin.queue.declare(streamKey[1]) p = Process(target=process_stream, args=(streamKey[1], )) #p = threading.Thread(target=process_stream) self.process = p p.start() print("Done starting") else: print(str(self.data)) message = Message.create(mainchannelin, str(self.data)) message.publish(self.streamKey)
def test_message_get_channel(self): class FakeClass(object): pass message = Message(body='', channel=FakeClass()) self.assertIsInstance(message.channel, FakeClass)
def test_message_auto_decode_when_method_contains_list(self): method_data = {'key': [b'a', b'b']} message = Message(body=self.message, method=method_data, channel=None) self.assertEqual(method_data['key'][0].decode('utf-8'), message.method['key'][0])
def publish_messages(): with Connection('127.0.0.1', 'guest', 'guest') as connection: with connection.channel() as channel: # Declare the Queue, 'simple_queue'. channel.queue.declare('simple_queue') # Message Properties. properties = { 'content_type': 'text/plain', 'headers': { 'key': 'value' } } # Enable server local transactions on channel. channel.tx.select() # Create the message. message = Message.create(channel, 'Hello World!', properties) # Publish the message to a queue called, 'simple_queue'. message.publish('simple_queue') # Commit the message(s). channel.tx.commit() # Alternatively rollback the message(s). # channel.tx.rollback() # You can also use the context manager. with channel.tx: message.publish('simple_queue')
def test_message_message_type_custom_value(self): message_type = 'mymessage' message = Message.create(None, '') message.message_type = message_type self.assertEqual(message_type, message.message_type)
def test_message_auto_decode_when_method_is_tuple(self): method_data = (1, 2, 3, 4, 5) message = Message(body=self.message, method=method_data, channel=None) self.assertEqual(method_data, message.method) self.assertEqual(method_data[0], message.method[0]) self.assertEqual(method_data[4], message.method[4])
def test_message_delivery_tag_is_none(self): message = Message.create(body='', channel=FakeChannel()) message._method = { 'delivery_tag': None } self.assertIsNone(message.delivery_tag)
def test_message_not_redelivered(self): message = Message.create(body='', channel=FakeChannel()) message._method = { 'redelivered': False } self.assertFalse(message.redelivered)
def send_message(self, routing_key, body): try: message = Message.create(self.channel, body=body) message.reply_to = "admin" message.publish(routing_key, mandatory=True) self._close() except Exception: raise
def test_message_redelivered_is_none(self): message = Message.create(body='', channel=FakeChannel()) message._method = { 'redelivered': None } self.assertIsNone(message.redelivered)
def test_message_id_custom_value(self): message_id = 'my-message-1' message = Message.create(None, '') message.message_id = message_id self.assertEqual(message_id, message.properties['message_id']) self.assertEqual(message_id, message._properties['message_id'])
def test_message_delivery_tag(self): message = Message.create(body='', channel=FakeChannel()) message._method = { 'delivery_tag': 5 } self.assertEqual(message.delivery_tag, 5)
def test_functional_publish_fail_recover(self): self.channel.queue.declare(self.queue_name) self.channel.confirm_deliveries() message = Message.create(self.channel, self.message) self.assertRaises(AMQPChannelError, message.publish, self.message, exchange=self.exchange_name, mandatory=True) self.assertFalse(self.channel.is_open)
def publisher(): with Connection(HOST, USERNAME, PASSWORD) as connection: with connection.channel() as channel: channel.queue.declare('simple_queue') message = Message.create(channel, 'Hello World!', {'content_type': 'text/plain', 'headers': {'key': 'value'}}) message.publish('simple_queue')
def setUp(self): self.connection = Connection(HOST, USERNAME, PASSWORD) self.channel = self.connection.channel() self.channel.queue.declare('test.basic.resend') self.channel.queue.purge('test.basic.resend') self.channel.confirm_deliveries() message = Message.create(self.channel, body=str(uuid.uuid4())) message.app_id = 'travis-ci' message.publish('test.basic.resend')
def call(self, number): self.response = None message = Message.create(self.channel, body=str(number)) message.reply_to = self.callback_queue self.correlation_id = message.correlation_id message.publish(routing_key='rpc_queue') while not self.response: self.channel.process_data_events(to_tuple=False) return int(self.response)
def test_message_ack(self): delivery_tag = 123456 message = Message.create(body='', channel=FakeChannel()) message._method = { 'delivery_tag': delivery_tag } message.ack() result = message.channel.result.pop(0) self.assertEqual(result[0], delivery_tag) self.assertEqual(result[1], False)
def send_request(self, payload): # Create the Message object. message = Message.create(self.channel, payload) message.reply_to = self.callback_queue # Create an entry in our local dictionary, using the automatically # generated correlation_id as our key. self.queue[message.correlation_id] = None # Publish the RPC request. message.publish(routing_key=self.rpc_queue) # Return the Unique ID used to identify the request. return message.correlation_id
def test_create_new_message(self): body = b'Hello World' message = Message.create(None, body, properties={'key': 'value', 'headers': { b'name': b'eandersson'} }) self.assertIsInstance(message, Message) self.assertEqual(message._body, body) result = message.to_dict() self.assertIsNone(result['method']) self.assertEqual(result['body'], body) self.assertEqual(result['properties']['key'], 'value')
def test_message_default_properties(self): body = self.message message = Message.create(None, body) self.assertIsNone(message.app_id) self.assertIsNone(message.reply_to) self.assertIsNone(message.content_encoding) self.assertIsNone(message.content_type) self.assertIsNone(message.priority) self.assertIsNone(message.delivery_mode) self.assertIsInstance(message.message_id, str) self.assertIsInstance(message.correlation_id, str) self.assertIsInstance(message.timestamp, datetime)
def on_request(message): number = int(message.body) print(" [.] fib(%s)" % (number,)) response = str(fib(number)) properties = { 'correlation_id': message.correlation_id } response = Message.create(message.channel, response, properties) response.publish(message.reply_to) message.ack()