#!/usr/bin/env python

import logging
import os
import signal

import docopt

from qw.manager import Manager
from qw import logging_setup
from qw import __version__


__doc__ = """Usage:
  qw-manager [--level=<log-level>] [--workers=<num-workers>] [--name=<name>] [--host=<host>] [--port=<port>] [--db=<db>] <target>
  qw-manager (--help | --version)

Options:
  --help                       Show this help message
  --version                    Show version information
  -l --level=<log-level>       Set the log level (debug,info,warn,error) [default: info]
  -w --workers=<num-workers>   Set the number of workers to start, defaults to number of cpus
  -n --name=<name>             Set the manager name, defaults to hostname
  -h --host=<host>             Set the redis host to use [default: localhost]
  -p --port=<port>             Set the redis port to use [default: 6379]
  -d --db=<db>                 Set the redis db number to use [default: 0]
"""

arguments = docopt.docopt(__doc__, version="qw-manager %s" % (__version__, ))

log_level = arguments["--level"].upper()

logging_setup.manager_logger.setLevel(log_level)
logging_setup.worker_logger.setLevel(log_level)

manager = Manager(
    arguments["<target>"], name=arguments["--name"], num_workers=arguments["--workers"],
    host=arguments["--host"], port=arguments["--port"], db=arguments["--db"]
)

manager.start()

def stop_handler(signum, frame):
    manager.stop()

signal.signal(signal.SIGTERM, stop_handler)
signal.signal(signal.SIGHUP, stop_handler)

try:
    manager.join()
except KeyboardInterrupt:
    manager.stop()
