Take out common code in a base class
This commit is contained in:
parent
916fcf1bab
commit
eaf0b51fe9
3 changed files with 31 additions and 18 deletions
24
src/chweb/base.py
Normal file
24
src/chweb/base.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
"""
|
||||||
|
Base classes used in multiple modules.
|
||||||
|
"""
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from chweb.models import Config
|
||||||
|
|
||||||
|
|
||||||
|
class Application:
|
||||||
|
"""
|
||||||
|
A base class for applications / services.
|
||||||
|
"""
|
||||||
|
def __init__(self, config: Config,
|
||||||
|
logger: logging.Logger,
|
||||||
|
event_loop: asyncio.AbstractEventLoop,
|
||||||
|
queue: asyncio.Queue):
|
||||||
|
self.config = config
|
||||||
|
self.logger = logger
|
||||||
|
self.loop = event_loop
|
||||||
|
self.queue = queue
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
raise NotImplementedError()
|
|
@ -10,21 +10,15 @@ from urllib.parse import urlparse
|
||||||
import aiokafka # type: ignore
|
import aiokafka # type: ignore
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from chweb.models import Config, Check
|
from chweb.base import Application
|
||||||
|
from chweb.models import Check
|
||||||
|
|
||||||
|
|
||||||
class Collector:
|
class Collector(Application):
|
||||||
"""
|
"""
|
||||||
A class that contains all methods needed to check the statuses of all
|
A class that contains all methods needed to check the statuses of all
|
||||||
websites present in the config.
|
websites present in the config.
|
||||||
"""
|
"""
|
||||||
def __init__(self, config: Config,
|
|
||||||
event_loop: asyncio.AbstractEventLoop,
|
|
||||||
queue: asyncio.Queue):
|
|
||||||
self.config = config
|
|
||||||
self.loop = event_loop
|
|
||||||
self.queue = queue
|
|
||||||
|
|
||||||
async def check(self, url: str, regex: Optional[str]) -> Check:
|
async def check(self, url: str, regex: Optional[str]) -> Check:
|
||||||
"""
|
"""
|
||||||
Checks the status of a website and optionally matches a regex on the
|
Checks the status of a website and optionally matches a regex on the
|
||||||
|
@ -45,7 +39,7 @@ class Collector:
|
||||||
response_time=res.elapsed.microseconds,
|
response_time=res.elapsed.microseconds,
|
||||||
regex_matches=matches,
|
regex_matches=matches,
|
||||||
status=res.status_code,
|
status=res.status_code,
|
||||||
url=res.url,
|
url=url,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def check_forever(self, site):
|
async def check_forever(self, site):
|
||||||
|
|
|
@ -3,22 +3,17 @@ Sample consumer.
|
||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
import aiokafka # type: ignore
|
import aiokafka # type: ignore
|
||||||
import asyncpg # type: ignore
|
import asyncpg # type: ignore
|
||||||
|
|
||||||
|
from chweb.base import Application
|
||||||
from chweb.models import Check
|
from chweb.models import Check
|
||||||
|
|
||||||
|
|
||||||
class Consumer:
|
class Consumer(Application):
|
||||||
def __init__(self, config: Dict[str, Any],
|
|
||||||
event_loop: asyncio.AbstractEventLoop,
|
|
||||||
queue: asyncio.Queue):
|
|
||||||
self.config = config
|
|
||||||
self.loop = event_loop
|
|
||||||
self.queue = queue
|
|
||||||
|
|
||||||
async def consume(self):
|
async def consume(self):
|
||||||
"""
|
"""
|
||||||
Consumes messages from a Kafka topic.
|
Consumes messages from a Kafka topic.
|
||||||
|
|
Loading…
Reference in a new issue