2024-08-15 09:17:36 +08:00
|
|
|
#
|
|
|
|
|
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
|
2026-03-25 18:17:52 +08:00
|
|
|
print("Start RAGFlow server...")
|
2024-11-18 17:38:17 +08:00
|
|
|
|
2026-01-12 12:48:23 +08:00
|
|
|
import time
|
|
|
|
|
start_ts = time.time()
|
2024-11-14 17:13:48 +08:00
|
|
|
|
2024-12-10 09:36:59 +08:00
|
|
|
import os
|
2026-05-13 18:01:33 +08:00
|
|
|
|
|
|
|
|
# LiteLLM fetches a model cost map from GitHub during import unless this is set.
|
|
|
|
|
# The API server should not block startup on external network access.
|
|
|
|
|
os.environ.setdefault("LITELLM_LOCAL_MODEL_COST_MAP", "True")
|
|
|
|
|
|
|
|
|
|
import logging
|
2024-08-15 09:17:36 +08:00
|
|
|
import signal
|
|
|
|
|
import sys
|
2025-02-28 14:52:40 +08:00
|
|
|
import threading
|
2025-03-20 17:01:28 +08:00
|
|
|
import uuid
|
2025-11-26 11:06:37 +08:00
|
|
|
import faulthandler
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-12-12 10:23:40 +08:00
|
|
|
from api.apps import app
|
2024-08-15 09:17:36 +08:00
|
|
|
from api.db.runtime_config import RuntimeConfig
|
|
|
|
|
from api.db.services.document_service import DocumentService
|
2025-11-02 21:05:28 +08:00
|
|
|
from common.file_utils import get_project_base_directory
|
2025-11-06 09:36:38 +08:00
|
|
|
from common import settings
|
2024-08-15 09:17:36 +08:00
|
|
|
from api.db.db_models import init_database_tables as init_web_db
|
2025-11-24 19:02:08 +08:00
|
|
|
from api.db.init_data import init_web_data, init_superuser
|
2025-11-06 19:24:46 +08:00
|
|
|
from common.versions import get_ragflow_version
|
2025-11-03 17:25:06 +08:00
|
|
|
from common.config_utils import show_configs
|
2025-11-17 15:34:17 +08:00
|
|
|
from common.mcp_tool_call_conn import shutdown_all_mcp_sessions
|
2026-01-12 12:48:23 +08:00
|
|
|
from common.log_utils import init_root_logger
|
2026-01-29 14:23:26 +08:00
|
|
|
from agent.plugin import GlobalPluginManager
|
2025-03-10 15:15:06 +08:00
|
|
|
from rag.utils.redis_conn import RedisDistributedLock
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-02-28 14:52:40 +08:00
|
|
|
stop_event = threading.Event()
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-03-26 15:34:42 +08:00
|
|
|
RAGFLOW_DEBUGPY_LISTEN = int(os.environ.get('RAGFLOW_DEBUGPY_LISTEN', "0"))
|
|
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
def update_progress():
|
2025-03-20 17:01:28 +08:00
|
|
|
lock_value = str(uuid.uuid4())
|
|
|
|
|
redis_lock = RedisDistributedLock("update_progress", lock_value=lock_value, timeout=60)
|
|
|
|
|
logging.info(f"update_progress lock_value: {lock_value}")
|
2025-02-28 14:52:40 +08:00
|
|
|
while not stop_event.is_set():
|
2024-08-15 09:17:36 +08:00
|
|
|
try:
|
2025-03-20 17:01:28 +08:00
|
|
|
if redis_lock.acquire():
|
|
|
|
|
DocumentService.update_progress()
|
|
|
|
|
redis_lock.release()
|
2024-11-12 17:35:13 +08:00
|
|
|
except Exception:
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.exception("update_progress exception")
|
2025-04-14 10:23:13 +08:00
|
|
|
finally:
|
2025-08-19 09:42:15 +08:00
|
|
|
try:
|
|
|
|
|
redis_lock.release()
|
|
|
|
|
except Exception:
|
|
|
|
|
logging.exception("update_progress exception")
|
|
|
|
|
stop_event.wait(6)
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-02-28 14:52:40 +08:00
|
|
|
def signal_handler(sig, frame):
|
|
|
|
|
logging.info("Received interrupt signal, shutting down...")
|
2025-06-26 13:52:01 +08:00
|
|
|
shutdown_all_mcp_sessions()
|
2025-02-28 14:52:40 +08:00
|
|
|
stop_event.set()
|
2025-12-01 14:24:06 +08:00
|
|
|
stop_event.wait(1)
|
2025-02-28 14:52:40 +08:00
|
|
|
sys.exit(0)
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2024-11-12 17:35:13 +08:00
|
|
|
if __name__ == '__main__':
|
2025-11-26 11:06:37 +08:00
|
|
|
faulthandler.enable()
|
|
|
|
|
init_root_logger("ragflow_server")
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info(r"""
|
2025-08-15 18:12:20 +08:00
|
|
|
____ ___ ______ ______ __
|
2024-09-29 16:28:07 +08:00
|
|
|
/ __ \ / | / ____// ____// /____ _ __
|
|
|
|
|
/ /_/ // /| | / / __ / /_ / // __ \| | /| / /
|
2025-08-15 18:12:20 +08:00
|
|
|
/ _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
|
|
|
|
|
/_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2024-11-12 17:35:13 +08:00
|
|
|
""")
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info(
|
2024-11-14 17:51:21 +08:00
|
|
|
f'RAGFlow version: {get_ragflow_version()}'
|
2024-11-14 15:51:30 +08:00
|
|
|
)
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info(
|
2025-11-02 21:05:28 +08:00
|
|
|
f'project base: {get_project_base_directory()}'
|
2024-08-15 09:17:36 +08:00
|
|
|
)
|
Print configs when startup RAGFlow server (#3414)
### What problem does this PR solve?
Print configs at the RAGFlow startup phase.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
```
2024-11-14 21:27:53,090 INFO 962231 Current configs, from /home/weilongma/Documents/development/ragflow/conf/service_conf.yaml:
2024-11-14 21:27:53,090 INFO 962231 ragflow: {'host': '0.0.0.0', 'http_port': 9380}
2024-11-14 21:27:53,090 INFO 962231 mysql: {'name': 'rag_flow', 'user': 'root', 'password': 'infini_rag_flow', 'host': 'mysql', 'port': 5455, 'max_connections': 100, 'stale_timeout': 30}
2024-11-14 21:27:53,090 INFO 962231 minio: {'user': 'rag_flow', 'password': 'infini_rag_flow', 'host': 'minio:9000'}
2024-11-14 21:27:53,090 INFO 962231 es: {'hosts': 'http://es01:1200', 'username': 'elastic', 'password': 'infini_rag_flow'}
2024-11-14 21:27:53,090 INFO 962231 redis: {'db': 1, 'password': 'infini_rag_flow', 'host': 'redis:6379'}
```
Signed-off-by: jinhai <haijin.chn@gmail.com>
2024-11-15 09:29:40 +08:00
|
|
|
show_configs()
|
2024-11-15 17:30:56 +08:00
|
|
|
settings.init_settings()
|
2025-11-06 09:36:38 +08:00
|
|
|
settings.print_rag_settings()
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-03-26 15:34:42 +08:00
|
|
|
if RAGFLOW_DEBUGPY_LISTEN > 0:
|
|
|
|
|
logging.info(f"debugpy listen on {RAGFLOW_DEBUGPY_LISTEN}")
|
|
|
|
|
import debugpy
|
|
|
|
|
debugpy.listen(("0.0.0.0", RAGFLOW_DEBUGPY_LISTEN))
|
|
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
# init db
|
|
|
|
|
init_web_db()
|
|
|
|
|
init_web_data()
|
|
|
|
|
# init runtime config
|
|
|
|
|
import argparse
|
2024-11-04 08:35:36 +01:00
|
|
|
|
2024-08-15 09:17:36 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
2024-11-04 08:35:36 +01:00
|
|
|
parser.add_argument(
|
Print configs when startup RAGFlow server (#3414)
### What problem does this PR solve?
Print configs at the RAGFlow startup phase.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
```
2024-11-14 21:27:53,090 INFO 962231 Current configs, from /home/weilongma/Documents/development/ragflow/conf/service_conf.yaml:
2024-11-14 21:27:53,090 INFO 962231 ragflow: {'host': '0.0.0.0', 'http_port': 9380}
2024-11-14 21:27:53,090 INFO 962231 mysql: {'name': 'rag_flow', 'user': 'root', 'password': 'infini_rag_flow', 'host': 'mysql', 'port': 5455, 'max_connections': 100, 'stale_timeout': 30}
2024-11-14 21:27:53,090 INFO 962231 minio: {'user': 'rag_flow', 'password': 'infini_rag_flow', 'host': 'minio:9000'}
2024-11-14 21:27:53,090 INFO 962231 es: {'hosts': 'http://es01:1200', 'username': 'elastic', 'password': 'infini_rag_flow'}
2024-11-14 21:27:53,090 INFO 962231 redis: {'db': 1, 'password': 'infini_rag_flow', 'host': 'redis:6379'}
```
Signed-off-by: jinhai <haijin.chn@gmail.com>
2024-11-15 09:29:40 +08:00
|
|
|
"--version", default=False, help="RAGFlow version", action="store_true"
|
2024-11-04 08:35:36 +01:00
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--debug", default=False, help="debug mode", action="store_true"
|
|
|
|
|
)
|
2025-11-24 19:02:08 +08:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--init-superuser", default=False, help="init superuser", action="store_true"
|
|
|
|
|
)
|
2024-08-15 09:17:36 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
if args.version:
|
2024-11-14 17:51:21 +08:00
|
|
|
print(get_ragflow_version())
|
2024-08-15 09:17:36 +08:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
2025-11-24 19:02:08 +08:00
|
|
|
if args.init_superuser:
|
|
|
|
|
init_superuser()
|
2024-08-15 09:17:36 +08:00
|
|
|
RuntimeConfig.DEBUG = args.debug
|
|
|
|
|
if RuntimeConfig.DEBUG:
|
2024-11-14 17:13:48 +08:00
|
|
|
logging.info("run on debug mode")
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
RuntimeConfig.init_env()
|
2024-11-15 17:30:56 +08:00
|
|
|
RuntimeConfig.init_config(JOB_SERVER_HOST=settings.HOST_IP, HTTP_PORT=settings.HOST_PORT)
|
2024-08-15 09:17:36 +08:00
|
|
|
|
2025-05-16 16:32:19 +08:00
|
|
|
GlobalPluginManager.load_plugins()
|
|
|
|
|
|
2025-02-28 14:52:40 +08:00
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
|
|
2025-05-30 13:38:30 +08:00
|
|
|
def delayed_start_update_progress():
|
|
|
|
|
logging.info("Starting update_progress thread (delayed)")
|
|
|
|
|
t = threading.Thread(target=update_progress, daemon=True)
|
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
|
if RuntimeConfig.DEBUG:
|
|
|
|
|
if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
|
|
|
|
|
threading.Timer(1.0, delayed_start_update_progress).start()
|
|
|
|
|
else:
|
|
|
|
|
threading.Timer(1.0, delayed_start_update_progress).start()
|
2024-08-15 09:17:36 +08:00
|
|
|
|
|
|
|
|
# start http server
|
|
|
|
|
try:
|
2026-01-12 12:48:23 +08:00
|
|
|
logging.info(f"RAGFlow server is ready after {time.time() - start_ts}s initialization.")
|
2026-04-10 18:01:49 +08:00
|
|
|
app.run(host=settings.HOST_IP, port=settings.HOST_PORT, use_reloader=RuntimeConfig.DEBUG, debug=False)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.exception(f"Unhandled exception: {e}")
|
2025-02-28 14:52:40 +08:00
|
|
|
stop_event.set()
|
2025-12-01 14:24:06 +08:00
|
|
|
stop_event.wait(1)
|
2024-11-04 08:35:36 +01:00
|
|
|
os.kill(os.getpid(), signal.SIGKILL)
|