def start_listening_to_rabbit_queue(queue, on_message_callback): rabbit = RabbitContext(queue_name=queue) rabbit.open_connection() rabbit.channel.basic_consume( queue=queue, on_message_callback=on_message_callback) rabbit.channel.start_consuming()
def test_attempt_to_publish_message_with_closed_connection_raises_correct_exception( self, patch_pika): with RabbitContext() as rabbit: pass with self.assertRaises(RabbitConnectionClosedError): rabbit.publish_message('This should raise an exception', 'text')
def publish_messages_from_json_file_path(queue_name: str, source_file_path: Path, destination_file_path: Path): """ NB: this exists to support the single (JSON) file model and should not be used with the new style (dump) files """ with RabbitContext(queue_name=queue_name) as rabbit: for file_path in source_file_path.rglob('*.json'): rabbit.publish_message(file_path.read_text(), 'application/json') file_path.replace(destination_file_path.joinpath(file_path.name))
def generate_messages_from_config_file(config_file, batch_id: uuid.UUID): config_file_reader = csv.DictReader(config_file) with RabbitContext() as rabbit: for row in config_file_reader: message_count = int(row['Quantity']) message_json = create_message_json(row['Questionnaire type'], batch_id) print(f'Queueing {message_count} questionnaire type {row["Questionnaire type"]}') for _ in range(message_count): rabbit.publish_message(message_json, 'application/json')
def test_attempt_to_publish_message_with_closed_connection_raises_correct_exception(self, patch_pika): with RabbitContext() as rabbit: def close_side_effect(): rabbit._connection.is_open = False patch_pika.BlockingConnection.return_value.close.side_effect = close_side_effect with self.assertRaises(RabbitConnectionClosedError): rabbit.publish_message('This should raise an exception', 'text')
def test_publish_message(self, patch_pika): with RabbitContext() as rabbit: rabbit.publish_message('Test message body', 'text') patch_pika.BasicProperties.assert_called_once_with(content_type='text', delivery_mode=2) patched_basic_publish = patch_pika.BlockingConnection.return_value.channel.return_value.basic_publish patched_basic_publish.assert_called_once_with(exchange=rabbit._exchange, routing_key=rabbit.queue_name, body='Test message body', properties=patch_pika.BasicProperties.return_value)
def _load_sample_units(action_plan_id: str, collection_exercise_id: str, sample_file_reader: Iterable[str], store_loaded_sample_units=False, sample_unit_log_frequency=5000, **kwargs): sample_units = {} with RabbitContext(**kwargs) as rabbit: logger.info(f'Loading sample units to queue {rabbit.queue_name}') for count, sample_row in enumerate(sample_file_reader, 1): sample_unit_id = uuid.uuid4() rabbit.publish_message(_create_case_json( sample_row, collection_exercise_id=collection_exercise_id, action_plan_id=action_plan_id), content_type='application/json') if store_loaded_sample_units: sample_unit = { f'sampleunit:{sample_unit_id}': _create_sample_unit_json(sample_unit_id, sample_row) } sample_units.update(sample_unit) if count % sample_unit_log_frequency == 0: logger.info(f'{count} sample units loaded') if count % sample_unit_log_frequency: logger.info(f'{count} sample units loaded') logger.info( f'All sample units have been added to the queue {rabbit.queue_name}') return sample_units
def test_context_manager_closes_connection(self, patch_pika): with RabbitContext(): pass patch_pika.BlockingConnection.return_value.close.assert_called_once()
def test_context_manager_opens_connection_and_channel(self, patch_pika): with RabbitContext(): patch_pika.BlockingConnection.assert_called_once() patch_pika.BlockingConnection.return_value.channel.assert_called_once( )
def publish_messages_from_dump_files(queue_name: str, source_file_path: Path, destination_file_path: Path): with RabbitContext(queue_name=queue_name) as rabbit: for file_path in source_file_path.rglob('*.dump'): for json_message in file_path.open(): rabbit.publish_message(json_message, 'application/json') file_path.replace(destination_file_path.joinpath(file_path.name))