from os.path import join, dirname
import logging.config
import click
import yaml
import jsonschema
from .daemon import run_daemon
from .config import load_config
[docs]def setup_logging(debug=False):
logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {"brief": {"format": "%(levelname)s %(name)s: %(message)s"}},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG" if debug else "INFO",
"formatter": "brief",
}
},
"root": {
"handlers": ["console"],
"level": "DEBUG" if debug else "INFO",
},
}
)
[docs]def validate_config(config):
with open(join(dirname(__file__), "config-schema.yaml")) as f:
schema = yaml.load(f)
jsonschema.validate(config, schema)
@click.group()
def cli():
pass
@cli.command(help="Run the scheduler daemon, attaching to a Redis db")
@click.option("--config-file", type=click.File("r"))
@click.option("--validate/--no-validate", default=False)
@click.option("--host", type=str)
@click.option("--port", type=int)
@click.option("--debug/--no-debug", default=False)
def daemon(config_file=None, validate=False, host=None, port=None, debug=False):
setup_logging(debug)
config = load_config(config_file)
if validate:
validate_config(config)
run_daemon(config, host, port)
if __name__ == "__main__":
cli()