Пример #1
0
 async def run(self, server):
     event_pb2_grpc.add_ReportEventServiceServicer_to_server(self, server)
     self._monitor = monitor_events(
         self._event_dir,
         lambda data: self._update_events(parse_event_strings(data)),
         source_types=event_consts.EVENT_HEAD_MONITOR_SOURCE_TYPES,
     )
Пример #2
0
 async def run(self, server):
     # Connect to dashboard.
     self._stub = await self._connect_to_dashboard()
     # Start monitor task.
     self._monitor = monitor_events(
         self._event_dir,
         lambda data: create_task(self._cached_events.put(data)),
         source_types=event_consts.EVENT_AGENT_MONITOR_SOURCE_TYPES)
     # Start reporting events.
     await self.report_events()
Пример #3
0
async def test_monitor_events():
    with tempfile.TemporaryDirectory() as temp_dir:
        common = event_pb2.Event.SourceType.Name(event_pb2.Event.COMMON)
        common_log = os.path.join(temp_dir, f"event_{common}.log")
        test_logger = _test_logger(__name__ + str(random.random()),
                                   common_log,
                                   max_bytes=10,
                                   backup_count=10)
        test_events1 = []
        monitor_task = monitor_events(temp_dir,
                                      lambda x: test_events1.extend(x),
                                      scan_interval_seconds=0.01)
        assert not monitor_task.done()
        count = 10

        async def _writer(*args, read_events, spin=True):
            for x in range(*args):
                test_logger.info("%s", x)
                if spin:
                    while str(x) not in read_events:
                        await asyncio.sleep(0.01)

        async def _check_events(expect_events, read_events, timeout=10):
            start_time = time.time()
            while True:
                sorted_events = sorted(int(i) for i in read_events)
                sorted_events = [str(i) for i in sorted_events]
                if time.time() - start_time > timeout:
                    raise TimeoutError(
                        f"Timeout, read events: {sorted_events}, "
                        f"expect events: {expect_events}")
                if len(sorted_events) == len(expect_events):
                    if sorted_events == expect_events:
                        break
                await asyncio.sleep(1)

        await asyncio.gather(
            _writer(count, read_events=test_events1),
            _check_events([str(i) for i in range(count)],
                          read_events=test_events1),
        )

        monitor_task.cancel()
        test_events2 = []
        monitor_task = monitor_events(temp_dir,
                                      lambda x: test_events2.extend(x),
                                      scan_interval_seconds=0.1)

        await _check_events([str(i) for i in range(count)],
                            read_events=test_events2)

        await _writer(count, count * 2, read_events=test_events2)
        await _check_events([str(i) for i in range(count * 2)],
                            read_events=test_events2)

        log_file_count = len(os.listdir(temp_dir))

        test_logger = _test_logger(__name__ + str(random.random()),
                                   common_log,
                                   max_bytes=1000,
                                   backup_count=10)
        assert len(os.listdir(temp_dir)) == log_file_count

        await _writer(count * 2,
                      count * 3,
                      spin=False,
                      read_events=test_events2)
        await _check_events([str(i) for i in range(count * 3)],
                            read_events=test_events2)
        await _writer(count * 3,
                      count * 4,
                      spin=False,
                      read_events=test_events2)
        await _check_events([str(i) for i in range(count * 4)],
                            read_events=test_events2)

        # Test cancel monitor task.
        monitor_task.cancel()
        with pytest.raises(asyncio.CancelledError):
            await monitor_task
        assert monitor_task.done()

        assert len(
            os.listdir(temp_dir)) > 1, "Event log should have rollovers."