def main_loop(conf, foreground=False, task_manager_class=None): """infinite daemon loop""" # define custom signal handlers signal.signal(signal.SIGTERM, daemon_shutdown) # initialize TaskManager try: log_file = conf.get("LOG_FILE", None) logger = logging.Logger("TaskManager") logger.setLevel(logging.DEBUG) if log_file: log_level = logging.getLevelName(conf.get("LOG_LEVEL", "DEBUG").upper()) kobo.log.add_rotating_file_logger(logger, log_file, log_level=log_level) if not task_manager_class: tm = TaskManager(conf=conf, logger=logger) else: tm = task_manager_class(conf=conf, logger=logger) except Exception as ex: raise sys.stderr.write("Error initializing TaskManager: %s\n" % ex) sys.exit(1) if foreground and tm._logger is not None: kobo.log.add_stderr_logger(tm._logger) while 1: try: tm.log_debug(80 * '-') # poll hub for new tasks tm.hub._login() tm.update_worker_info() tm.update_tasks() tm.get_next_task() # write to stdout / stderr sys.stdout.flush() sys.stderr.flush() # sleep for some time tm.sleep() except (ShutdownException, KeyboardInterrupt): # ignore keyboard interrupts and sigterm signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN) tm.log_info('Exiting...') tm.shutdown() tm.log_info('Cleanup done...') break except: # this is a little extreme: log the exception and continue traceback = Traceback() tm.log_error(traceback.get_traceback()) tm.sleep()
def test_update_worker_info_catch_protocol_error(self): tm = TaskManager(conf={'worker': self._worker}) tm.hub.worker.update_worker = Mock(side_effect=ProtocolError(None, None, None, None)) tm.update_worker_info() with self.assertRaises(ValueError): tm.hub.worker.update_worker = Mock(side_effect=ValueError) tm.update_worker_info()
def test_update_worker_info(self): tm = TaskManager(conf={'worker': self._worker}) self.assertTrue(tm.worker_info['enabled']) self.assertTrue(tm.worker_info['ready']) tm.worker_info['enabled'] = False tm.worker_info['ready'] = False tm.update_worker_info() self.assertFalse(tm.worker_info['enabled']) self.assertFalse(tm.worker_info['ready'])
def main_loop(conf, foreground=False): """infinite daemon loop""" # define custom signal handlers signal.signal(signal.SIGTERM, daemon_shutdown) # initialize TaskManager try: log_file = conf.get("LOG_FILE", None) logger = logging.Logger("TaskManager") logger.setLevel(logging.DEBUG) if log_file: log_level = logging._levelNames.get(conf.get("LOG_LEVEL", "DEBUG").upper()) kobo.log.add_rotating_file_logger(logger, log_file, log_level=log_level) tm = TaskManager(conf=conf, logger=logger) except Exception as ex: raise sys.stderr.write("Error initializing TaskManager: %s\n" % ex) sys.exit(1) if foreground and tm._logger is not None: kobo.log.add_stderr_logger(tm._logger) while 1: try: tm.log_debug(80 * '-') # poll hub for new tasks tm.hub._login() tm.update_worker_info() tm.update_tasks() tm.get_next_task() # write to stdout / stderr sys.stdout.flush() sys.stderr.flush() # sleep for some time tm.sleep() except (ShutdownException, KeyboardInterrupt): # ignore keyboard interrupts and sigterm signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN) tm.log_info('Exiting...') tm.shutdown() tm.log_info('Cleanup done...') break except: # this is a little extreme: log the exception and continue traceback = Traceback() tm.log_error(traceback.get_traceback()) tm.sleep()