Example #1
0
    def run(self):
        self._run_all_sensors()

        success_exception_cls = concurrency.get_greenlet_exit_exception_class()

        try:
            while not self._stopped:
                # Poll for all running processes
                sensor_ids = list(self._sensors.keys())

                if len(sensor_ids) >= 1:
                    LOG.debug('%d active sensor(s)' % (len(sensor_ids)))
                    self._poll_sensors_for_results(sensor_ids)
                else:
                    LOG.debug('No active sensors')

                concurrency.sleep(self._poll_interval)
        except success_exception_cls:
            # This exception is thrown when sensor container manager
            # kills the thread which runs process container. Not sure
            # if this is the best thing to do.
            self._stopped = True
            return SUCCESS_EXIT_CODE
        except:
            LOG.exception('Container failed to run sensors.')
            self._stopped = True
            return FAILURE_EXIT_CODE

        self._stopped = True
        LOG.error('Process container stopped.')

        exit_code = self._exit_code or SUCCESS_EXIT_CODE
        return exit_code
Example #2
0
def make_read_and_store_stream_func(execution_db, action_db, store_data_func):
    """
    Factory function which returns a function for reading from a stream (stdout / stderr).

    This function writes read data into a buffer and stores it in a database.
    """
    # NOTE: This import has intentionally been moved here to avoid massive performance overhead
    # (1+ second) for other functions inside this module which don't need to use those imports.
    from st2common.util import concurrency

    greenlet_exit_exc_cls = concurrency.get_greenlet_exit_exception_class()

    def read_and_store_stream(stream, buff):
        try:
            while not stream.closed:
                line = stream.readline()
                if not line:
                    break

                if isinstance(line, six.binary_type):
                    line = line.decode('utf-8')

                buff.write(line)

                # Filter out result delimiter lines
                if ACTION_OUTPUT_RESULT_DELIMITER in line:
                    continue

                if cfg.CONF.actionrunner.stream_output:
                    store_data_func(execution_db=execution_db,
                                    action_db=action_db,
                                    data=line)
        except RuntimeError:
            # process was terminated abruptly
            pass
        except greenlet_exit_exc_cls:
            # Green thread exited / was killed
            pass

    return read_and_store_stream
Example #3
0
    def run(self):
        self._running = True

        self._register_signal_handlers()

        # Wait a couple of seconds before performing initial collection to prevent thundering herd
        # effect when restarting multiple services at the same time
        jitter_seconds = random.uniform(0, 3)
        concurrency.sleep(jitter_seconds)

        success_exception_cls = concurrency.get_greenlet_exit_exception_class()

        try:
            self._main_loop()
        except success_exception_cls:
            self._running = False
            return SUCCESS_EXIT_CODE
        except Exception as e:
            LOG.exception("Exception in the garbage collector: %s" % (six.text_type(e)))
            self._running = False
            return FAILURE_EXIT_CODE

        return SUCCESS_EXIT_CODE