app.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import os
  2. import sys
  3. python_version = sys.version_info
  4. if not ((3, 11) <= python_version < (3, 13)):
  5. print(f"Python 3.11 or 3.12 is required, current version is {python_version.major}.{python_version.minor}")
  6. raise SystemExit(1)
  7. from configs import dify_config
  8. if not dify_config.DEBUG:
  9. from gevent import monkey
  10. monkey.patch_all()
  11. import grpc.experimental.gevent
  12. grpc.experimental.gevent.init_gevent()
  13. import json
  14. import threading
  15. import time
  16. import warnings
  17. from flask import Response
  18. from app_factory import create_app
  19. # DO NOT REMOVE BELOW
  20. from events import event_handlers # noqa: F401
  21. from extensions.ext_database import db
  22. # TODO: Find a way to avoid importing models here
  23. from models import account, dataset, model, source, task, tool, tools, web # noqa: F401
  24. # DO NOT REMOVE ABOVE
  25. warnings.simplefilter("ignore", ResourceWarning)
  26. os.environ["TZ"] = "UTC"
  27. # windows platform not support tzset
  28. if hasattr(time, "tzset"):
  29. time.tzset()
  30. # create app
  31. app = create_app()
  32. celery = app.extensions["celery"]
  33. if dify_config.TESTING:
  34. print("App is running in TESTING mode")
  35. @app.after_request
  36. def after_request(response):
  37. """Add Version headers to the response."""
  38. response.headers.add("X-Version", dify_config.CURRENT_VERSION)
  39. response.headers.add("X-Env", dify_config.DEPLOY_ENV)
  40. return response
  41. @app.route("/health")
  42. def health():
  43. return Response(
  44. json.dumps({"pid": os.getpid(), "status": "ok", "version": dify_config.CURRENT_VERSION}),
  45. status=200,
  46. content_type="application/json",
  47. )
  48. @app.route("/threads")
  49. def threads():
  50. num_threads = threading.active_count()
  51. threads = threading.enumerate()
  52. thread_list = []
  53. for thread in threads:
  54. thread_name = thread.name
  55. thread_id = thread.ident
  56. is_alive = thread.is_alive()
  57. thread_list.append(
  58. {
  59. "name": thread_name,
  60. "id": thread_id,
  61. "is_alive": is_alive,
  62. }
  63. )
  64. return {
  65. "pid": os.getpid(),
  66. "thread_num": num_threads,
  67. "threads": thread_list,
  68. }
  69. @app.route("/db-pool-stat")
  70. def pool_stat():
  71. engine = db.engine
  72. return {
  73. "pid": os.getpid(),
  74. "pool_size": engine.pool.size(),
  75. "checked_in_connections": engine.pool.checkedin(),
  76. "checked_out_connections": engine.pool.checkedout(),
  77. "overflow_connections": engine.pool.overflow(),
  78. "connection_timeout": engine.pool.timeout(),
  79. "recycle_time": db.engine.pool._recycle,
  80. }
  81. if __name__ == "__main__":
  82. app.run(host="0.0.0.0", port=5001)