コード例 #1
0
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()
コード例 #2
0
    def test_get_next_task_raise_shutdown_exception_if_locked_and_no_tasks_running(self):
        tm = TaskManager(conf={'worker': self._worker})
        tm.lock()

        # ensure no tasks are running
        self.assertEqual(len(self._worker.worker.running_tasks()), 0)

        with self.assertRaises(ShutdownException):
            tm.get_next_task()
コード例 #3
0
ファイル: main.py プロジェクト: kdudka/kobo
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()
コード例 #4
0
    def test_get_next_task_runs_assigened_awaited_task_if_locked(self):
        t1 = Task.objects.create(
            worker=self._worker.worker,
            arch=self._arch,
            channel=self._channel,
            owner=self._user,
            method='DummyForegroundTask',
            state=TASK_STATES['OPEN'],
        )

        t2 = Task.objects.create(
            worker=self._worker.worker,
            arch=self._arch,
            channel=self._channel,
            owner=self._user,
            method='DummyForegroundTask',
            state=TASK_STATES['ASSIGNED'],
            awaited=True,
            parent=t1,
        )

        t3 = Task.objects.create(
            worker=self._worker.worker,
            arch=self._arch,
            channel=self._channel,
            owner=self._user,
            method='DummyForegroundTask',
            waiting=True,
            parent=t2,
            state=TASK_STATES['FREE'],
        )

        self.assertEqual(t1.state, TASK_STATES['OPEN'])
        self.assertEqual(t2.state, TASK_STATES['ASSIGNED'])
        self.assertEqual(t3.state, TASK_STATES['FREE'])

        tm = TaskManager(conf={'worker': self._worker})
        tm.lock()
        tm.get_next_task()

        # reload task info
        t1 = Task.objects.get(id=t1.id)
        self.assertEqual(t1.state, TASK_STATES['OPEN'])

        t2 = Task.objects.get(id=t2.id)
        self.assertEqual(t2.state, TASK_STATES['CLOSED'])

        t3 = Task.objects.get(id=t3.id)
        self.assertEqual(t3.state, TASK_STATES['FREE'])
コード例 #5
0
    def test_get_next_task_runs_free_task(self):
        t = Task.objects.create(
            worker=self._worker.worker,
            arch=self._arch,
            channel=self._channel,
            owner=self._user,
            method='DummyForegroundTask',
            state=TASK_STATES['FREE'],
        )

        self.assertEqual(t.state, TASK_STATES['FREE'])

        tm = TaskManager(conf={'worker': self._worker})
        tm.get_next_task()

        # reload task info
        t = Task.objects.get(id=t.id)
        self.assertEqual(t.state, TASK_STATES['CLOSED'])
コード例 #6
0
    def test_get_next_task_dont_run_tasks_if_not_ready(self):
        t = Task.objects.create(
            worker=self._worker.worker,
            arch=self._arch,
            channel=self._channel,
            owner=self._user,
            method='DummyForegroundTask',
            state=TASK_STATES['FREE'],
        )

        self.assertEqual(t.state, TASK_STATES['FREE'])

        tm = TaskManager(conf={'worker': self._worker})
        tm.worker_info['ready'] = False

        tm.get_next_task()

        # reload task info
        t = Task.objects.get(id=t.id)
        self.assertEqual(t.state, TASK_STATES['FREE'])