app.py 2.6 KB

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