Example #1
0
class Server(object):
    def __init__(self, config, options):
        self.config = config
        self.options = options
        self.name = self.options.name
        if self.name is None:
            self.procid = self.name = uuid.uuid4()
        else:
            self.procid = "%s-%s" % (self.name, uuid.uuid4())
        self.server_config = self.get_config_section("server")
        self.components = {}
        self.broker = Broker(self.name, self.procid)
        self.wsgiservers = {}
        # concurrency = 4
        # if self.config.has_option('server', 'mq_concurrency'):
        # concurrency = self.config.getint('server', 'mq_concurrency')
        # self._pool = pool.Pool(max_size=concurrency)
        self._setupLogging()

    def get_config_section(self, section):
        name = self.name
        return get_config_section_for_name(self.config, section, name)

    def start(self, start_listeners=True):
        self.log("Server", "info", 'starting server "%s" (%s)' % (self.name, self.procid))
        # setup ipc connections
        blisten = self.server_config.get("broker_listen", "")
        for i in blisten.split(","):
            if i:
                host, _, port = i.partition(":")
                port = int(port)
                self.broker.connections.listen((host, port))
        conns = self.get_config_section("connections")
        for k, v in conns:
            host, _, port = v.partition(":")
            port = int(port)
            self.broker.connections.connect((host, port), target=k)
        self.broker.start()

        # wsgi server...
        self.wsgi = WSGIServer(self, self.name, config=self.get_config_section("http"))
        dirs = self.server_config.get("static_directories", None)
        if dirs is not None:
            from drivel.contrib.fileserver import StaticFileServer

            self.wsgi.app = StaticFileServer(dirs.split(","), self.wsgi.app, self)

        # components...
        components = self.get_config_section("components")
        for name in components:
            self.log("Server", "info", 'adding "%s" component to %s' % (name, self.procid))
            self.components[name] = components.import_(name)(self, name)

        # start everything listening...
        if start_listeners and "backdoor_port" in self.config.server:
            # enable backdoor console
            bdport = self.server_config.getint("backdoor_port")
            self.log("Server", "info", "enabling backdoor on port %s" % bdport)
            eventlet.spawn(
                backdoor.backdoor_server,
                listen(("127.0.0.1", bdport)),
                locals={
                    "server": self,
                    "debug": debug,
                    "exit": safe_exit(),
                    "quit": safe_exit(),
                    "stats": lambda: pprint.pprint(self.stats()),
                },
            )

        if start_listeners and self.server_config.getboolean("start_www", True):
            self.wsgi.start(listen=listen)
        if start_listeners:
            try:
                hubs.get_hub().switch()
            except KeyboardInterrupt, e:
                pass