| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | import osimport sysfrom configs import dify_configif not dify_config.DEBUG:    from gevent import monkey    monkey.patch_all()    import grpc.experimental.gevent    grpc.experimental.gevent.init_gevent()import jsonimport threadingimport timeimport warningsfrom flask import Responsefrom app_factory import create_app# DO NOT REMOVE BELOWfrom events import event_handlers  # noqa: F401from extensions.ext_database import db# TODO: Find a way to avoid importing models herefrom models import account, dataset, model, source, task, tool, tools, web  # noqa: F401# DO NOT REMOVE ABOVEif sys.version_info[:2] == (3, 10):    print("Warning: Python 3.10 will not be supported in the next version.")warnings.simplefilter("ignore", ResourceWarning)os.environ["TZ"] = "UTC"# windows platform not support tzsetif hasattr(time, "tzset"):    time.tzset()# create appapp = create_app()celery = app.extensions["celery"]if dify_config.TESTING:    print("App is running in TESTING mode")@app.after_requestdef after_request(response):    """Add Version headers to the response."""    response.set_cookie("remember_token", "", expires=0)    response.headers.add("X-Version", dify_config.CURRENT_VERSION)    response.headers.add("X-Env", dify_config.DEPLOY_ENV)    return response@app.route("/health")def health():    return Response(        json.dumps({"pid": os.getpid(), "status": "ok", "version": dify_config.CURRENT_VERSION}),        status=200,        content_type="application/json",    )@app.route("/threads")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()        thread_list.append(            {                "name": thread_name,                "id": thread_id,                "is_alive": is_alive,            }        )    return {        "pid": os.getpid(),        "thread_num": num_threads,        "threads": thread_list,    }@app.route("/db-pool-stat")def pool_stat():    engine = db.engine    return {        "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,    }if __name__ == "__main__":    app.run(host="0.0.0.0", port=5001)
 |