Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ragflow_server.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 initRootLogger
  20. from plugin import GlobalPluginManager
  21. initRootLogger("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.redis_conn import RedisDistributedLock
  42. stop_event = threading.Event()
  43. RAGFLOW_DEBUGPY_LISTEN = int(os.environ.get('RAGFLOW_DEBUGPY_LISTEN', "0"))
  44. def update_progress():
  45. lock_value = str(uuid.uuid4())
  46. redis_lock = RedisDistributedLock("update_progress", lock_value=lock_value, timeout=60)
  47. logging.info(f"update_progress lock_value: {lock_value}")
  48. while not stop_event.is_set():
  49. try:
  50. if redis_lock.acquire():
  51. DocumentService.update_progress()
  52. redis_lock.release()
  53. stop_event.wait(6)
  54. except Exception:
  55. logging.exception("update_progress exception")
  56. finally:
  57. redis_lock.release()
  58. def signal_handler(sig, frame):
  59. logging.info("Received interrupt signal, shutting down...")
  60. stop_event.set()
  61. time.sleep(1)
  62. sys.exit(0)
  63. if __name__ == '__main__':
  64. logging.info(r"""
  65. ____ ___ ______ ______ __
  66. / __ \ / | / ____// ____// /____ _ __
  67. / /_/ // /| | / / __ / /_ / // __ \| | /| / /
  68. / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
  69. /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
  70. """)
  71. logging.info(
  72. f'RAGFlow version: {get_ragflow_version()}'
  73. )
  74. logging.info(
  75. f'project base: {utils.file_utils.get_project_base_directory()}'
  76. )
  77. show_configs()
  78. settings.init_settings()
  79. print_rag_settings()
  80. if RAGFLOW_DEBUGPY_LISTEN > 0:
  81. logging.info(f"debugpy listen on {RAGFLOW_DEBUGPY_LISTEN}")
  82. import debugpy
  83. debugpy.listen(("0.0.0.0", RAGFLOW_DEBUGPY_LISTEN))
  84. # init db
  85. init_web_db()
  86. init_web_data()
  87. # init runtime config
  88. import argparse
  89. parser = argparse.ArgumentParser()
  90. parser.add_argument(
  91. "--version", default=False, help="RAGFlow version", action="store_true"
  92. )
  93. parser.add_argument(
  94. "--debug", default=False, help="debug mode", action="store_true"
  95. )
  96. args = parser.parse_args()
  97. if args.version:
  98. print(get_ragflow_version())
  99. sys.exit(0)
  100. RuntimeConfig.DEBUG = args.debug
  101. if RuntimeConfig.DEBUG:
  102. logging.info("run on debug mode")
  103. RuntimeConfig.init_env()
  104. RuntimeConfig.init_config(JOB_SERVER_HOST=settings.HOST_IP, HTTP_PORT=settings.HOST_PORT)
  105. GlobalPluginManager.load_plugins()
  106. signal.signal(signal.SIGINT, signal_handler)
  107. signal.signal(signal.SIGTERM, signal_handler)
  108. def delayed_start_update_progress():
  109. logging.info("Starting update_progress thread (delayed)")
  110. t = threading.Thread(target=update_progress, daemon=True)
  111. t.start()
  112. if RuntimeConfig.DEBUG:
  113. if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
  114. threading.Timer(1.0, delayed_start_update_progress).start()
  115. else:
  116. threading.Timer(1.0, delayed_start_update_progress).start()
  117. # start http server
  118. try:
  119. logging.info("RAGFlow HTTP server start...")
  120. run_simple(
  121. hostname=settings.HOST_IP,
  122. port=settings.HOST_PORT,
  123. application=app,
  124. threaded=True,
  125. use_reloader=RuntimeConfig.DEBUG,
  126. use_debugger=RuntimeConfig.DEBUG,
  127. )
  128. except Exception:
  129. traceback.print_exc()
  130. stop_event.set()
  131. time.sleep(1)
  132. os.kill(os.getpid(), signal.SIGKILL)