You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. from werkzeug.serving import run_simple
  30. from api import settings
  31. from api.apps import app
  32. from api.db.runtime_config import RuntimeConfig
  33. from api.db.services.document_service import DocumentService
  34. from api import utils
  35. from api.db.db_models import init_database_tables as init_web_db
  36. from api.db.init_data import init_web_data
  37. from api.versions import get_ragflow_version
  38. from api.utils import show_configs
  39. from rag.settings import print_rag_settings
  40. from rag.utils.redis_conn import RedisDistributedLock
  41. stop_event = threading.Event()
  42. def update_progress():
  43. redis_lock = RedisDistributedLock("update_progress", timeout=60)
  44. while not stop_event.is_set():
  45. try:
  46. if not redis_lock.acquire():
  47. continue
  48. DocumentService.update_progress()
  49. stop_event.wait(6)
  50. except Exception:
  51. logging.exception("update_progress exception")
  52. finally:
  53. redis_lock.release()
  54. def signal_handler(sig, frame):
  55. logging.info("Received interrupt signal, shutting down...")
  56. stop_event.set()
  57. time.sleep(1)
  58. sys.exit(0)
  59. if __name__ == '__main__':
  60. logging.info(r"""
  61. ____ ___ ______ ______ __
  62. / __ \ / | / ____// ____// /____ _ __
  63. / /_/ // /| | / / __ / /_ / // __ \| | /| / /
  64. / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
  65. /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
  66. """)
  67. logging.info(
  68. f'RAGFlow version: {get_ragflow_version()}'
  69. )
  70. logging.info(
  71. f'project base: {utils.file_utils.get_project_base_directory()}'
  72. )
  73. show_configs()
  74. settings.init_settings()
  75. print_rag_settings()
  76. # init db
  77. init_web_db()
  78. init_web_data()
  79. # init runtime config
  80. import argparse
  81. parser = argparse.ArgumentParser()
  82. parser.add_argument(
  83. "--version", default=False, help="RAGFlow version", action="store_true"
  84. )
  85. parser.add_argument(
  86. "--debug", default=False, help="debug mode", action="store_true"
  87. )
  88. args = parser.parse_args()
  89. if args.version:
  90. print(get_ragflow_version())
  91. sys.exit(0)
  92. RuntimeConfig.DEBUG = args.debug
  93. if RuntimeConfig.DEBUG:
  94. logging.info("run on debug mode")
  95. RuntimeConfig.init_env()
  96. RuntimeConfig.init_config(JOB_SERVER_HOST=settings.HOST_IP, HTTP_PORT=settings.HOST_PORT)
  97. signal.signal(signal.SIGINT, signal_handler)
  98. signal.signal(signal.SIGTERM, signal_handler)
  99. thread = ThreadPoolExecutor(max_workers=1)
  100. thread.submit(update_progress)
  101. # start http server
  102. try:
  103. logging.info("RAGFlow HTTP server start...")
  104. run_simple(
  105. hostname=settings.HOST_IP,
  106. port=settings.HOST_PORT,
  107. application=app,
  108. threaded=True,
  109. use_reloader=RuntimeConfig.DEBUG,
  110. use_debugger=RuntimeConfig.DEBUG,
  111. )
  112. except Exception:
  113. traceback.print_exc()
  114. stop_event.set()
  115. time.sleep(1)
  116. os.kill(os.getpid(), signal.SIGKILL)