-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Remove Sockets and clean some code (#85)
* feat: Remove Sockets and clean some code * feat: reorganized command handlers and filters * chore: refactor * flake8 * typo --------- Co-authored-by: andruten <andruten@users.noreply.github.com>
- Loading branch information
Showing
23 changed files
with
231 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .request_backend import RequestBackend | ||
|
||
__all__ = [ | ||
'RequestBackend', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from abc import ABC, abstractmethod | ||
from datetime import datetime | ||
from typing import Tuple, Optional | ||
|
||
|
||
class BaseBackend(ABC): | ||
def __init__(self, service) -> None: | ||
self.service = service | ||
|
||
@abstractmethod | ||
async def check( | ||
self, | ||
*args, | ||
**kwargs, | ||
) -> Tuple[bool, Optional[float], Optional[datetime], Optional[int]]: # pragma: no cover | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import logging | ||
from datetime import datetime | ||
from typing import Tuple, Optional | ||
|
||
import httpx | ||
import ssl | ||
|
||
from .base_backend import BaseBackend | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RequestBackend(BaseBackend): | ||
async def check(self, session) -> Tuple[bool, Optional[float], Optional[datetime], Optional[int]]: | ||
headers = { | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' | ||
'(KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36' | ||
} | ||
try: | ||
logger.debug(f"Fetching {self.service.url}") | ||
response = await session.request(method='GET', url=self.service.url, headers=headers) | ||
except (httpx.HTTPError, ssl.SSLCertVerificationError,) as exc: | ||
logger.warning(f'"{self.service.url}" request failed {exc}') | ||
return False, None, None, None | ||
else: | ||
raw_stream = response.extensions['network_stream'] | ||
ssl_object = raw_stream.get_extra_info('ssl_object') | ||
cert = ssl_object.getpeercert() | ||
expire_date = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z') | ||
elapsed_total_seconds = response.elapsed.total_seconds() | ||
logger.debug(f'{self.service.url} fetched in {elapsed_total_seconds}') | ||
service_is_healthy = (400 <= response.status_code <= 511) | ||
return not service_is_healthy, elapsed_total_seconds, expire_date, response.status_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .service import Service, ServiceStatus | ||
|
||
__all__ = [ | ||
'Service', | ||
'ServiceStatus', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import enum | ||
from dataclasses import dataclass, field, asdict | ||
from datetime import datetime | ||
from typing import Optional, Dict | ||
|
||
from backends import RequestBackend | ||
|
||
|
||
class ServiceStatus(enum.Enum): | ||
UNKNOWN = 'unknown' | ||
HEALTHY = 'healthy' | ||
UNHEALTHY = 'unhealthy' | ||
|
||
|
||
def service_asdict_factory(data): | ||
def convert_value(obj): | ||
if isinstance(obj, ServiceStatus): | ||
return obj.value | ||
elif isinstance(obj, datetime): | ||
return obj.strftime('%Y-%m-%dT%H:%M:%S.%f') | ||
return obj | ||
|
||
return dict((k, convert_value(v)) for k, v in data) | ||
|
||
|
||
@dataclass | ||
class Service: | ||
name: str = field() | ||
url: str = field() | ||
enabled: bool = field(default=True) | ||
last_time_healthy: Optional[datetime] = field(default=None) | ||
last_http_response_status_code: Optional[int] = field(default=None) | ||
time_to_first_byte: float = field(default=0.0) | ||
status: ServiceStatus = field(init=True, default=ServiceStatus.UNKNOWN) | ||
expire_date: Optional[datetime] = field(default=None) | ||
|
||
@property | ||
def healthcheck_backend(self) -> RequestBackend: | ||
return RequestBackend(self) | ||
|
||
def __repr__(self) -> str: # pragma: no cover | ||
return f'{self.name} <{self.url}>' | ||
|
||
def __str__(self) -> str: # pragma: no cover | ||
return f'{self.name} <{self.url}>' | ||
|
||
def to_dict(self) -> Dict: | ||
return asdict(self, dict_factory=service_asdict_factory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .base_persistence import BaseRepository | ||
from .local_json_repository import LocalJsonRepository | ||
|
||
__all__ = [ | ||
'BaseRepository', | ||
'LocalJsonRepository', | ||
] |
Oops, something went wrong.