Esempio n. 1
0
    def start(self):
        # get global IOLoop
        self.ioloop = tornado.ioloop.IOLoop.instance()

        self._exit_checker = tornado.ioloop.PeriodicCallback(self._check_exit, 1000, self.ioloop)
        self._exit_checker.start()

        self.api_pool = ApiPool(num_workers=10, ioloop=self.ioloop, config=self.config)
        self.api_pool.start()

        self.heartbeat_data = HeartbeatData(ioloop=self.ioloop, config=self.config)
        self.heartbeat = Heartbeat(self.heartbeat_data)
        self.heartbeat.start()

        routes = [
            (r'/static/(.*)', StaticHandler, {"path": STATIC_PATH}),
            (r'/(?:ws|socket.io)', WebSocketHandler, {"api_pool": self.api_pool, "heartbeat": self.heartbeat_data}),
            (r'/()(?:.*)', IndexHandler, {"config": self.config})
        ]

        app = tornado.web.Application(
            routes,
            template_path=TEMPLATE_PATH,
            static_path=STATIC_PATH
        )

        self.server = tornado.httpserver.HTTPServer(app)
        self.server.listen(address='0.0.0.0', port=self.options.port)

        logger.info("Running server at %s:%s" % (self.options.host, self.options.port))

        try:
            self.ioloop.start()
        except KeyboardInterrupt:
            pass

        self.cleanup()
Esempio n. 2
0
class WebServer(Command):

    @staticmethod
    def configure(parser, subparsers):
        my_parser = subparsers.add_parser('web', help='Run the eklhad web server')
        my_parser.set_defaults(CommandClass=WebServer)
        my_parser.add_argument('--host', type=str, help='Hostname of where to run the app', default='localhost')
        my_parser.add_argument('--port', type=int, help='Port of where to run the app', default=7000)

    def __init__(self, options, config, external_start=False, skip_signal_handler=False, exit_lock=None):
        self._exit_lock = exit_lock or threading.Lock()
        self.options = options
        self.config = config

        if not skip_signal_handler:
            signal.signal(signal.SIGINT, self.stop)
            signal.signal(signal.SIGTERM, self.stop)

        if not external_start:
            self.start()

    def start(self):
        # get global IOLoop
        self.ioloop = tornado.ioloop.IOLoop.instance()

        self._exit_checker = tornado.ioloop.PeriodicCallback(self._check_exit, 1000, self.ioloop)
        self._exit_checker.start()

        self.api_pool = ApiPool(num_workers=10, ioloop=self.ioloop, config=self.config)
        self.api_pool.start()

        self.heartbeat_data = HeartbeatData(ioloop=self.ioloop, config=self.config)
        self.heartbeat = Heartbeat(self.heartbeat_data)
        self.heartbeat.start()

        routes = [
            (r'/static/(.*)', StaticHandler, {"path": STATIC_PATH}),
            (r'/(?:ws|socket.io)', WebSocketHandler, {"api_pool": self.api_pool, "heartbeat": self.heartbeat_data}),
            (r'/()(?:.*)', IndexHandler, {"config": self.config})
        ]

        app = tornado.web.Application(
            routes,
            template_path=TEMPLATE_PATH,
            static_path=STATIC_PATH
        )

        self.server = tornado.httpserver.HTTPServer(app)
        self.server.listen(address='0.0.0.0', port=self.options.port)

        logger.info("Running server at %s:%s" % (self.options.host, self.options.port))

        try:
            self.ioloop.start()
        except KeyboardInterrupt:
            pass

        self.cleanup()

    def _check_exit(self):
        success = self._exit_lock.acquire(False)
        if success:
            self._exit_lock.release()
        else:
            self._exit_checker.stop()
            self.ioloop.stop()

    def cleanup(self):
        logger.info("Exiting")
        self.api_pool.terminate()
        self.ioloop.close()

    def stop(self, *args):
        self._exit_lock.acquire()