def register_all(self, message: RegisterAll) -> Set[Type[RdisqMessage]]: handlers: Dict[Type[RdisqMessage], "_Handler"] = get_rdisq_config( ).handler_factory.create_handlers_for_object( message.new_handler_kwargs, message.handler_class) for message_type, handler in handlers.items(): self.add_queue(AddQueue(message_type.get_message_class_id())) self._handlers[message_type] = handler self._on_process_loop() return self.get_registered_messages()
def list_uids(cls): uids = [] rdb = get_rdisq_config().request_dispatcher.get_redis() key = cls.get_service_uid_list_key() for k, v in rdb.hgetall(key).items(): if float(v) > time.time() - 10: uids.append(k.decode()) else: rdb.hdel(key, k) return uids
def register_message(self, message: RegisterMessage) -> Set[Type[RdisqMessage]]: if message.new_message_class in self.get_registered_messages(): raise RuntimeError( f"Tried registering {message.new_message_class} to {self}." f"But it's already registered.") self.add_queue( AddQueue(message.new_message_class.get_message_class_id())) self._handlers[message.new_message_class] = get_rdisq_config( ).handler_factory.create_handler(message.new_message_class, message.new_handler_instance, self._handlers.values()) self._on_process_loop() return self.get_registered_messages()
def test_service_control_messages(rdisq_message_fixture): receiver_service = rdisq_message_fixture.spawn_receiver() Thread(group=None, target=receiver_service.process).start() receiver_service.wait_for_process_to_start(5) assert RdisqRequest(RegisterMessage(SumMessage)).send_and_wait_reply() == { SumMessage } | CORE_RECEIVER_MESSAGES try: RdisqRequest(RegisterMessage(SumMessage)).send_and_wait_reply() except Exception: pass else: raise RuntimeError( "Should have failed to re-sum_ a message to a receiver.") dispatcher = get_rdisq_config().request_dispatcher receivers_from_redis = dispatcher.get_receiver_services() assert receivers_from_redis[receiver_service.uid].registered_messages == { SumMessage } | CORE_RECEIVER_MESSAGES assert receivers_from_redis[ receiver_service.uid].uid == receiver_service.uid assert receiver_service.get_registered_messages() == { SumMessage } | CORE_RECEIVER_MESSAGES assert RdisqRequest(GetRegisteredMessages()).send_and_wait_reply() == { SumMessage } | CORE_RECEIVER_MESSAGES assert RdisqRequest(SumMessage(1, 2)).send_and_wait_reply() == 3 RdisqRequest(UnregisterMessage(SumMessage)).send_and_wait_reply() try: RdisqRequest(SumMessage(1, 2)).send_and_wait_reply(1) except RuntimeError: pass else: raise RuntimeError("Should have failed communicating with receiver") assert RdisqRequest(RegisterMessage( AddMessage, {"start": 1 })).send_and_wait_reply() == {AddMessage} | CORE_RECEIVER_MESSAGES assert RdisqRequest(AddMessage(3)).send_and_wait_reply() == 4 rdisq_message_fixture.kill_all()
def __init__(self, uid=None, message_class: Type[RdisqMessage] = None, instance: object = None, dispatcher: RequestDispatcher = None): self._tags = {} self.redis_dispatcher = dispatcher or get_rdisq_config( ).request_dispatcher super().__init__(uid) self._handlers = dict() for m in CORE_RECEIVER_MESSAGES: handling_message = RegisterMessage(m, self) self.register_message(handling_message) if message_class: self.register_message(RegisterMessage(message_class, instance)) self._on_process_loop()
def test_queues(rdisq_message_fixture): receiver_service = rdisq_message_fixture.spawn_receiver() RdisqRequest(AddQueue(new_queue_name="test_queue")).send_async() receiver_service.rdisq_process_one() assert 'ReceiverService_test_queue' in receiver_service.listening_queues RdisqRequest(RegisterMessage(SumMessage)).send_async() receiver_service.rdisq_process_one() dispatcher = get_rdisq_config().request_dispatcher r = dispatcher.queue_task("ReceiverService_test_queue", SumMessage(1, 2)) receiver_service.rdisq_process_one() assert r.wait(1) == 3 r = RdisqRequest(RemoveQueue(old_queue_name="test_queue")).send_async() receiver_service.rdisq_process_one() result = r.wait() assert 'ReceiverService_test_queue' not in result assert 'ReceiverService_test_queue' not in receiver_service.listening_queues
def set_handler(cls, handler_function: Callable) -> Callable: """Can be used as a decorator""" return get_rdisq_config().handler_factory.set_handler_function( handler_function, cls)
def _get_handler_class(message_class: Type["RdisqMessage"]): return get_rdisq_config( ).handler_factory._get_handler_class_for_function( _HandlerFactory._messages_registered_handlers[message_class])