【问题标题】:Django: Checks with web-viewDjango:使用网络视图检查
【发布时间】:2019-06-12 09:19:04
【问题描述】:
【问题讨论】:
标签:
python
django
health-monitoring
【解决方案1】:
内置系统检查实际上主要用于开发 - 关键是如果这些测试失败,您的项目很可能根本无法运行。
但您仍然可以使用 management.call_command 从 python 代码调用此(或任何其他)管理命令 - 您只需要提供一个可写的类似文件的对象来捕获 stdout/stderr:
from StringIO import StringIO
from django.core.management import call_command, check
def check_view(request):
out = StringIO()
cmd = check.Command(stdout=out, stderr=out)
call_command(check)
out.seek(0)
context = {"results": out.readlines()}
return render(request, "check.html", context)
然后只需将其插入您的管理员即可(已记录在案,因此我不会给出完整的示例)。
注意:wrt/将您自己的检查添加到check 命令this is fully documented too。
【解决方案2】:
云平台提供商通常会提供运行状况检查,在特定时间点对您的应用执行 ping 操作,例如:/health
django-health-check 提供可以在访问 /health 时执行的测试。
如果他们都通过了,则返回 200。否则云提供商将通知您的管理员。
当然,您可以使该页面仅对管理员可见,以手动检查或编写您自己的脚本来监督您的应用程序状态。