mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 09:57:37 +03:00 
			
		
		
		
	* Update main example * Updating wiring module * Update wiring test case name * Implement string imports for wiring * Update example * Refactor implementation * Update front example * Fix a typo in README * Update wiring docs * Update single container example * Update multiple containers example * Update quotes in multiple containers example * Update quotes in single container example * Update decoupled-packages example * Update single and multiple containers example * Update quotes * Update fastapi+redis example * Update resource docs * Update quotes in CLI tutorial * Update CLI application (movie lister) tutorial * Update monitoring daemon example * Update python version in asyncio daemon example * Update asyncio daemon tutorial * Update quotes in wiring docs * Update wiring docs
		
			
				
	
	
		
			57 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			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",
 | 
						|
            self._method,
 | 
						|
            self._url,
 | 
						|
            response.status,
 | 
						|
            response.content_length,
 | 
						|
            round(time_took, 3)
 | 
						|
        )
 |