mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
Update monitoring daemon example
This commit is contained in:
parent
d3d2e70079
commit
3bd0edce5f
|
@ -1,7 +1,5 @@
|
||||||
"""Main module."""
|
"""Main module."""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from dependency_injector.wiring import inject, Provide
|
from dependency_injector.wiring import inject, Provide
|
||||||
|
|
||||||
from .dispatcher import Dispatcher
|
from .dispatcher import Dispatcher
|
||||||
|
@ -13,10 +11,10 @@ def main(dispatcher: Dispatcher = Provide[Container.dispatcher]) -> None:
|
||||||
dispatcher.run()
|
dispatcher.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
container = Container()
|
container = Container()
|
||||||
container.config.from_yaml('config.yml')
|
container.config.from_yaml("config.yml")
|
||||||
container.init_resources()
|
container.init_resources()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Dispatcher:
|
||||||
asyncio.run(self.start())
|
asyncio.run(self.start())
|
||||||
|
|
||||||
async def start(self) -> None:
|
async def start(self) -> None:
|
||||||
self._logger.info('Starting up')
|
self._logger.info("Starting up")
|
||||||
|
|
||||||
for monitor in self._monitors:
|
for monitor in self._monitors:
|
||||||
self._monitor_tasks.append(
|
self._monitor_tasks.append(
|
||||||
|
@ -41,11 +41,11 @@ class Dispatcher:
|
||||||
|
|
||||||
self._stopping = True
|
self._stopping = True
|
||||||
|
|
||||||
self._logger.info('Shutting down')
|
self._logger.info("Shutting down")
|
||||||
for task, monitor in zip(self._monitor_tasks, self._monitors):
|
for task, monitor in zip(self._monitor_tasks, self._monitors):
|
||||||
task.cancel()
|
task.cancel()
|
||||||
self._monitor_tasks.clear()
|
self._monitor_tasks.clear()
|
||||||
self._logger.info('Shutdown finished successfully')
|
self._logger.info("Shutdown finished successfully")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def _run_monitor(monitor: Monitor) -> None:
|
async def _run_monitor(monitor: Monitor) -> None:
|
||||||
|
@ -61,6 +61,6 @@ class Dispatcher:
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
break
|
break
|
||||||
except Exception:
|
except Exception:
|
||||||
monitor.logger.exception('Error executing monitor check')
|
monitor.logger.exception("Error executing monitor check")
|
||||||
|
|
||||||
await asyncio.sleep(_until_next(last=time_start))
|
await asyncio.sleep(_until_next(last=time_start))
|
||||||
|
|
|
@ -25,10 +25,10 @@ class HttpMonitor(Monitor):
|
||||||
options: Dict[str, Any],
|
options: Dict[str, Any],
|
||||||
) -> None:
|
) -> None:
|
||||||
self._client = http_client
|
self._client = http_client
|
||||||
self._method = options.pop('method')
|
self._method = options.pop("method")
|
||||||
self._url = options.pop('url')
|
self._url = options.pop("url")
|
||||||
self._timeout = options.pop('timeout')
|
self._timeout = options.pop("timeout")
|
||||||
super().__init__(check_every=options.pop('check_every'))
|
super().__init__(check_every=options.pop("check_every"))
|
||||||
|
|
||||||
async def check(self) -> None:
|
async def check(self) -> None:
|
||||||
time_start = time.time()
|
time_start = time.time()
|
||||||
|
@ -43,11 +43,11 @@ class HttpMonitor(Monitor):
|
||||||
time_took = time_end - time_start
|
time_took = time_end - time_start
|
||||||
|
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'Check\n'
|
"Check\n"
|
||||||
' %s %s\n'
|
" %s %s\n"
|
||||||
' response code: %s\n'
|
" response code: %s\n"
|
||||||
' content length: %s\n'
|
" content length: %s\n"
|
||||||
' request took: %s seconds',
|
" request took: %s seconds",
|
||||||
self._method,
|
self._method,
|
||||||
self._url,
|
self._url,
|
||||||
response.status,
|
response.status,
|
||||||
|
|
|
@ -19,22 +19,22 @@ class RequestStub:
|
||||||
def container():
|
def container():
|
||||||
container = Container()
|
container = Container()
|
||||||
container.config.from_dict({
|
container.config.from_dict({
|
||||||
'log': {
|
"log": {
|
||||||
'level': 'INFO',
|
"level": "INFO",
|
||||||
'formant': '[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s',
|
"formant": "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s",
|
||||||
},
|
},
|
||||||
'monitors': {
|
"monitors": {
|
||||||
'example': {
|
"example": {
|
||||||
'method': 'GET',
|
"method": "GET",
|
||||||
'url': 'http://fake-example.com',
|
"url": "http://fake-example.com",
|
||||||
'timeout': 1,
|
"timeout": 1,
|
||||||
'check_every': 1,
|
"check_every": 1,
|
||||||
},
|
},
|
||||||
'httpbin': {
|
"httpbin": {
|
||||||
'method': 'GET',
|
"method": "GET",
|
||||||
'url': 'https://fake-httpbin.org/get',
|
"url": "https://fake-httpbin.org/get",
|
||||||
'timeout': 1,
|
"timeout": 1,
|
||||||
'check_every': 1,
|
"check_every": 1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -43,7 +43,7 @@ def container():
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_example_monitor(container, caplog):
|
async def test_example_monitor(container, caplog):
|
||||||
caplog.set_level('INFO')
|
caplog.set_level("INFO")
|
||||||
|
|
||||||
http_client_mock = mock.AsyncMock()
|
http_client_mock = mock.AsyncMock()
|
||||||
http_client_mock.request.return_value = RequestStub(
|
http_client_mock.request.return_value = RequestStub(
|
||||||
|
@ -55,14 +55,14 @@ async def test_example_monitor(container, caplog):
|
||||||
example_monitor = container.example_monitor()
|
example_monitor = container.example_monitor()
|
||||||
await example_monitor.check()
|
await example_monitor.check()
|
||||||
|
|
||||||
assert 'http://fake-example.com' in caplog.text
|
assert "http://fake-example.com" in caplog.text
|
||||||
assert 'response code: 200' in caplog.text
|
assert "response code: 200" in caplog.text
|
||||||
assert 'content length: 635' in caplog.text
|
assert "content length: 635" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dispatcher(container, caplog, event_loop):
|
async def test_dispatcher(container, caplog, event_loop):
|
||||||
caplog.set_level('INFO')
|
caplog.set_level("INFO")
|
||||||
|
|
||||||
example_monitor_mock = mock.AsyncMock()
|
example_monitor_mock = mock.AsyncMock()
|
||||||
httpbin_monitor_mock = mock.AsyncMock()
|
httpbin_monitor_mock = mock.AsyncMock()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user