轻松创建运行状况检查的终结点
django-healthpoint的Python项目详细描述
用于向项目中添加运行状况检查终结点的框架。
快速启动
安装应用程序:
# settings.py INSTALLED_APPS = [ ... 'healthpoint' ] # If specified, this user is able to see the details for each # individual check in the endpoint. HEALTHPOINT_BASICAUTH_USERNAME = 'john' HEALTHPOINT_BASICAUTH_PASSWORD = 'doe' # urls.py urlpatterns = [ ... url(r'^', include('healthpoint.urls')), ]
将名为health.py的模块添加到任何应用程序中。例如:
from datetime import timedelta from django.contrib.auth.models import User from django.utils import timezone from healthpoint.decorators import health_check @health_check def user_signup(): last_user = User.objects.last() time_since_last_signup = timezone.now() - last_user.date_joined # Return True/False, throw an exception, or return a tuple with a # detail message. return ( time_since_last_signup <= timedelta(days=1), "last signup was: {}".format(last_user.date_joined))
可以通过/health/端点访问运行状况检查:
它执行所有运行状况检查,如果所有检查成功,则报告状态200,否则报告状态500。
如果员工用户已登录,则端点将报告每个单独检查的结果:
{ "success": {}, "error": { "myproject.myapp.health.user_signup": "Last signup was: 2017-10-29 08:45:51" } }
为了提供有关结果的更多详细信息,@health_check可以返回一个元组(success:bool, detail:str)。详细信息将列在结果中。