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 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
|
||||
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:
|
||||
"""
|
||||
Checks the status of a website and optionally matches a regex on the
|
||||
|
@ -45,7 +39,7 @@ class Collector:
|
|||
response_time=res.elapsed.microseconds,
|
||||
regex_matches=matches,
|
||||
status=res.status_code,
|
||||
url=res.url,
|
||||
url=url,
|
||||
)
|
||||
|
||||
async def check_forever(self, site):
|
||||
|
|
|
@ -3,22 +3,17 @@ Sample consumer.
|
|||
"""
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Dict
|
||||
|
||||
import aiokafka # type: ignore
|
||||
import asyncpg # type: ignore
|
||||
|
||||
from chweb.base import Application
|
||||
from chweb.models import Check
|
||||
|
||||
|
||||
class Consumer:
|
||||
def __init__(self, config: Dict[str, Any],
|
||||
event_loop: asyncio.AbstractEventLoop,
|
||||
queue: asyncio.Queue):
|
||||
self.config = config
|
||||
self.loop = event_loop
|
||||
self.queue = queue
|
||||
|
||||
class Consumer(Application):
|
||||
async def consume(self):
|
||||
"""
|
||||
Consumes messages from a Kafka topic.
|
||||
|
|
Loading…
Reference in a new issue