Beispiel #1
0
    def stop(self):
        if self.stopped:
            return

        logger.info('stopping FriServer...')
        self.__conn_handler_thread.stop()
        sock = None
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(1.0)

            if self.hostname == '0.0.0.0':
                hostname = '127.0.0.1'
            else:
                hostname = self.hostname

            sock.connect((hostname, self.port))
        except socket.error:
            pass
        finally:
            if sock:
                sock.close()
                del sock

        self.__conn_handler_thread.join()
        self.stopped = True
        self.workers_manager.stop()
        logger.info('FriServer is stopped!')
Beispiel #2
0
    def run(self):
        logger.info('Starting connection handler thread...')
        self.__bind_socket()
        logger.info('Connection handler thread started!')

        while True:
            try:
                (sock, addr) = self.sock.accept()
            except Exception, err:
                logger.write = logger.debug
                traceback.print_exc(file=logger)
                logger.error('[FriConnectionHandler.accept crash] %s'%err)
                break

            try:
                if self.stopped.is_set():
                    sock.close()
                    break

                if self.need_reduce:
                    sock = reduce_handle(sock.fileno())

                self.queue.put(sock)
            except Exception, err:
                logger.write = logger.debug
                traceback.print_exc(file=logger)
                logger.error('[FriConnectionHandler.run] %s'%err)
Beispiel #3
0
    def __init__(self, hostname, port, home_dir, node_name='anonymous_node',
                    ks_path=None, ks_passwd=None, node_type=None,
                    bind_host='0.0.0.0', config={}):
        self.hostname = hostname
        self.bind_host = bind_host
        self.port = port
        self.home_dir = home_dir
        self.node_name = node_name
        self.node_type = node_type
        self.config = config
        self.__auth_key = hashlib.sha1(str(uuid.uuid4())).hexdigest()

        config_file = os.path.join(home_dir, CONFIG_FILE_NAME)
        Config.load(config_file)
        self.config.update(Config.get_config_dict())

        if ks_path:
            self.keystore = init_keystore(ks_path, ks_passwd)
            ca_files = self.keystore.autodetect_ca(home_dir)
            if ca_files:
                logger.info('CA certificates detected at: %s'%(' '.join(ca_files),))
            else:
                logger.warning('No one CA certificate autodetected')
        else:
            self.keystore = None

        self.server = None
        self.operator_process = None
        self.osproc_stat = None
        cur_thread = threading.current_thread()
        cur_thread.setName('%s-main'%self.node_name)

        self.operators_map = {'base': Operator}
        self.operators_map.update( PluginsManager.get_operators() )
Beispiel #4
0
    def start(self):
        self.stopped = False

        self.workers_manager.start_carefully()
        self.__conn_handler_thread.start()

        while self.__conn_handler_thread.status == S_PENDING:
            time.sleep(.1)

        if self.__conn_handler_thread.status == S_ERROR:
            self.stop()
            logger.error('FriServer does not started!')
            return False
        else:
            logger.info('FriServer is started!')
            return True
Beispiel #5
0
    def run(self):
        self.before_start()
        logger.info('worker is started!')
        while True:
            data = self.__queue.get()
            if data == STOP_WORKER_EVENT:
                break

            self.__busy_flag.set()
            try:
                self.worker_routine(data)
            except Exception, err:
                logger.error(err)
                logger.write = logger.debug
                traceback.print_exc(file=logger)
            finally:
Beispiel #6
0
    def stop(self):
        logger.info('stopping workers manager for %s...'%self.worker_class.__name__)
        self.stopped.set()
        self.__lock.acquire()
        try:
            act_count = 0
            for worker in self.__workers:
                if worker.is_alive():
                    act_count += 1

            for i in xrange(act_count):
                self.queue.put(STOP_WORKER_EVENT)

            for worker in self.__workers:
                if worker.is_alive():
                    worker.join()
        except Exception, err:
            logger.error('stopping error: %s'%err)
Beispiel #7
0
    def run(self):
        cur_thread = threading.current_thread()
        cur_thread.setName(self.__name)

        self.before_start()

        logger.info('worker is started!')
        while True:
            data = self.__queue.get()
            if data == STOP_WORKER_EVENT:
                break

            self.__is_busy.value = True
            try:
                self.worker_routine(data)
            except Exception, err:
                logger.error(err)
                logger.write = logger.debug
                traceback.print_exc(file=logger)
            finally:
Beispiel #8
0
                if worker.is_alive():
                    act_count += 1

            for i in xrange(act_count):
                self.queue.put(STOP_WORKER_EVENT)

            for worker in self.__workers:
                if worker.is_alive():
                    worker.join()
        except Exception, err:
            logger.error('stopping error: %s'%err)
        finally:
            self.__lock.release()

        self.join()
        logger.info('workers manager for %s is stopped!'%self.worker_class.__name__)


    def start_carefully(self):
        self.start()
        while True:
            self.__lock.acquire()
            try:
                if self.__status != S_PENDING:
                    break
            finally:
                self.__lock.release()

            time.sleep(.1)

        if self.__status == S_ERROR:
Beispiel #9
0
                logger.write = logger.debug
                traceback.print_exc(file=logger)
                logger.error('[FriConnectionHandler.accept crash] %s'%err)
                break

            try:
                if self.stopped.is_set():
                    sock.close()
                    break

                if self.need_reduce:
                    sock = reduce_handle(sock.fileno())

                self.queue.put(sock)
            except Exception, err:
                logger.write = logger.debug
                traceback.print_exc(file=logger)
                logger.error('[FriConnectionHandler.run] %s'%err)

        if self.sock:
            self.sock.shutdown(socket.SHUT_RDWR)
            self.sock.close()
            del self.sock

        logger.info('Connection handler thread stopped!')

    def stop(self):
        self.stopped.set()


Beispiel #10
0
            if data == STOP_WORKER_EVENT:
                break

            self.__busy_flag.set()
            try:
                self.worker_routine(data)
            except Exception, err:
                logger.error(err)
                logger.write = logger.debug
                traceback.print_exc(file=logger)
            finally:
                #self.__queue.task_done()
                self.__busy_flag.clear()

        self.after_stop()
        logger.info('worker is stopped!')

    def worker_routine(self, data):
        """This method must be implemented in inherited class"""
        raise RuntimeError('Not implemented')

    def before_start(self):
        """This method can be implemented in inherited class"""
        pass

    def after_stop(self):
        """This method can be implemented in inherited class"""
        pass


class ProcessBasedAbstractWorker(mp.Process):