diff --git a/requirements.txt b/requirements.txt index 79a5f07f..4177d4c5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,4 @@ sentry-sdk>=0.11.1 click>=7.0 keyring>=19.2.0 urllib3>=1.25.6 +feedparser>=5.2.1 diff --git a/royalnet/monitors/__init__.py b/royalnet/monitors/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/royalnet/monitors/monitor.py b/royalnet/monitors/monitor.py new file mode 100644 index 00000000..d1a1761b --- /dev/null +++ b/royalnet/monitors/monitor.py @@ -0,0 +1,30 @@ +import asyncio +import time + + +class Monitor: + def __init__(self, interval: float, *, loop: asyncio.AbstractEventLoop): + self.interval: float = interval + self._loop: asyncio.AbstractEventLoop = loop + self.last_check_start: float = 0.0 + self.last_check_end: float = 0.0 + + @property + def last_check_duration(self): + return self.last_check_end - self.last_check_start + + def run_blocking(self): + self._loop.run_until_complete(self.run()) + + async def run(self): + while True: + self.last_check_start = time.time() + await self.check() + self.last_check_end = time.time() + await asyncio.sleep(self.interval - self.last_check_duration, loop=self._loop) + + async def check(self): + raise NotImplementedError() + + def __repr__(self): + return f"<{self.__class__.__qualname__} running every {self.interval} seconds>" diff --git a/setup.py b/setup.py index 057c07e4..01d020bb 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,8 @@ setuptools.setup( "sortedcontainers>=2.1.0", "sentry-sdk>=0.11.1", "click>=7.0", - "keyring>=19.2.0"], + "keyring>=19.2.0", + "feedparser>=5.2.1"], python_requires=">=3.7", classifiers=[ "Development Status :: 3 - Alpha",