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

ragflow_server.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. initRootLogger("ragflow_server")
  21. import logging
  22. import os
  23. import signal
  24. import sys
  25. import time
  26. import traceback
  27. from concurrent.futures import ThreadPoolExecutor
  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. signal.signal(signal.SIGINT, signal_handler)
  106. signal.signal(signal.SIGTERM, signal_handler)
  107. thread = ThreadPoolExecutor(max_workers=1)
  108. thread.submit(update_progress)
  109. # start http server
  110. try:
  111. logging.info("RAGFlow HTTP server start...")
  112. run_simple(
  113. hostname=settings.HOST_IP,
  114. port=settings.HOST_PORT,
  115. application=app,
  116. threaded=True,
  117. use_reloader=RuntimeConfig.DEBUG,
  118. use_debugger=RuntimeConfig.DEBUG,
  119. )
  120. except Exception:
  121. traceback.print_exc()
  122. stop_event.set()
  123. time.sleep(1)
  124. os.kill(os.getpid(), signal.SIGKILL)