app.py 2.5 KB

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