#!/usr/bin/env python

import logging
from sys import argv, exit
import socket

import click
from distributed import  Scheduler, sync
from distributed.utils import get_ip
from tornado.ioloop import IOLoop

logger = logging.getLogger('distributed.scheduler')

ip = get_ip()


@click.command()
@click.argument('center', type=str, default='')
@click.option('--port', type=int, default=8786, help="Serving port")
def go(center, port):
    while True:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.bind(('127.0.0.1', port))
        except socket.error:
            port += 1
        else:
            sock.close()
            break

    loop = IOLoop.current()
    scheduler = Scheduler(center)
    if center:
        loop.run_sync(scheduler.sync_center)
    done = scheduler.start()
    scheduler.listen(port)

    try:
        logger.info("Start Scheduler at %s:%s", ip, port)
        loop.start()
    except KeyboardInterrupt:
        logger.info("End scheduler at %s:%d", ip, port)

if __name__ == '__main__':
    go()
