Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py109 linesDownload Raw Back to api
1import os2 3from configs import dify_config4 5if os.environ.get("DEBUG", "false").lower() != "true":6    from gevent import monkey7 8    monkey.patch_all()9 10    import grpc.experimental.gevent11 12    grpc.experimental.gevent.init_gevent()13 14import json15import threading16import time17import warnings18 19from flask import Response20 21from app_factory import create_app22 23# DO NOT REMOVE BELOW24from events import event_handlers  # noqa: F40125from extensions.ext_database import db26 27# TODO: Find a way to avoid importing models here28from models import account, dataset, model, source, task, tool, tools, web  # noqa: F40129 30# DO NOT REMOVE ABOVE31 32 33warnings.simplefilter("ignore", ResourceWarning)34 35os.environ["TZ"] = "UTC"36# windows platform not support tzset37if hasattr(time, "tzset"):38    time.tzset()39 40 41# create app42app = create_app()43celery = app.extensions["celery"]44 45if dify_config.TESTING:46    print("App is running in TESTING mode")47 48 49@app.after_request50def after_request(response):51    """Add Version headers to the response."""52    response.set_cookie("remember_token", "", expires=0)53    response.headers.add("X-Version", dify_config.CURRENT_VERSION)54    response.headers.add("X-Env", dify_config.DEPLOY_ENV)55    return response56 57 58@app.route("/health")59def health():60    return Response(61        json.dumps({"pid": os.getpid(), "status": "ok", "version": dify_config.CURRENT_VERSION}),62        status=200,63        content_type="application/json",64    )65 66 67@app.route("/threads")68def threads():69    num_threads = threading.active_count()70    threads = threading.enumerate()71 72    thread_list = []73    for thread in threads:74        thread_name = thread.name75        thread_id = thread.ident76        is_alive = thread.is_alive()77 78        thread_list.append(79            {80                "name": thread_name,81                "id": thread_id,82                "is_alive": is_alive,83            }84        )85 86    return {87        "pid": os.getpid(),88        "thread_num": num_threads,89        "threads": thread_list,90    }91 92 93@app.route("/db-pool-stat")94def pool_stat():95    engine = db.engine96    return {97        "pid": os.getpid(),98        "pool_size": engine.pool.size(),99        "checked_in_connections": engine.pool.checkedin(),100        "checked_out_connections": engine.pool.checkedout(),101        "overflow_connections": engine.pool.overflow(),102        "connection_timeout": engine.pool.timeout(),103        "recycle_time": db.engine.pool._recycle,104    }105 106 107if __name__ == "__main__":108    app.run(host="0.0.0.0", port=5001)109