def process_next_message(self, queue_name, callback, model_validator, max_retry=5): logger.info("Process next message") topic = queue_name.split("/")[0] if topic in self.topics and queue_name in self.topics[topic] and len( self.topics[topic][queue_name]) > 0: msg = self.topics[topic][queue_name].pop(0) e = Event(**msg) callback(e)
def process_next_message(self, queue_name, callback, model_validator, max_retry=0): sub_queue = self.get_simple_queue(queue_name) retry_count = 0 while True: try: msg = sub_queue.get(block=False, timeout=20) try: e = Event(**msg.payload) except ValueError as ve: logger.error( f"Rejecting not valid event payload: {msg.payload}") msg.ack() return True try: if model_validator is not None: try: call_result = callback( e, model_validator(**e.payload)) except Exception as error: logger.error( f"Invalid payload for type. Errors: {str(error)}" ) msg.ack() return True else: call_result = callback(e) if call_result: msg.ack() return True else: logger.debug("Callback returning false") msg.requeue() return False except: msg.requeue() return False except IOError: logger.error("Lost connection with Rabbit") self.queues = {} return False except Empty as e: if retry_count < max_retry: sleep(0.1) retry_count += 1 else: return True
def test_should_have_event_name(self): invalid_event = valid_event.copy() invalid_event.pop("event_name") with pytest.raises(ValueError): Event(**invalid_event)
def test_should_have_aggregate_id(self): invalid_event = valid_event.copy() invalid_event.pop("aggregate_id") with pytest.raises(ValueError): Event(**invalid_event)
def test_should_have_sender(self): invalid_event = valid_event.copy() invalid_event.pop("sender") with pytest.raises(ValueError): Event(**invalid_event)
def test_should_have_payload(self): invalid_event = valid_event.copy() invalid_event.pop("payload") with pytest.raises(ValueError): Event(**invalid_event)
def test_should_have_version(self): invalid_event = valid_event.copy() invalid_event.pop("version") with pytest.raises(ValueError): Event(**invalid_event)
def test_should_valid_event_payload(self): Event(**valid_event)