您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ragflow_server.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. from concurrent.futures import ThreadPoolExecutor
  29. import threading
  30. import uuid
  31. from werkzeug.serving import run_simple
  32. from api import settings
  33. from api.apps import app
  34. from api.db.runtime_config import RuntimeConfig
  35. from api.db.services.document_service import DocumentService
  36. from api import utils
  37. from api.db.db_models import init_database_tables as init_web_db
  38. from api.db.init_data import init_web_data
  39. from api.versions import get_ragflow_version
  40. from api.utils import show_configs
  41. from rag.settings import print_rag_settings
  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. stop_event.set()
  62. time.sleep(1)
  63. sys.exit(0)
  64. if __name__ == '__main__':
  65. logging.info(r"""
  66. ____ ___ ______ ______ __
  67. / __ \ / | / ____// ____// /____ _ __
  68. / /_/ // /| | / / __ / /_ / // __ \| | /| / /
  69. / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
  70. /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
  71. """)
  72. logging.info(
  73. f'RAGFlow version: {get_ragflow_version()}'
  74. )
  75. logging.info(
  76. f'project base: {utils.file_utils.get_project_base_directory()}'
  77. )
  78. show_configs()
  79. settings.init_settings()
  80. print_rag_settings()
  81. if RAGFLOW_DEBUGPY_LISTEN > 0:
  82. logging.info(f"debugpy listen on {RAGFLOW_DEBUGPY_LISTEN}")
  83. import debugpy
  84. debugpy.listen(("0.0.0.0", RAGFLOW_DEBUGPY_LISTEN))
  85. # init db
  86. init_web_db()
  87. init_web_data()
  88. # init runtime config
  89. import argparse
  90. parser = argparse.ArgumentParser()
  91. parser.add_argument(
  92. "--version", default=False, help="RAGFlow version", action="store_true"
  93. )
  94. parser.add_argument(
  95. "--debug", default=False, help="debug mode", action="store_true"
  96. )
  97. args = parser.parse_args()
  98. if args.version:
  99. print(get_ragflow_version())
  100. sys.exit(0)
  101. RuntimeConfig.DEBUG = args.debug
  102. if RuntimeConfig.DEBUG:
  103. logging.info("run on debug mode")
  104. RuntimeConfig.init_env()
  105. RuntimeConfig.init_config(JOB_SERVER_HOST=settings.HOST_IP, HTTP_PORT=settings.HOST_PORT)
  106. GlobalPluginManager.load_plugins()
  107. signal.signal(signal.SIGINT, signal_handler)
  108. signal.signal(signal.SIGTERM, signal_handler)
  109. thread = ThreadPoolExecutor(max_workers=1)
  110. thread.submit(update_progress)
  111. # start http server
  112. try:
  113. logging.info("RAGFlow HTTP server start...")
  114. run_simple(
  115. hostname=settings.HOST_IP,
  116. port=settings.HOST_PORT,
  117. application=app,
  118. threaded=True,
  119. use_reloader=RuntimeConfig.DEBUG,
  120. use_debugger=RuntimeConfig.DEBUG,
  121. )
  122. except Exception:
  123. traceback.print_exc()
  124. stop_event.set()
  125. time.sleep(1)
  126. os.kill(os.getpid(), signal.SIGKILL)