app.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # TODO: Find a way to avoid importing models here
  16. from models import account, dataset, model, source, task, tool, tools, web # noqa: F401
  17. # DO NOT REMOVE ABOVE
  18. warnings.simplefilter("ignore", ResourceWarning)
  19. os.environ["TZ"] = "UTC"
  20. # windows platform not support tzset
  21. if hasattr(time, "tzset"):
  22. time.tzset()
  23. # -------------
  24. # Configuration
  25. # -------------
  26. config_type = os.getenv("EDITION", default="SELF_HOSTED") # ce edition first
  27. # create app
  28. app = create_app()
  29. celery = app.extensions["celery"]
  30. if app.config.get("TESTING"):
  31. print("App is running in TESTING mode")
  32. @app.after_request
  33. def after_request(response):
  34. """Add Version headers to the response."""
  35. response.set_cookie("remember_token", "", expires=0)
  36. response.headers.add("X-Version", app.config["CURRENT_VERSION"])
  37. response.headers.add("X-Env", app.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": app.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)