烧瓶运行状况错误: { "status" : 500, "title" : "The live check function could not be imported" }



我正在尝试添加健康活力和准备到Flask API如下:

from flask import Flask
from flask_healthz import healthz
from flask_healthz import HealthError
app = Flask(__name__)
app.register_blueprint(healthz, url_prefix="/healthz")
def printok():
print("Everything is fine")
def liveness():
try:
printok()
except Exception:
raise HealthError("Can't connect to the file")
def readiness():
try:
printok()
except Exception:
raise HealthError("Can't connect to the file")
app.config.update(
HEALTHZ = {
"live": "app.liveness",
"ready": "app.readiness",
}
)
@app.route("/")
def hello():
return "Hello World"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

但是当我运行curl检查它时,我得到了错误" live函数不能被导入";

$ curl localhost:5000/healthz/live
{"status": 500, "title": "The live check function could not be imported"}

$ curl localhost:5000/healthz/ready
{"status": 500, "title": "The ready check function could not be imported"}

有趣的是,这个例子中已经提出了这个链接healthz烧瓶作为工作

知道为什么发生这个错误吗?我该如何解决?

对我来说,这是由于PYTHONPATH环境变量。HEALTHZ映射值需要指向lively和readiness方法,而PYTHONPATH是必需的。

在我的PyCharm的调试中,这个环境变量被正确设置,而在运行中没有设置。手动添加此运行修复此。如果你正在对应用程序进行dockerization,这将会有所帮助- https://stackoverflow.com/a/49631407/2112865.

罪魁祸首是这一行:

app = Flask(__name__)

它会用你的代码创建一个文件名称的应用程序,所以如果你把文件命名为'mycheck.py',你会得到同样的错误。我修复了这个变化:

app.config.update(
HEALTHZ = {
"live": app.name + ".liveness",
"ready": app.name + ".readiness",
}
)

请注意,由于某些原因,这也不能工作:

app = Flask("app")