Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ragflow_server.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # from beartype import BeartypeConf
  17. # from beartype.claw import beartype_all # <-- you didn't sign up for this
  18. # beartype_all(conf=BeartypeConf(violation_type=UserWarning)) # <-- emit warnings from all code
  19. from api.utils.log_utils import init_root_logger
  20. from plugin import GlobalPluginManager
  21. init_root_logger("ragflow_server")
  22. import logging
  23. import os
  24. import signal
  25. import sys
  26. import time
  27. import traceback
  28. import threading
  29. import uuid
  30. from werkzeug.serving import run_simple
  31. from api import settings
  32. from api.apps import app
  33. from api.db.runtime_config import RuntimeConfig
  34. from api.db.services.document_service import DocumentService
  35. from api import utils
  36. from api.db.db_models import init_database_tables as init_web_db
  37. from api.db.init_data import init_web_data
  38. from api.versions import get_ragflow_version
  39. from api.utils import show_configs
  40. from rag.settings import print_rag_settings
  41. from rag.utils.mcp_tool_call_conn import shutdown_all_mcp_sessions
  42. from rag.utils.redis_conn import RedisDistributedLock
  43. stop_event = threading.Event()
  44. RAGFLOW_DEBUGPY_LISTEN = int(os.environ.get('RAGFLOW_DEBUGPY_LISTEN', "0"))
  45. def update_progress():
  46. lock_value = str(uuid.uuid4())
  47. redis_lock = RedisDistributedLock("update_progress", lock_value=lock_value, timeout=60)
  48. logging.info(f"update_progress lock_value: {lock_value}")
  49. while not stop_event.is_set():
  50. try:
  51. if redis_lock.acquire():
  52. DocumentService.update_progress()
  53. redis_lock.release()
  54. stop_event.wait(6)
  55. except Exception:
  56. logging.exception("update_progress exception")
  57. finally:
  58. redis_lock.release()
  59. def signal_handler(sig, frame):
  60. logging.info("Received interrupt signal, shutting down...")
  61. shutdown_all_mcp_sessions()
  62. stop_event.set()
  63. time.sleep(1)
  64. sys.exit(0)
  65. if __name__ == '__main__':
  66. logging.info(r"""
  67. ____ ___ ______ ______ __
  68. / __ \ / | / ____// ____// /____ _ __
  69. / /_/ // /| | / / __ / /_ / // __ \| | /| / /
  70. / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
  71. /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
  72. """)
  73. logging.info(
  74. f'RAGFlow version: {get_ragflow_version()}'
  75. )
  76. logging.info(
  77. f'project base: {utils.file_utils.get_project_base_directory()}'
  78. )
  79. show_configs()
  80. settings.init_settings()
  81. print_rag_settings()
  82. if RAGFLOW_DEBUGPY_LISTEN > 0:
  83. logging.info(f"debugpy listen on {RAGFLOW_DEBUGPY_LISTEN}")
  84. import debugpy
  85. debugpy.listen(("0.0.0.0", RAGFLOW_DEBUGPY_LISTEN))
  86. # init db
  87. init_web_db()
  88. init_web_data()
  89. # init runtime config
  90. import argparse
  91. parser = argparse.ArgumentParser()
  92. parser.add_argument(
  93. "--version", default=False, help="RAGFlow version", action="store_true"
  94. )
  95. parser.add_argument(
  96. "--debug", default=False, help="debug mode", action="store_true"
  97. )
  98. args = parser.parse_args()
  99. if args.version:
  100. print(get_ragflow_version())
  101. sys.exit(0)
  102. RuntimeConfig.DEBUG = args.debug
  103. if RuntimeConfig.DEBUG:
  104. logging.info("run on debug mode")
  105. RuntimeConfig.init_env()
  106. RuntimeConfig.init_config(JOB_SERVER_HOST=settings.HOST_IP, HTTP_PORT=settings.HOST_PORT)
  107. GlobalPluginManager.load_plugins()
  108. signal.signal(signal.SIGINT, signal_handler)
  109. signal.signal(signal.SIGTERM, signal_handler)
  110. def delayed_start_update_progress():
  111. logging.info("Starting update_progress thread (delayed)")
  112. t = threading.Thread(target=update_progress, daemon=True)
  113. t.start()
  114. if RuntimeConfig.DEBUG:
  115. if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
  116. threading.Timer(1.0, delayed_start_update_progress).start()
  117. else:
  118. threading.Timer(1.0, delayed_start_update_progress).start()
  119. # start http server
  120. try:
  121. logging.info("RAGFlow HTTP server start...")
  122. run_simple(
  123. hostname=settings.HOST_IP,
  124. port=settings.HOST_PORT,
  125. application=app,
  126. threaded=True,
  127. use_reloader=RuntimeConfig.DEBUG,
  128. use_debugger=RuntimeConfig.DEBUG,
  129. )
  130. except Exception:
  131. traceback.print_exc()
  132. stop_event.set()
  133. time.sleep(1)
  134. os.kill(os.getpid(), signal.SIGKILL)