def test_prepare_models(self):
     conf = WorkerConfig(self.mkargs('--name=foo --database=localhost:6379:13'
                                     ' --job-model=tests.workers.WorkerConfigRunTest.JobModelOtherDB'
                                     ' --queue-model=tests.workers.WorkerConfigRunTest.QueueModelOtherDB'
                                     ' --error-model=tests.workers.WorkerConfigRunTest.ErrorModelOtherDB'))
     conf.prepare_models()
     for model_name in ('job', 'queue', 'error'):
         model = getattr(conf.options, '%s_model' % model_name)
         self.assertEqual(model.database.connection_settings['db'], 13)
    def test_proc_title(self):
        conf = WorkerConfig(self.mkargs('--name=foo'))
        self.assertEqual('test-script [init]', conf.get_proc_title())

        conf.prepare_worker()
        self.assertEqual('test-script [init] queue=foo', conf.get_proc_title())

        conf.worker.status = 'waiting'
        self.assertEqual('test-script [waiting] queue=foo loop=0/1000 waiting-jobs=0', conf.get_proc_title())

        conf.worker.end_forced = True
        self.assertEqual('test-script [waiting - ending] queue=foo loop=0/1000 waiting-jobs=0', conf.get_proc_title())
    def test_prepare_worker(self):
        conf = WorkerConfig(self.mkargs('--name=foo'))
        self.assertIsNone(getattr(conf, 'worker', None))

        conf.prepare_worker()
        self.assertIsInstance(conf.worker, Worker)

        conf = WorkerConfig(self.mkargs('--name=bar --worker-class=tests.workers.WorkerConfigRunTest.WorkerClass'))
        conf.prepare_worker()
        self.assertIsInstance(conf.worker, WorkerConfigRunTest.WorkerClass)
        self.assertFalse(conf.worker.end_forced)

        conf = WorkerConfig(self.mkargs('--name=baz --dry-run'))
        conf.prepare_worker()
        self.assertTrue(conf.worker.end_forced)
Exemple #4
0
def main():
    # first options needed for this script itself (the rest will be ignored for now)
    option_list = (
        make_option('--pythonpath', action='append',
            help='A directory to add to the Python path, e.g. --pythonpath=/my/module'),
        make_option('--worker-config', dest='worker_config',
            help='The worker config class to use, e.g. --worker-config=my.module.MyWorkerConfig, '
                  'default to limpyd_jobs.workers.WorkerConfig')
    )

    # create a light option parser that ignore everything but basic options
    # defined below
    parser = LaxOptionParser(usage="%prog [options]", option_list=option_list)
    options, args = parser.parse_args(sys.argv[:])

    # if we have some pythonpaths, add them
    if options.pythonpath:
        sys.path[0:0] = parser.parse_python_paths(sys.argv[:])

    try:
        # still load the defaut config, needed to parse the worker_config option
        from limpyd_jobs.workers import WorkerConfig

        # by default use the default worker config
        worker_config_class = WorkerConfig

        # and try to load the one passed as argument if any
        if options.worker_config:
            worker_config_class = WorkerConfig.import_class(options.worker_config)

        # finally instantiate and run the worker
        worker_config = worker_config_class()
        worker_config.execute()
    except ImportError as e:
        parser.print_lax_help()
        parser.lax_error('No WorkerConfig found. You need to use --pythonpath '
                         'and/or --worker-config: %s' % str(e))
            "default to limpyd_jobs.workers.WorkerConfig",
        ),
    )

    # create a light option parser that ignore everything but basic options
    # defined below
    parser = LaxOptionParser(usage="%prog [options]", option_list=option_list)
    options, args = parser.parse_args(sys.argv[:])

    # if we have some pythonpaths, add them
    if options.pythonpath:
        sys.path[0:0] = parser.parse_python_paths(sys.argv[:])

    try:
        # still load the defaut config, needed to parse the worker_config option
        from limpyd_jobs.workers import WorkerConfig

        # by default use the default worker config
        worker_config_class = WorkerConfig

        # and try to load the one passed as argument if any
        if options.worker_config:
            worker_config_class = WorkerConfig.import_class(options.worker_config)

        # finally instantiate and run the worker
        worker_config = worker_config_class()
        worker_config.execute()
    except ImportError, e:
        parser.print_lax_help()
        parser.lax_error("No WorkerConfig found. You need to use --pythonpath " "and/or --worker-config: %s" % str(e))