def run(self):
        # sleep for average * ODC ms
        with session_scope() as db_session:
            average = get_avg_duration(
                db_session,
                self._endpoint.id) * config.outlier_detection_constant
        time.sleep(average / 1000)
        if not self._stopped:
            stack_list = []
            try:
                frame = sys._current_frames()[self._current_thread]
            except KeyError:
                log('Can\'t get the stacktrace of the main thread.')
                return
            in_endpoint_code = False
            # filename, line number, function name, source code line
            for fn, ln, fun, line in traceback.extract_stack(frame):
                if self._endpoint.name == fun:
                    in_endpoint_code = True
                if in_endpoint_code:
                    stack_list.append(
                        'File: "{}", line {}, in "{}": "{}"'.format(
                            fn, ln, fun, line))

            # Set the values in the object
            self._stacktrace = '<br />'.join(stack_list)
            self._cpu_percent = str(
                psutil.cpu_percent(interval=None, percpu=True))
            self._memory = str(psutil.virtual_memory())
Example #2
0
 def test_get_avg_duration(self):
     with session_scope() as db_session:
         self.assertEqual(get_avg_duration(db_session, ENDPOINT_ID), 12000)