2023-05-15 08:51:32 +08:00
|
|
|
import os
|
2024-11-11 18:32:28 +08:00
|
|
|
import sys
|
2023-06-15 13:59:36 +08:00
|
|
|
|
2024-10-22 11:01:32 +08:00
|
|
|
from configs import dify_config
|
|
|
|
|
2024-11-08 09:42:53 +08:00
|
|
|
if not dify_config.DEBUG:
|
2023-05-15 08:51:32 +08:00
|
|
|
from gevent import monkey
|
2024-04-12 14:53:44 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
monkey.patch_all()
|
2024-04-25 22:26:45 +08:00
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
import grpc.experimental.gevent
|
2024-04-12 14:53:44 +08:00
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
grpc.experimental.gevent.init_gevent()
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
import json
|
|
|
|
import threading
|
2024-01-12 12:34:01 +08:00
|
|
|
import time
|
|
|
|
import warnings
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-10-18 08:23:36 +08:00
|
|
|
from flask import Response
|
2024-04-18 20:24:05 +08:00
|
|
|
|
2024-10-18 08:23:36 +08:00
|
|
|
from app_factory import create_app
|
2024-04-18 20:24:05 +08:00
|
|
|
|
|
|
|
# DO NOT REMOVE BELOW
|
2024-10-08 11:13:11 +08:00
|
|
|
from events import event_handlers # noqa: F401
|
2024-10-18 12:30:25 +08:00
|
|
|
from extensions.ext_database import db
|
2024-07-04 18:18:26 +08:00
|
|
|
|
|
|
|
# TODO: Find a way to avoid importing models here
|
2024-10-08 11:13:11 +08:00
|
|
|
from models import account, dataset, model, source, task, tool, tools, web # noqa: F401
|
2024-04-12 14:53:44 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
# DO NOT REMOVE ABOVE
|
|
|
|
|
2024-11-11 18:32:28 +08:00
|
|
|
if sys.version_info[:2] == (3, 10):
|
|
|
|
print("Warning: Python 3.10 will not be supported in the next version.")
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
warnings.simplefilter("ignore", ResourceWarning)
|
|
|
|
|
2024-09-24 17:33:29 +08:00
|
|
|
os.environ["TZ"] = "UTC"
|
|
|
|
# windows platform not support tzset
|
|
|
|
if hasattr(time, "tzset"):
|
2023-11-21 11:49:07 +08:00
|
|
|
time.tzset()
|
2023-11-15 19:14:31 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
# create app
|
|
|
|
app = create_app()
|
|
|
|
celery = app.extensions["celery"]
|
|
|
|
|
2024-10-22 11:01:32 +08:00
|
|
|
if dify_config.TESTING:
|
2023-05-15 08:51:32 +08:00
|
|
|
print("App is running in TESTING mode")
|
|
|
|
|
|
|
|
|
|
|
|
@app.after_request
|
|
|
|
def after_request(response):
|
|
|
|
"""Add Version headers to the response."""
|
2024-08-15 12:54:05 +08:00
|
|
|
response.set_cookie("remember_token", "", expires=0)
|
2024-10-22 11:01:32 +08:00
|
|
|
response.headers.add("X-Version", dify_config.CURRENT_VERSION)
|
|
|
|
response.headers.add("X-Env", dify_config.DEPLOY_ENV)
|
2023-05-15 08:51:32 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@app.route("/health")
|
2023-05-15 08:51:32 +08:00
|
|
|
def health():
|
2024-08-15 12:54:05 +08:00
|
|
|
return Response(
|
2024-10-22 11:01:32 +08:00
|
|
|
json.dumps({"pid": os.getpid(), "status": "ok", "version": dify_config.CURRENT_VERSION}),
|
2024-08-15 12:54:05 +08:00
|
|
|
status=200,
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@app.route("/threads")
|
2023-05-15 08:51:32 +08:00
|
|
|
def threads():
|
|
|
|
num_threads = threading.active_count()
|
|
|
|
threads = threading.enumerate()
|
|
|
|
|
|
|
|
thread_list = []
|
|
|
|
for thread in threads:
|
|
|
|
thread_name = thread.name
|
|
|
|
thread_id = thread.ident
|
|
|
|
is_alive = thread.is_alive()
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
thread_list.append(
|
|
|
|
{
|
|
|
|
"name": thread_name,
|
|
|
|
"id": thread_id,
|
|
|
|
"is_alive": is_alive,
|
|
|
|
}
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
return {
|
2024-08-15 12:54:05 +08:00
|
|
|
"pid": os.getpid(),
|
|
|
|
"thread_num": num_threads,
|
|
|
|
"threads": thread_list,
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@app.route("/db-pool-stat")
|
2023-08-12 00:57:00 +08:00
|
|
|
def pool_stat():
|
|
|
|
engine = db.engine
|
|
|
|
return {
|
2024-08-15 12:54:05 +08:00
|
|
|
"pid": os.getpid(),
|
|
|
|
"pool_size": engine.pool.size(),
|
|
|
|
"checked_in_connections": engine.pool.checkedin(),
|
|
|
|
"checked_out_connections": engine.pool.checkedout(),
|
|
|
|
"overflow_connections": engine.pool.overflow(),
|
|
|
|
"connection_timeout": engine.pool.timeout(),
|
|
|
|
"recycle_time": db.engine.pool._recycle,
|
2023-08-12 00:57:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0", port=5001)
|