class BasicHealthCheckTest(AioHTTPTestCase): path = '/h' def setUp(self): self.hc = HealthCheck() super().setUp() async def get_application(self): app = web.Application() app.router.add_get(self.path, self.hc.__call__) return app @unittest_run_loop async def test_basic_check(self): response = await self.client.get(self.path) self.assertEqual(200, response.status) @unittest_run_loop async def test_failing_check(self): async def fail_check(): return False, "FAIL" self.hc.add_check(fail_check) response = await self.client.get(self.path) self.assertEqual(500, response.status) @unittest_run_loop async def test_sync_check(self): def test_check(): return True, "OK" self.hc.add_check(test_check) response = await self.client.get(self.path) self.assertEqual(200, response.status)
def main(): log = logging.getLogger() log.setLevel(logging.INFO) ch_format = ColoredFormatter( "%(asctime)s - %(threadName)s - %(name)s - %(log_color)s %(levelname)s - %(reset)s %(purple)s %(message)s", datefmt=None, reset=True, log_colors={ "DEBUG": "cyan", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "red,bg_white", }, secondary_log_colors={}, style="%") ch = logging.StreamHandler(sys.stdout) ch.setFormatter(ch_format) log.addHandler(ch) if not os.path.exists(LOG_FOLDER): try: log.warning("Log folder does not exists. Creating...") os.makedirs(LOG_FOLDER) log.info("Created!") except Exception as err: print("Error happened during log folder creating") print(repr(err)) print("Cannot continue, sorry") sys.exit(1) fh = handlers.RotatingFileHandler(os.path.join(LOG_FOLDER, "adapt-info-back.log"), maxBytes=(1024 * 100), backupCount=10) fh_format = logging.Formatter( "%(asctime)s - [%(threadName)s] - %(name)s - [%(levelname)s] - %(message)s" ) fh.setFormatter(fh_format) log.addHandler(fh) app = web.Application(middlewares=(web.normalize_path_middleware(), cors_factory, auth_middleware)) packages = ["api"] for route in get_routes(packages): app.router.add_route(*route) healthcheck = HealthCheck() app.router.add_get("/api/healthcheck", healthcheck) app.internal_api_token = TOKEN # app.router.add_static("/static", STATIC_PATH, name="static") log.info("server started!") web.run_app(app, host=HOST, port=PORT)
from aiogram import Dispatcher from aiogram.utils.executor import Executor from aiohttp_healthcheck import HealthCheck from loguru import logger from app import config health = HealthCheck() def setup(executor: Executor): executor.on_startup(on_startup, webhook=True, polling=False) async def on_startup(dispatcher: Dispatcher): from app.utils.executor import runner logger.info("Setup healthcheck") health.add_check(check_redis) health.add_check(check_postgres) health.add_check(check_webhook) runner.web_app.router.add_get("/healthcheck", health) async def check_redis(): from app.misc import storage try: redis = await storage.redis() info = await redis.info()
def setUp(self): self.hc = HealthCheck() super().setUp()
from typing import Tuple from aiogram import Bot from aiohttp import web from aiohttp_healthcheck import HealthCheck from loguru import logger async def health_check() -> Tuple[bool, str]: return True, 'Server alive' async def check_webhook() -> Tuple[bool, str]: from data import config bot: Bot = health_app['bot'] webhook = await bot.get_webhook_info() if webhook.url and webhook.url == config.WEBHOOK_URL: return True, f'Webhook configured. Pending updates count {webhook.pending_update_count}' else: logger.error('Configured wrong webhook URL {webhook}', webhook=webhook.url) return False, 'Configured invalid webhook URL' health_app = web.Application() health = HealthCheck() health.add_check(health_check) health.add_check(check_webhook) health_app.add_routes([web.get('/check', health)])
from aiohttp import web from aiohttp_healthcheck import HealthCheck from setting import logger from view import handler_of_userinfo, health_check app = web.Application() # app = web.Application(loop=loop, middlewares=middlewares) app.router.add_route('POST', '/useradd', handler_of_userinfo) # app.router.add_route('GET', '/{name}', handler) logger.info('Application started') health = HealthCheck() app.router.add_get("/healthcheck", health) health.add_check(health_check)