def test_should_publish_reject_and_requeue_from_dead_letter_exchange( given_any_petisco, make_user_created_event, given_random_organization, given_random_service, given_random_topic, given_a_short_message_ttl, ): event = make_user_created_event() tracked_events_spy = TrackedEventsSpy() tracked_requeue_events_dead_letter_spy = TrackedEventsSpy() publisher = RabbitMQEventPublisher( connector=RabbitMqConnector(), organization=given_random_organization, service=given_random_service, topic=given_random_topic, ) @subscriber_handler() def main_handler(event: Event): tracked_events_spy.append(event) return isFailure @subscriber_handler() def requeue_from_dead_letter(event: Event): tracked_requeue_events_dead_letter_spy.append(event) publisher.publish(event) return isSuccess publisher.publish(event) subscriber = RabbitMQEventSubscriber( connector=RabbitMqConnector(), subscribers={ "auth": ConfigEventSubscriber( organization=given_random_organization, service=given_random_service, topic=given_random_topic, handler=main_handler, ), "dead-letter": ConfigEventSubscriber( organization=given_random_organization, service=given_random_service, topic=given_random_topic, handler=requeue_from_dead_letter, dead_letter=True, ), }, ) subscriber.start() await_for_events() tracked_events_spy.assert_number_events(1) tracked_requeue_events_dead_letter_spy.assert_number_events(1) subscriber.stop()
def subscribe_to_dead_letter(): subscriber = RabbitMQEventSubscriber( connector=RabbitMqConnector(), subscribers={ "dead-letter": ConfigEventSubscriber( organization="acme", service="taskmanager", topic="taskmanager-events", handler=requeue_from_dead_letter, dead_letter=True, ) }, connection_name="dead-letter-subscriber", ) subscriber.start() scheduler = BackgroundScheduler() def shutdown(): subscriber.start() scheduler.shutdown() scheduler.add_job(func=shutdown, trigger="interval", seconds=TEN_MINUTES) scheduler.start()
def default(main_handler: Callable, connector: RabbitMqConnector = None): connector = RabbitMqConnector() if not connector else connector return RabbitMQEventSubscriber( connector=connector, subscribers={ "auth": ConfigEventSubscriber( organization=DEFAULT_ORGANIZATION, service=DEFAULT_SERVICE, topic=f"{DEFAULT_SERVICE}-event", handler=main_handler, ) }, connection_name="subscriber", )
def test_should_work_successfully_a_happy_path_pub_sub( given_any_petisco, make_user_created_event, given_random_organization, given_random_service, given_random_topic, ): event_1 = make_user_created_event() event_2 = make_user_created_event() tracked_events_spy = TrackedEventsSpy() @subscriber_handler() def main_handler(event: Event): tracked_events_spy.append(event) return isSuccess publisher = RabbitMQEventPublisher( connector=RabbitMqConnector(), organization=given_random_organization, service=given_random_service, topic=given_random_topic, ) publisher.publish_events([event_1, event_2]) subscriber = RabbitMQEventSubscriber( connector=RabbitMqConnector(), subscribers={ "auth": ConfigEventSubscriber( organization=given_random_organization, service=given_random_service, topic=given_random_topic, handler=main_handler, ) }, ) subscriber.start() await_for_events() tracked_events_spy.assert_number_events(2) subscriber.stop()
def _given_any_subscriber( given_random_organization, given_random_service, given_random_topic, subscriber_handler: Callable, ): subscriber = RabbitMQEventSubscriber( connector=RabbitMqConnector(), subscribers={ "petisco": ConfigEventSubscriber( organization=given_random_organization, service=given_random_service, topic=given_random_topic, handler=subscriber_handler, ) }, ) return subscriber
@subscriber_handler() def a_handler(event: Event): print(f" [x] a_handler {event}") return isSuccess @subscriber_handler(percentage_simulate_nack=0.5) def b_handler(event: Event): print(f" [x] b_handler {event}") return isSuccess subscriber = RabbitMQEventSubscriber( connector=RabbitMqConnector(), subscribers={ "a": ConfigEventSubscriber(organization="acme", service="a", topic="a-events", handler=a_handler), "b": ConfigEventSubscriber(organization="acme", service="b", topic="b-events", handler=b_handler), }, connection_name="petisco-subscribers", ) subscriber.start()
@subscriber_handler(delay_after=1) def b_requeue(event: Event): print(f"requeue > {event}") b_publisher.publish(event) return isSuccess subscriber = RabbitMQEventSubscriber( connector=RabbitMqConnector(), subscribers={ "dl-a": ConfigEventSubscriber( organization="acme", service="a", topic="a-events", handler=a_requeue, dead_letter=True, ), "dl-b": ConfigEventSubscriber( organization="acme", service="b", topic="b-events", handler=b_requeue, dead_letter=True, ), }, connection_name="petisco-subscribers", ) subscriber.start()