Changeset View
Changeset View
Standalone View
Standalone View
swh/scheduler/cli.py
Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | lines = [ | ||||
pretty_print_list(task['arguments']['args'], indent=4), | pretty_print_list(task['arguments']['args'], indent=4), | ||||
click.style(' Keyword args:\n', bold=True), | click.style(' Keyword args:\n', bold=True), | ||||
pretty_print_dict(task['arguments']['kwargs'], indent=4), | pretty_print_dict(task['arguments']['kwargs'], indent=4), | ||||
] | ] | ||||
return ''.join(lines) | return ''.join(lines) | ||||
def list_task_types(ctx, param, value): | |||||
if not value or ctx.resilient_parsing: | |||||
return | |||||
click.echo("Known task types:") | |||||
for tasktype in ctx.obj['scheduler'].get_task_types(): | |||||
click.echo('{type}:\n {description}'.format(**tasktype)) | |||||
ctx.exit() | |||||
@click.group(context_settings=CONTEXT_SETTINGS) | @click.group(context_settings=CONTEXT_SETTINGS) | ||||
@click.option('--cls', '-c', default='local', | @click.option('--cls', '-c', default='local', | ||||
type=click.Choice(['local', 'remote']), | type=click.Choice(['local', 'remote']), | ||||
help="Scheduler's class, default to 'local'") | help="Scheduler's class, default to 'local'") | ||||
@click.option('--database', '-d', | @click.option('--database', '-d', | ||||
help="Scheduling database DSN (if cls is 'local')") | help="Scheduling database DSN (if cls is 'local')") | ||||
@click.option('--url', '-u', | @click.option('--url', '-u', | ||||
help="Scheduler's url access (if cls is 'remote')") | help="Scheduler's url access (if cls is 'remote')") | ||||
Show All 29 Lines | except Exception: | ||||
pass | pass | ||||
ctx.obj['scheduler'] = scheduler | ctx.obj['scheduler'] = scheduler | ||||
ctx.obj['config'] = {'cls': cls, 'args': override_config} | ctx.obj['config'] = {'cls': cls, 'args': override_config} | ||||
ctx.obj['loglevel'] = log_level | ctx.obj['loglevel'] = log_level | ||||
@cli.group('task') | @cli.group('task') | ||||
@click.option('--list-types', '-l', is_flag=True, default=False, is_eager=True, | |||||
expose_value=False, callback=list_task_types) | |||||
@click.pass_context | @click.pass_context | ||||
def task(ctx): | def task(ctx): | ||||
"""Manipulate tasks.""" | """Manipulate tasks.""" | ||||
pass | pass | ||||
@task.command('schedule') | @task.command('schedule') | ||||
@click.option('--columns', '-c', multiple=True, | @click.option('--columns', '-c', multiple=True, | ||||
▲ Show 20 Lines • Show All 319 Lines • ▼ Show 20 Lines | if ctx.obj['config']['args']: | ||||
conf['scheduler']['args'].update(ctx.obj['config']['args']) | conf['scheduler']['args'].update(ctx.obj['config']['args']) | ||||
app.config.update(conf) | app.config.update(conf) | ||||
if debug is None: | if debug is None: | ||||
debug = ctx.obj['loglevel'] <= logging.DEBUG | debug = ctx.obj['loglevel'] <= logging.DEBUG | ||||
app.run(host, port=port, debug=bool(debug)) | app.run(host, port=port, debug=bool(debug)) | ||||
@cli.group('task-type') | |||||
@click.pass_context | |||||
def task_type(ctx): | |||||
"""Manipulate task types.""" | |||||
pass | |||||
@task_type.command('list') | |||||
@click.option('--verbose', '-v', is_flag=True, default=False) | |||||
@click.option('--task_type', '-t', multiple=True, default=None, | |||||
help='List task types of given type') | |||||
@click.option('--task_name', '-n', multiple=True, default=None, | |||||
help='List task types of given backend task name') | |||||
@click.pass_context | |||||
def list_task_types(ctx, verbose, task_type, task_name): | |||||
click.echo("Known task types:") | |||||
if verbose: | |||||
tmpl = click.style('{type}: ', bold=True) + '''{backend_name} | |||||
{description} | |||||
interval: {default_interval} [{min_interval}, {max_interval}] | |||||
backoff_factor: {backoff_factor} | |||||
max_queue_length: {max_queue_length} | |||||
num_retries: {num_retries} | |||||
retry_delay: {retry_delay} | |||||
''' | |||||
else: | |||||
tmpl = '{type}:\n {description}' | |||||
for tasktype in ctx.obj['scheduler'].get_task_types(): | |||||
if task_type and tasktype['type'] not in task_type: | |||||
continue | |||||
if task_name and tasktype['backend_name'] not in task_name: | |||||
continue | |||||
click.echo(tmpl.format(**tasktype)) | |||||
@task_type.command('add') | |||||
@click.argument('type', required=1) | |||||
@click.argument('task-name', required=1) | |||||
@click.argument('description', required=1) | |||||
@click.option('--default-interval', '-i', default='90 days', | |||||
help='Default interval ("90 days" by default)') | |||||
@click.option('--min-interval', default=None, | |||||
help='Minimum interval (default interval if not set)') | |||||
@click.option('--max-interval', '-i', default=None, | |||||
help='Maximal interval (default interval if not set)') | |||||
@click.option('--backoff-factor', '-f', type=float, default=1, | |||||
help='Backoff factor') | |||||
@click.pass_context | |||||
def add_task_type(ctx, type, task_name, description, | |||||
default_interval, min_interval, max_interval, | |||||
backoff_factor): | |||||
"""Create a new task type | |||||
""" | |||||
scheduler = ctx.obj['scheduler'] | |||||
if not scheduler: | |||||
raise ValueError('Scheduler class (local/remote) must be instantiated') | |||||
task_type = dict( | |||||
type=type, | |||||
backend_name=task_name, | |||||
description=description, | |||||
default_interval=default_interval, | |||||
min_interval=min_interval, | |||||
max_interval=max_interval, | |||||
backoff_factor=backoff_factor, | |||||
max_queue_length=None, | |||||
num_retries=None, | |||||
retry_delay=None, | |||||
) | |||||
scheduler.create_task_type(task_type) | |||||
click.echo('OK') | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
cli() | cli() |