async def test_healthcheck_server_failure(timemachine): app = Pyncette( repository_factory=wrap_factory(sqlite_repository, timemachine)) async def healthcheck_handler(app_context): return False # Bind on random port to avoid conflict use_healthcheck_server(app, port=0, bind_address="127.0.0.1", healthcheck_handler=healthcheck_handler) async with app.create() as ctx, aiohttp.ClientSession() as session: task = asyncio.create_task(ctx.run()) async with session.get( f"http://127.0.0.1:{get_healthcheck_port(ctx)}/health" ) as resp: assert resp.status == 500 ctx.shutdown() await task await timemachine.unwind()
async def test_healthcheck_server_invalid_verb(timemachine): app = Pyncette( repository_factory=wrap_factory(sqlite_repository, timemachine)) async def healthcheck_handler(app_context): pass # pragma: no cover # Bind on random port to avoid conflict use_healthcheck_server(app, port=0, bind_address="127.0.0.1", healthcheck_handler=healthcheck_handler) async with app.create() as ctx, aiohttp.ClientSession() as session: task = asyncio.create_task(ctx.run()) async with session.post( f"http://127.0.0.1:{get_healthcheck_port(ctx)}/health", json={"test": "object"}, ) as resp: assert resp.status == 405 ctx.shutdown() await task await timemachine.unwind()
curl localhost:8080/health """ import asyncio import logging import random from pyncette import Context from pyncette import Pyncette from pyncette.executor import SynchronousExecutor from pyncette.healthcheck import use_healthcheck_server logger = logging.getLogger(__name__) # We use the SynchronousExecutor so long-running tasks will delay # cause polling to stall and simulate unhealthiness. app = Pyncette(executor_cls=SynchronousExecutor) use_healthcheck_server(app, port=8080) @app.task(schedule="* * * * * */2") async def hello_world(context: Context) -> None: if random.choice([True, False]): await asyncio.sleep(4) logger.info("Hello, world!") if __name__ == "__main__": app.main()
import logging from prometheus_client import start_http_server from pyncette import Context from pyncette import FailureMode from pyncette import Pyncette from pyncette.healthcheck import use_healthcheck_server from pyncette.prometheus import use_prometheus from pyncette.sqlite import sqlite_repository logger = logging.getLogger(__name__) app = Pyncette(sqlite_database="pyncette.db") use_healthcheck_server(app) use_prometheus(app) @app.task(schedule="* * * * * */2") async def hello_world(context: Context): logger.info("Hello, world!") if __name__ == "__main__": start_http_server(port=9699, addr="0.0.0.0") app.main()