예제 #1
0
    def _adapter_connect(self):
        """Connect to the remote socket, adding the socket to the IOLoop if
        connected

        :rtype: bool

        """
        LOGGER.debug('init io and signal watchers if any')
        # reuse existing signal watchers, can only be declared for 1 ioloop
        global global_sigint_watcher, global_sigterm_watcher
        error = super(LibevConnection, self)._adapter_connect()

        if not error:
            if self._on_signal_callback and not global_sigterm_watcher:
                global_sigterm_watcher = self.ioloop.signal(signal.SIGTERM,
                                                            self._handle_sigterm)

            if self._on_signal_callback and not global_sigint_watcher:
                global_sigint_watcher = self.ioloop.signal(signal.SIGINT,
                                                           self._handle_sigint)

            if not self._io_watcher:
                self._io_watcher = self.ioloop.io(self.socket.fileno(),
                                                  self._PIKA_TO_LIBEV_ARRAY[self.event_state],
                                                  self._handle_events)

            self.ioasync = pyev.Async(self.ioloop, self._noop_callable)
            self.ioasync.start()
            if self._on_signal_callback:
                global_sigterm_watcher.start()
            if self._on_signal_callback:
                global_sigint_watcher.start()
            self._io_watcher.start()

        return error
예제 #2
0
        for sock in self.cnssockets:
            sock.setblocking(0)
            # Watcher data are used (instead logical watcher.fd due to Win32 mismatch)
            self.watchers.append(
                pyev.Io(sock._sock,
                        pyev.EV_READ,
                        self.loop,
                        self.__accept_cb,
                        data=sock._sock.fileno()))

        self.conns = weakref.WeakSet()
        self.termstatus = None
        self.termstatus_change = None

        # Prepare also exit watcher - can be used to 'simulate' terminal signal (useful on Win32)
        self.exitwatcher = pyev.Async(self.loop, self.__terminal_signal_cb)
        self.exitwatcher.start()

        program_roaster.__init__(self)
        idlework_appmixin.__init__(self)

        # Build notificator component
        self.notificator = notificator(self)

        # Reopen stdout and stderr - if pointing to log file, this includes also log rotate check
        self.__rotate_stdout_stderr()

    def run(self):
        for sock in self.cnssockets:
            sock.listen(socket.SOMAXCONN)
        for watcher in self.watchers:
예제 #3
0
class httpfend_app(object):

    STOPSIGNALS = [signal.SIGINT, signal.SIGTERM]
    NONBLOCKING = frozenset([errno.EAGAIN, errno.EWOULDBLOCK])
    # Maximum number of worker threads serving the client requests
    MAX_WORKER_THREADS = 10

    def __init__(self):
        # Read config
        read_config()

        # Configure logging
        try:
            loglvl = get_numeric_loglevel(
                config.get(os.environ['RAMONA_SECTION'], 'loglevel'))
        except:
            loglvl = logging.INFO
        logging.basicConfig(
            level=loglvl,
            stream=sys.stderr,
            format="%(asctime)s %(levelname)s: %(message)s",
        )

        try:
            self.listenaddr = config.get(os.environ['RAMONA_SECTION'],
                                         'listen')
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            self.listenaddr = config_defaults['ramona:httpfend']['listenaddr']

        self.username = None
        self.password = None
        try:
            self.username = config.get(os.environ['RAMONA_SECTION'],
                                       'username')
            self.password = config.get(os.environ['RAMONA_SECTION'],
                                       'password')
        except:
            pass

        if self.username is not None and self.password is None:
            L.fatal(
                "Configuration error: 'username' option is set, but 'password' option is not set. Please set 'password'"
            )
            sys.exit(1)

        self.logmsgcnt = itertools.count()
        self.logmsgs = dict()

        self.workers = collections.deque()
        self.dyingws = collections.deque()  # Dying workers

        self.svrsockets = []

        for addr in self.listenaddr.split(','):
            socket_factory = socketuri.socket_uri(addr)
            try:
                socks = socket_factory.create_socket_listen()
            except socket.error, e:
                L.fatal(
                    "It looks like that server is already running: {0}".format(
                        e))
                sys.exit(1)
            self.svrsockets.extend(socks)

        if len(self.svrsockets) == 0:
            L.fatal(
                "There is no http server listen address configured - considering this as fatal error"
            )
            sys.exit(1)

        self.loop = pyev.default_loop()
        self.watchers = [
            pyev.Signal(sig, self.loop, self.__terminal_signal_cb)
            for sig in self.STOPSIGNALS
        ]
        self.dyingwas = pyev.Async(
            self.loop, self.__wdied_cb)  # Dying workers async. signaling
        self.watchers.append(self.dyingwas)

        for sock in self.svrsockets:
            sock.setblocking(0)
            self.watchers.append(
                pyev.Io(sock._sock,
                        pyev.EV_READ,
                        self.loop,
                        self.__on_accept,
                        data=sock._sock.fileno()))
예제 #4
0
	def __init__(self):
		threading.Thread.__init__(self)
		self.aw = pyev.Async(default_loop, self.process)
		self.aw.start()
		self.calls = []