def test_set_child_logger_queue(self, setup):
     new_logger_queue = multiprocessing.Queue()
     LoggerManager.set_child_logger_queue(self.config_logger,
                                          new_logger_queue)
     assert LoggerManager.logger_queue == new_logger_queue
     assert LoggerManager.config_logger['subprocess']['handlers']['queue'][
         'queue'] == new_logger_queue
 def setup(self):
     with patch(
             "via_common.multiprocess.logger_manager.LoggerManager._check_or_create_directory"
     ):
         self.__class__.config_conn_dict = {
             "host": "127.0.0.1",
             "port": 41570,
             "authkey": "abc"
         }
         LoggerManager().__del__()
         self.__class__.config_internal_conn = ConfigServerPure(
             self.config_conn_dict, 'test')
         self.__class__.config_logger = config_logger_obj(
             config_logger=
             '{"root":{"version":1}, "subprocess":{"version":1}}')
         queue_name_list = ['test']
         shutdown_receiver = None
         self.__class__.process = self._processmixin_obj(
             'process_name', self.config_internal_conn, self.config_logger,
             queue_name_list, shutdown_receiver)
         LoggerManager.init_root_logger(self.config_logger)
         self.__class__.queue_manager = QueueManager(
             self.config_internal_conn, ['test'])
     yield "teardown"
     LoggerManager().__del__()
Example #3
0
 def __init__(self,
              config: ConfigMixInServer,
              config_logger: ConfigMixInLogger,
              logger_queue=None) -> None:
     super().__init__(config)
     LoggerManager.set_child_logger_queue(config_logger, logger_queue)
     self.logger = LoggerManager.get_logger(__class__.__name__)
     self.conn = None
     self.registered_channel = []
     self.connected = False
     self.disconnecting = False
Example #4
0
 def __init__(self,
              config_internal_conn: ConfigMixInServer,
              queue_name_list=[]):
     self.config_internal_conn = config_internal_conn
     self.queue_name_list = queue_name_list
     #
     self.logger = LoggerManager.get_logger(__class__.__name__)
     self.system_queue = Queue()
     self.logger_queue = LoggerManager.get_logger_queue()
     self.manager = None
     #
     self._init_queue()
Example #5
0
    def _run_child_process(self, config_conn_cls, config_logger_cls):
        # All this is to setup the logger_queue to be able to log.
        self.config_internal_conn = config_conn_cls()
        self.config_logger = config_logger_cls()

        # arguments here are passed through pickle. They cannot be class member
        self.shutdown_handler = PipeAdapter(self.shutdown_receiver,
                                            self.shutdown)
        self.shutdown_handler.start()

        if self.queue_name_list:
            for queue_name in self.queue_name_list:
                BaseManager.register(queue_name)
            # end for queue_name in queue_name_list
        # end if queue_name_list

        BaseManager.register('logger_queue')
        BaseManager.register('system_queue')
        manager = BaseManager(
            address=(self.config_internal_conn.host(),
                     self.config_internal_conn.port()),
            authkey=self.config_internal_conn.authkey().encode())
        time.sleep(0.5)
        attempt = 5
        while attempt:
            try:
                manager.connect()
                break
            except Exception as e:  # TODO exception: which one(s)
                attempt -= 1
        # end while attempt

        if attempt == 0:
            raise RuntimeError("{} child process FAILED to start".format(
                (self.__class__.__name__)))

        # Create the object fields for the queues
        if self.queue_name_list:
            for queue_name in self.queue_name_list:
                self.__dict__[queue_name] = manager.__getattribute__(
                    queue_name)()
            # end for queue_name in queue_name_list
        # end if queue_name_list

        # Set the queue for the logger
        self.system_queue = manager.system_queue()
        logger_queue = manager.logger_queue()
        LoggerManager.set_child_logger_queue(self.config_logger, logger_queue)
        self.logger = LoggerManager.get_logger(self.process_name)
        self.logger.info('Sub-process [{}] initialized'.format(
            self.process_name))

        self._run_child_forever(logger_queue)
 def test_get_logger(self, setup):
     logger = LoggerManager.get_logger('test')
     assert isinstance(logger, logging.Logger)
     assert logger.name == 'test'
     assert isinstance(LoggerManager.logger_queue_listener,
                       logging.handlers.QueueListener)
     assert LoggerManager.logger_queue_listener.queue == LoggerManager.logger_queue
 def setup(self):
     with patch("via_common.multiprocess.logger_manager.LoggerManager._check_or_create_directory"):
         LoggerManager().__del__()
         config_logger = config_logger_obj(config_logger='{"root":{"version":1}, "subprocess":{}}')
         LoggerManager.init_root_logger(config_logger)
         config_conn = {"host": "127.0.0.1", "port": 41567, "authkey": "abc"}
         # We need a proper config otherwise cannot be pickled
         self.__class__.config_internal_conn = ConfigServerPure(config_conn, 'test')
         config_server = '{"host":"127.0.0.1", "port":12346, "authkey":"xyz", "user_id": "pqr", "login": "******", "timeout":123, "keepalive":456, "retry":789}'
         with patch("builtins.open", new_callable=mock_open, read_data=config_server):
             self.__class__.config_server = config_server_obj()
         self.__class__.middleware = middleware_obj(self.config_server)
         self.__class__.queue_manager = QueueManager(self.config_internal_conn)
         self.__class__.backgroundthread = backgroundthread_obj(self.config_internal_conn, self.middleware)
     yield "teardown"
     LoggerManager().__del__()
 def setup(self):
     LoggerManager().__del__()
     config_conn = {"host": "127.0.0.1", "port": 41568, "authkey": "abc"}
     self.__class__.config_internal_conn = ConfigServerPure(
         config_conn, 'test')
     self.__class__.queue_name_list = ['test']
     self.__class__.config_logger = config_logger_obj(
         config_logger=basic_logger_config)
     yield "teardown"
     LoggerManager().__del__()
     logging.shutdown()
     import time
     time.sleep(1)
     if os.path.isfile('logger_file_name'):
         os.remove('logger_file_name')
     if os.path.isfile('logger_file_name_error'):
         os.remove('logger_file_name_error')
Example #9
0
 def setup(self):
     with patch(
             "via_common.multiprocess.logger_manager.LoggerManager._check_or_create_directory"
     ):
         # with patch("via_common.multiprocess.logger_manager.LoggerManager.config_logger", lambda: '{"root":{"version":1}, "subprocess":{}}'):
         LoggerManager().__del__()
         config_logger = config_logger_obj(
             config_logger='{"root":{"version":1}, "subprocess":{}}')
         LoggerManager.init_root_logger(config_logger)
         # patching manually the LoggerManager to avoid some side effects that are NOT under the scope of what is tested here
         LoggerManager.logger_queue_listener = 'not none'
         config_server = '{"host":"abc", "port":1234, "authkey":"xyz", "user_id": "pqr", "login": "******", "timeout":123, "keepalive":456, "retry":789}'
         with patch("builtins.open",
                    new_callable=mock_open,
                    read_data=config_server):
             self.__class__.config_server = config_server_obj()
         config_logger = config_logger_obj(
             config_logger='{"root":{"version":1}, "subprocess":{}}')
         self.__class__.middleware = middleware_redis_obj(
             self.config_server, config_logger, 'queue')
         yield "teardown"
         LoggerManager().__del__()
 def setup(self):
     with patch(
             "via_common.multiprocess.logger_manager.LoggerManager._check_or_create_directory"
     ):
         LoggerManager().__del__()
         config_logger = config_logger_obj(
             config_logger='{"root":{"version":1}, '
             '"subprocess":{"version":1,'
             '    "handlers":{"queue":{"class":"logging.handlers.QueueHandler","queue": "None"}}}}'
         )
         LoggerManager.init_root_logger(config_logger)
         config_conn = {
             "host": "127.0.0.1",
             "port": 41569,
             "authkey": "abc"
         }
         # We need a proper config otherwise cannot be pickled
         self.__class__.config_internal_conn = ConfigServerPure(
             config_conn, 'test')
         self.__class__.queue_name_list = ['test']
         self.__class__.queue_manager = QueueManager(
             self.config_internal_conn, self.queue_name_list)
     yield "teardown"
     LoggerManager().__del__()
 def test_get_logger_queue(self, setup):
     logger_queue = self.queue_manager.get_logger_queue()
     assert isinstance(logger_queue, multiprocessing.queues.Queue)
     assert self.queue_manager.logger_queue == logger_queue
     assert LoggerManager.get_logger_queue() == logger_queue
 def test__check_or_create_directory(self, setup):
     global makedirs_called
     LoggerManager._check_or_create_directory()
     assert makedirs_called
 def test_get_logger_queue(self, setup):
     assert LoggerManager.get_logger_queue() == LoggerManager.logger_queue
 def test_1_init_root_logger(self, setup):
     LoggerManager.init_root_logger(self.config_logger)
     assert isinstance(LoggerManager.logger_queue,
                       multiprocessing.queues.Queue)
     assert LoggerManager.config_logger['subprocess']['handlers']['queue'][
         'queue'] == LoggerManager.logger_queue