def run(self):
        def sigterm():
            logger.info('Starting SIGTERM')
            self.scheduler.shutdown(True)

        gevent_signal(signal.SIGTERM, sigterm)

        def sigusr1():
            logger.info('Starting SIGUSR1')
            self.scheduler.shutdown()

        gevent_signal(signal.SIGUSR1, sigusr1)

        logger.info('Starting node: {}'.format(self.server_name))

        for auction_item in iterview(self.config['main']["couch_url"],
                                     self.config['main']['auctions_db'],
                                     'chronograph/start_date'):
            datestamp = (datetime.now(self.timezone) +
                         timedelta(minutes=1)).isoformat()

            # ADD FILTER BY VALUE {start: '2016-09-10T14:36:40.378777+03:00', test: false}
            if datestamp < auction_item['value']['start']:
                self.scheduler.schedule_auction(auction_item['id'],
                                                auction_item['value'])

            if self.scheduler.exit:
                break

        while not self.scheduler.execution_stopped:
            sleep(10)
            logger.info('Wait until execution stopped')
Ejemplo n.º 2
0
    def __init__(self, listener, application=None, backlog=None,
                 spawn='default', log='default', handler_class=None,
                 environ=None, socket_type=None, address_family=None,
                 graceful_timeout=None, disable_monkeypatch=False,
                 **ssl_args):
        if address_family:
            self.address_family = address_family
        if socket_type:
            self.socket_type = socket_type
        if handler_class:
            self.handler_class = handler_class

        host, port = listener
        self.socket = create_socket(host, port, self.address_family,
                                    self.socket_type, backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()

        if graceful_timeout is not None:
            self.stop_timeout = graceful_timeout
            gevent_signal(signal.SIGTERM, self.stop)

        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, self.handler_class, environ,
                                     **ssl_args)
Ejemplo n.º 3
0
    def __init__(self,
                 listener,
                 application=None,
                 backlog=None,
                 spawn='default',
                 log='default',
                 handler_class=None,
                 environ=None,
                 socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET,
                 graceful_timeout=None,
                 **ssl_args):
        host, port = listener
        self.socket = create_socket(host,
                                    port,
                                    self.address_family,
                                    self.socket_type,
                                    backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()

        if graceful_timeout is not None:
            self.stop_timeout = graceful_timeout
            gevent_signal(signal.SIGTERM, self.stop)

        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, handler_class, environ, **ssl_args)
Ejemplo n.º 4
0
    def _run(self):
        self.m2m_app.run(self, self.m2m_ep)

        _server = self._get_server()
        self.logger.debug("Serving on %s:%s", self.listen_on, self.port)
        gevent_signal(SIGTERM, _server.stop)
        gevent_signal(SIGINT, _server.stop)
        _server.serve_forever()
Ejemplo n.º 5
0
def run_game(game):

    def sigterm_handler(*args, **kwargs):
        if game.state == Game.STATE_PLAYING:
            game.state = Game.STATE_READY
            game.save()
        _log('Handled SIGTERM for %s' % game)
        sys.exit(0)

    gevent_signal(signal.SIGTERM, sigterm_handler)

    if game.state != Game.STATE_READY:
        raise Exception("Controller tried to run game that wasn't ready")

    game.state = Game.STATE_PLAYING
    game.save()

    new_game_state = None

    # We have exclusive game access now
    _log('starting game: %s' % game.id)

    while game.state != Game.STATE_DONE:
        start_time = time.time()

        # moves = fetch_moves_async
        game = game.refetch()

        if game.state == Game.STATE_PAUSED:
            _log('paused: %s' % game)
            break

        if game.state != Game.STATE_PLAYING:
            _log('aborted: %s' % new_game_state)
            break

        try:
            new_game_state = next_turn(game)
        except Exception as e:
            _log('failed to insert game state for %s: %s' % (game.id, e))
            break

        _log('finished turn: %s' % new_game_state)

        if new_game_state.is_done:
            end_game(game, new_game_state)

        else:
            # Wait at least
            elasped_time = time.time() - start_time
            sleep_for = max(0, float(game.turn_time) - elasped_time)
            _log('sleeping for %.2f: %s' % (sleep_for, new_game_state.id))
            time.sleep(sleep_for)

    _log('done: %s' % new_game_state)
Ejemplo n.º 6
0
def run_game(game):
    def sigterm_handler(*args, **kwargs):
        if game.state == Game.STATE_PLAYING:
            game.state = Game.STATE_READY
            game.save()
        _log('Handled SIGTERM for %s' % game)
        sys.exit(0)

    gevent_signal(signal.SIGTERM, sigterm_handler)

    if game.state != Game.STATE_READY:
        raise Exception("Controller tried to run game that wasn't ready")

    game.state = Game.STATE_PLAYING
    game.save()

    new_game_state = None

    # We have exclusive game access now
    _log('starting game: %s' % game.id)

    while game.state != Game.STATE_DONE:
        start_time = time.time()

        # moves = fetch_moves_async
        game = game.refetch()

        if game.state == Game.STATE_PAUSED:
            _log('paused: %s' % game)
            break

        if game.state != Game.STATE_PLAYING:
            _log('aborted: %s' % new_game_state)
            break

        try:
            new_game_state = next_turn(game)
        except Exception as e:
            _log('failed to insert game state for %s: %s' % (game.id, e))
            break

        _log('finished turn: %s' % new_game_state)

        if new_game_state.is_done:
            end_game(game, new_game_state)

        else:
            # Wait at least
            elasped_time = time.time() - start_time
            sleep_for = max(0, float(game.turn_time) - elasped_time)
            _log('sleeping for %.2f: %s' % (sleep_for, new_game_state.id))
            time.sleep(sleep_for)

    _log('done: %s' % new_game_state)
Ejemplo n.º 7
0
    def _run(self):
        if self.host == "auto":
            self._set_auto_host()

        self.base_uri = "http://%s:%s" % (self.host, self.port)
        self.m2m_app.run(self, self.m2m_ep)

        _server = self._get_server()
        self.logger.debug("Serving on %s:%s", self.listen_on, self.port)
        gevent_signal(SIGTERM, _server.stop)
        gevent_signal(SIGINT, _server.stop)
        _server.serve_forever()
Ejemplo n.º 8
0
def run_game(game):
    def sigterm_handler(*args, **kwargs):
        if game.state == Game.STATE_PLAYING:
            game.mark_ready()
        logger.info('Handled SIGTERM for %s', game)
        sys.exit(0)

    gevent_signal(signal.SIGTERM, sigterm_handler)

    if game.state != Game.STATE_READY:
        raise Exception("Controller tried to run game that wasn't ready")

    game.state = Game.STATE_PLAYING
    game.save()

    new_game_state = None

    # We have exclusive game access now
    logger.info('Starting game: %s', game.id)

    while True:
        start_time = time.time()

        # Refresh game data
        game = game.refetch()

        if game.state == Game.STATE_PAUSED:
            logger.info('Paused game: %s', game)
            break

        if game.state != Game.STATE_PLAYING:
            logger.info('Aborted game: %s', game)
            break

        new_game_state = next_turn(game)
        logger.info('Finished turn: %s', new_game_state)

        if new_game_state.is_done:
            end_game(game, new_game_state)
            break

        # Wait at least turn_time
        elasped_time = time.time() - start_time
        sleep_for = max(0, float(game.turn_time) - elasped_time)
        logger.info('Sleeping for %.2f: %s', sleep_for, new_game_state.id)
        time.sleep(sleep_for)

    logger.info('Done: %s', new_game_state)
Ejemplo n.º 9
0
    def start(self):
        from flask import (
            Flask,
            request,
            Response,
        )
        from gevent import signal as gevent_signal
        from signal import (
            SIGTERM,
            SIGINT,
        )

        app = Flask(__name__)

        @app.after_request
        def attach_headers(response):
            response.headers['x-m2m-ri'] = request.headers['x-m2m-ri']
            return response

        @app.route('/', methods=['POST'])
        def index():
            assert 'x-m2m-origin' in request.headers, 'No originator set'
            assert 'x-m2m-ri' in request.headers, 'Missing request id'
            assert 'content-type' in request.headers, 'Unspecified content type'

            notification = self._unpack_notification(
                get_onem2m_decoder(request.content_type).decode(request.data))
            self._callback(request.headers['x-m2m-origin'], **notification)

            return Response(headers={
                'x-m2m-rsc': 2000,
            }, )

        if self._endpoint.scheme == 'https':
            self.server = WSGIServer(
                (self._endpoint.hostname, self._endpoint.port or 6050),
                application=app,
                keyfile=self.key_file,
                certfile=self.cert_file,
                ca_certs=self.ca_certs)
        else:
            self.server = WSGIServer(
                (self._endpoint.hostname, self._endpoint.port or 6050),
                application=app,
            )
        gevent_signal(SIGINT, self.server.stop)
        gevent_signal(SIGTERM, self.server.stop)
        spawn(self.server.serve_forever)
Ejemplo n.º 10
0
    def __init__(self, listener, application=None, backlog=None,
                 spawn='default', log='default', handler_class=None,
                 environ=None, socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET, graceful_timeout=None,
                 **ssl_args):
        self.address_family = address_family
        self.socket_type = socket_type

        host, port = listener
        self.handler_class = WebSocketHandler
        self.socket = create_socket(host, port, self.address_family,
                                    self.socket_type, backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()

        if graceful_timeout is not None:
            self.stop_timeout = graceful_timeout
            gevent_signal(signal.SIGTERM, self.stop)

        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, handler_class, environ, **ssl_args)
Ejemplo n.º 11
0
def run():
    """ Run the workbench server """

    # Load the configuration file relative to this script location
    config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.ini')
    workbench_conf = ConfigParser.ConfigParser()
    config_ini = workbench_conf.read(config_path)
    if not config_ini:
        print 'Could not locate config.ini file, tried %s : exiting...' % config_path
        exit(1)

    # Pull configuration settings
    datastore_uri = workbench_conf.get('workbench', 'datastore_uri')
    database = workbench_conf.get('workbench', 'database')
    worker_cap = workbench_conf.getint('workbench', 'worker_cap')
    samples_cap = workbench_conf.getint('workbench', 'samples_cap')

    # Spin up Workbench ZeroRPC
    try:
        store_args = {'uri': datastore_uri, 'database': database, 'worker_cap':worker_cap, 'samples_cap':samples_cap}
        workbench = zerorpc.Server(WorkBench(store_args=store_args), name='workbench', heartbeat=60)
        workbench.bind('tcp://0.0.0.0:4242')
        print '\nWorkbench is ready and feeling super duper!'
        gevent_signal(signal.SIGTERM, workbench.stop)
        gevent_signal(signal.SIGINT, workbench.stop)
        gevent_signal(signal.SIGKILL, workbench.stop)
        workbench.run()
        print '\nWorkbench Server Shutting Down... and dreaming of sheep...'

    except zmq.error.ZMQError:
        print '\nInfo: Could not start Workbench server (no worries, probably already running...)\n'
    def run(self):

        LOGGER.info('Starting node: {}'.format(self.server_name))

        def sigterm():
            LOGGER.info('Starting SIGTERM')
            self.scheduler.shutdown(True)

        gevent_signal(signal.SIGTERM, sigterm)

        def sigusr1():
            LOGGER.info('Starting SIGUSR1')
            self.scheduler.shutdown()

        gevent_signal(signal.SIGUSR1, sigusr1)

        for auction_item in iterview(self.config['main']["couch_url"],
                                     self.config['main']['auctions_db'],
                                     'chronograph/start_date'):
            datestamp = (
                datetime.now(self.timezone) + timedelta(minutes=1)
            ).isoformat()
            # ADD FILTER BY VALUE
            # {start: '2016-09-10T14:36:40.378777+03:00', test: false}
            if datestamp < auction_item['value']['start']:
                worker_cmd_provider = \
                    self.mapper(FeedItem(auction_item['value']))
                if not worker_cmd_provider:
                    continue
                self.scheduler.schedule_auction(
                    auction_item['id'], auction_item['value'],
                    args=worker_cmd_provider(auction_item['id'])
                )

            if self.scheduler.exit:
                break

        while not self.scheduler.execution_stopped:
            sleep(10)
            LOGGER.info('Wait until execution stopped')
Ejemplo n.º 13
0
 def listen(self, address, port):
     # Language negotiation
     init_language_selector(select_language)
     # Say hello
     address = address if address is not None else '*'
     print 'Listen %s:%d' % (address, port)
     msg = 'Listing at port %s' % port
     log_info(msg)
     self.port = port
     # Say hello
     address = address if address is not None else '*'
     msg = 'Listen %s:%d' % (address, port)
     log_info(msg)
     self.port = port
     self.wsgi_server = WSGIServer(
         ('', port), application,
         handler_class=ServerHandler,
         log=self.access_log)
     gevent_signal(SIGTERM, self.stop)
     gevent_signal(SIGINT, self.stop)
     if self.profile:
         runctx("self.wsgi_server.serve_forever()", globals(), locals(), self.profile)
     else:
         self.wsgi_server.serve_forever()
Ejemplo n.º 14
0
def run():
    """ Run the workbench server """

    # Load the configuration file relative to this script location
    config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               'config.ini')
    workbench_conf = ConfigParser.ConfigParser()
    config_ini = workbench_conf.read(config_path)
    if not config_ini:
        print 'Could not locate config.ini file, tried %s : exiting...' % config_path
        exit(1)

    # Pull configuration settings
    datastore_uri = workbench_conf.get('workbench', 'datastore_uri')
    database = workbench_conf.get('workbench', 'database')
    worker_cap = workbench_conf.getint('workbench', 'worker_cap')
    samples_cap = workbench_conf.getint('workbench', 'samples_cap')

    # Spin up Workbench ZeroRPC
    try:
        store_args = {
            'uri': datastore_uri,
            'database': database,
            'worker_cap': worker_cap,
            'samples_cap': samples_cap
        }
        workbench = zerorpc.Server(WorkBench(store_args=store_args),
                                   name='workbench',
                                   heartbeat=60)
        workbench.bind('tcp://0.0.0.0:4242')
        print '\nWorkbench is ready and feeling super duper!'
        gevent_signal(signal.SIGTERM, workbench.stop)
        gevent_signal(signal.SIGINT, workbench.stop)
        gevent_signal(signal.SIGKILL, workbench.stop)
        workbench.run()
        print '\nWorkbench Server Shutting Down... and dreaming of sheep...'

    except zmq.error.ZMQError:
        print '\nInfo: Could not start Workbench server (no worries, probably already running...)\n'