Exemple #1
0
    def __init__(self, app):
        self.app = app

        self._stop_waiter = Waiter(
            timeout=self.app.config.PROCESS_STOP_TIMEOUT * 2)

        super(GafferControl, self).__init__()
Exemple #2
0
class GafferControl(LoopMixin):
    """Start and stop gaffer."""

    def __init__(self, app):
        self.app = app

        self._stop_waiter = Waiter(
            timeout=self.app.config.PROCESS_STOP_TIMEOUT * 2)

        super(GafferControl, self).__init__()

    @in_loop
    def start(self):
        self.app.gaffer_manager.start()

    def stop(self):
        stop_waiter = self._stop_waiter

        @loop_delegate
        def async_stop():
            self.app.gaffer_manager.stop(
                callback=lambda *args: stop_waiter.done())

        async_stop()
        stop_waiter.wait_or_terminate('Timeout happened when stopping gaffer.')

    def abort(self):
        self._stop_waiter.abort()
Exemple #3
0
    def __init__(self, app, listeners, controller):
        self.app = app
        self.listeners = listeners
        self.controller = controller

        broker = self.broker = self.Broker(self.app)
        self.factory = self.Factory(self.app, broker,
            setup_callback=self.setup_cb, teardown_callback=self.teardown_cb)

        self._bootstrapped = {}

        self._start_waiter = Waiter(
            timeout=self.app.config.PROCESS_START_TIMEOUT)

        super(ProcessManager, self).__init__()
Exemple #4
0
class ProcessManager(ManagerMixin, LoopMixin):
    """Start and manage workers."""

    #: Which class should be used to bootstrap process.
    Factory = ProcessFactory

    #: Which class should be used to pass commands to processes.
    Broker = Broker

    #: How worker process should be named.
    name_template = '[thriftworker-{0}] -c {1.CONCURRENCY} -k {1.WORKER_TYPE}'

    def __init__(self, app, listeners, controller):
        self.app = app
        self.listeners = listeners
        self.controller = controller

        broker = self.broker = self.Broker(self.app)
        self.factory = self.Factory(self.app, broker,
            setup_callback=self.setup_cb, teardown_callback=self.teardown_cb)

        self._bootstrapped = {}

        self._start_waiter = Waiter(
            timeout=self.app.config.PROCESS_START_TIMEOUT)

        super(ProcessManager, self).__init__()

    def __len__(self):
        return len(self._bootstrapped)

    def __iter__(self):
        return iter(self._bootstrapped)

    def __getitem__(self, process_id):
        """Return registered process."""
        return self._bootstrapped.get(process_id)

    def eliminate(self, process_id):
        """Unregister and stop process."""
        process = self[process_id]
        if process is None:
            return
        self.broker.unregister(process_id)
        process.stop()

    def is_ready(self):
        """Are all workers started or not?"""
        return len(self) >= self.app.config.WORKERS

    def setup_cb(self, proxy, process):
        # Change name of process.
        name = self.name_template.format(process.pid, self.app.config)
        proxy.change_title(name)

        # Register acceptors in remote process.
        proxy.register_acceptors({i: listener.name
            for i, listener in iteritems(self.listeners.enumerated)})

        for listener in self.listeners:
            if listener.started:
                proxy.start_acceptor(listener.name)

        # Set startup time for process.
        process.startup_time = self.loop.now()

        # Notify about process initialization.
        self._bootstrapped[process.pid] = process
        logger.info('Worker %d initialized.', process.pid)
        if self.is_ready():
            self.ready_cb()

    def teardown_cb(self, pid):
        try:
            self._bootstrapped.pop(pid)
        except KeyError:
            pass

    def ready_cb(self, *args):
        logger.info('Workers initialization done.')
        self._start_waiter.done()

    def start(self):

        @loop_delegate
        def async_start():
            self.factory.setup(self.listeners.channels)

        async_start()
        self._start_waiter.wait_or_terminate(
            'Timeout happened when starting processes.')

    @in_loop
    def stop(self):
        self.factory.teardown()

    def abort(self):
        self._start_waiter.abort()