python-dependency-injector/examples/miniapps/monitoring-daemon-asyncio/monitoringdaemon/monitors.py
Roman Mogylatov 3a61457be7
Asyncio daemon tutorial (#278)
* Fix a typo in the docblock of the Configuration provider

* Update the changelog

* Add tutorial sections

* Switch to use https for httpbin.org requests

* Add what we are going to build section

* Fix ``Makefile`` to run ``aiohttp`` integration tests on Python 3.5+

* Add prerequisities and prepare the env sections

* Add logging, config and the dispacher sections

* Change logging

* Fix multiple typos in the ``flask`` and ``aiohttp`` tutorials

* Add the initial and dirty version

* Fix multiple typos in the ``flask`` and ``aiohttp`` tutorials

* Fix the 3.27.0 changelog

* Finish all the parts before the dispatcher

* Finish dispatcher section

* Update http monitor logging format

* Finish the tutorial

* Fix docblock in the dispatcher module

* Update changelog

* Add meta keywords and description
2020-08-08 14:48:05 -04:00

57 lines
1.4 KiB
Python

"""Monitors module."""
import logging
import time
from typing import Dict, Any
from .http import HttpClient
class Monitor:
def __init__(self, check_every: int) -> None:
self.check_every = check_every
self.logger = logging.getLogger(self.__class__.__name__)
async def check(self) -> None:
raise NotImplementedError()
class HttpMonitor(Monitor):
def __init__(
self,
http_client: HttpClient,
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'))
async def check(self) -> None:
time_start = time.time()
response = await self._client.request(
method=self._method,
url=self._url,
timeout=self._timeout,
)
time_end = time.time()
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\n',
self._method,
self._url,
response.status,
response.content_length,
round(time_took, 3)
)