From 3bd0edce5f6faa138da834fda6829d4144595bc1 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Thu, 30 Sep 2021 12:00:30 -0400 Subject: [PATCH] Update monitoring daemon example --- .../monitoringdaemon/__main__.py | 8 ++-- .../monitoringdaemon/dispatcher.py | 8 ++-- .../monitoringdaemon/monitors.py | 18 ++++----- .../asyncio-daemon/monitoringdaemon/tests.py | 38 +++++++++---------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/examples/miniapps/asyncio-daemon/monitoringdaemon/__main__.py b/examples/miniapps/asyncio-daemon/monitoringdaemon/__main__.py index c15757ad..42669394 100644 --- a/examples/miniapps/asyncio-daemon/monitoringdaemon/__main__.py +++ b/examples/miniapps/asyncio-daemon/monitoringdaemon/__main__.py @@ -1,7 +1,5 @@ """Main module.""" -import sys - from dependency_injector.wiring import inject, Provide from .dispatcher import Dispatcher @@ -13,10 +11,10 @@ def main(dispatcher: Dispatcher = Provide[Container.dispatcher]) -> None: dispatcher.run() -if __name__ == '__main__': +if __name__ == "__main__": container = Container() - container.config.from_yaml('config.yml') + container.config.from_yaml("config.yml") container.init_resources() - container.wire(modules=[sys.modules[__name__]]) + container.wire(modules=[__name__]) main() diff --git a/examples/miniapps/asyncio-daemon/monitoringdaemon/dispatcher.py b/examples/miniapps/asyncio-daemon/monitoringdaemon/dispatcher.py index e3d00118..8f3d0aad 100644 --- a/examples/miniapps/asyncio-daemon/monitoringdaemon/dispatcher.py +++ b/examples/miniapps/asyncio-daemon/monitoringdaemon/dispatcher.py @@ -21,7 +21,7 @@ class Dispatcher: asyncio.run(self.start()) async def start(self) -> None: - self._logger.info('Starting up') + self._logger.info("Starting up") for monitor in self._monitors: self._monitor_tasks.append( @@ -41,11 +41,11 @@ class Dispatcher: self._stopping = True - self._logger.info('Shutting down') + self._logger.info("Shutting down") for task, monitor in zip(self._monitor_tasks, self._monitors): task.cancel() self._monitor_tasks.clear() - self._logger.info('Shutdown finished successfully') + self._logger.info("Shutdown finished successfully") @staticmethod async def _run_monitor(monitor: Monitor) -> None: @@ -61,6 +61,6 @@ class Dispatcher: except asyncio.CancelledError: break except Exception: - monitor.logger.exception('Error executing monitor check') + monitor.logger.exception("Error executing monitor check") await asyncio.sleep(_until_next(last=time_start)) diff --git a/examples/miniapps/asyncio-daemon/monitoringdaemon/monitors.py b/examples/miniapps/asyncio-daemon/monitoringdaemon/monitors.py index d814ac1c..216ebcb0 100644 --- a/examples/miniapps/asyncio-daemon/monitoringdaemon/monitors.py +++ b/examples/miniapps/asyncio-daemon/monitoringdaemon/monitors.py @@ -25,10 +25,10 @@ class HttpMonitor(Monitor): options: Dict[str, Any], ) -> None: self._client = http_client - self._method = options.pop('method') - self._url = options.pop('url') - self._timeout = options.pop('timeout') - super().__init__(check_every=options.pop('check_every')) + self._method = options.pop("method") + self._url = options.pop("url") + self._timeout = options.pop("timeout") + super().__init__(check_every=options.pop("check_every")) async def check(self) -> None: time_start = time.time() @@ -43,11 +43,11 @@ class HttpMonitor(Monitor): time_took = time_end - time_start self.logger.info( - 'Check\n' - ' %s %s\n' - ' response code: %s\n' - ' content length: %s\n' - ' request took: %s seconds', + "Check\n" + " %s %s\n" + " response code: %s\n" + " content length: %s\n" + " request took: %s seconds", self._method, self._url, response.status, diff --git a/examples/miniapps/asyncio-daemon/monitoringdaemon/tests.py b/examples/miniapps/asyncio-daemon/monitoringdaemon/tests.py index 6d118e97..b598a9a1 100644 --- a/examples/miniapps/asyncio-daemon/monitoringdaemon/tests.py +++ b/examples/miniapps/asyncio-daemon/monitoringdaemon/tests.py @@ -19,22 +19,22 @@ class RequestStub: def container(): container = Container() container.config.from_dict({ - 'log': { - 'level': 'INFO', - 'formant': '[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s', + "log": { + "level": "INFO", + "formant": "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s", }, - 'monitors': { - 'example': { - 'method': 'GET', - 'url': 'http://fake-example.com', - 'timeout': 1, - 'check_every': 1, + "monitors": { + "example": { + "method": "GET", + "url": "http://fake-example.com", + "timeout": 1, + "check_every": 1, }, - 'httpbin': { - 'method': 'GET', - 'url': 'https://fake-httpbin.org/get', - 'timeout': 1, - 'check_every': 1, + "httpbin": { + "method": "GET", + "url": "https://fake-httpbin.org/get", + "timeout": 1, + "check_every": 1, }, }, }) @@ -43,7 +43,7 @@ def container(): @pytest.mark.asyncio async def test_example_monitor(container, caplog): - caplog.set_level('INFO') + caplog.set_level("INFO") http_client_mock = mock.AsyncMock() http_client_mock.request.return_value = RequestStub( @@ -55,14 +55,14 @@ async def test_example_monitor(container, caplog): example_monitor = container.example_monitor() await example_monitor.check() - assert 'http://fake-example.com' in caplog.text - assert 'response code: 200' in caplog.text - assert 'content length: 635' in caplog.text + assert "http://fake-example.com" in caplog.text + assert "response code: 200" in caplog.text + assert "content length: 635" in caplog.text @pytest.mark.asyncio async def test_dispatcher(container, caplog, event_loop): - caplog.set_level('INFO') + caplog.set_level("INFO") example_monitor_mock = mock.AsyncMock() httpbin_monitor_mock = mock.AsyncMock()