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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env bash
  2. set -e
  3. # -----------------------------------------------------------------------------
  4. # Usage and command-line argument parsing
  5. # -----------------------------------------------------------------------------
  6. function usage() {
  7. echo "Usage: $0 [--disable-webserver] [--disable-taskexecutor] [--consumer-no-beg=<num>] [--consumer-no-end=<num>] [--workers=<num>] [--host-id=<string>]"
  8. echo
  9. echo " --disable-webserver Disables the web server (nginx + ragflow_server)."
  10. echo " --disable-taskexecutor Disables task executor workers."
  11. echo " --enable-mcpserver Enables the MCP server."
  12. echo " --consumer-no-beg=<num> Start range for consumers (if using range-based)."
  13. echo " --consumer-no-end=<num> End range for consumers (if using range-based)."
  14. echo " --workers=<num> Number of task executors to run (if range is not used)."
  15. echo " --host-id=<string> Unique ID for the host (defaults to \`hostname\`)."
  16. echo
  17. echo "Examples:"
  18. echo " $0 --disable-taskexecutor"
  19. echo " $0 --disable-webserver --consumer-no-beg=0 --consumer-no-end=5"
  20. echo " $0 --disable-webserver --workers=2 --host-id=myhost123"
  21. echo " $0 --enable-mcpserver"
  22. exit 1
  23. }
  24. ENABLE_WEBSERVER=1 # Default to enable web server
  25. ENABLE_TASKEXECUTOR=1 # Default to enable task executor
  26. ENABLE_MCP_SERVER=0
  27. CONSUMER_NO_BEG=0
  28. CONSUMER_NO_END=0
  29. WORKERS=1
  30. MCP_HOST="127.0.0.1"
  31. MCP_PORT=9382
  32. MCP_BASE_URL="http://127.0.0.1:9380"
  33. MCP_SCRIPT_PATH="/ragflow/mcp/server/server.py"
  34. # -----------------------------------------------------------------------------
  35. # Host ID logic:
  36. # 1. By default, use the system hostname if length <= 32
  37. # 2. Otherwise, use the full MD5 hash of the hostname (32 hex chars)
  38. # -----------------------------------------------------------------------------
  39. CURRENT_HOSTNAME="$(hostname)"
  40. if [ ${#CURRENT_HOSTNAME} -le 32 ]; then
  41. DEFAULT_HOST_ID="$CURRENT_HOSTNAME"
  42. else
  43. DEFAULT_HOST_ID="$(echo -n "$CURRENT_HOSTNAME" | md5sum | cut -d ' ' -f 1)"
  44. fi
  45. HOST_ID="$DEFAULT_HOST_ID"
  46. # Parse arguments
  47. for arg in "$@"; do
  48. case $arg in
  49. --disable-webserver)
  50. ENABLE_WEBSERVER=0
  51. shift
  52. ;;
  53. --disable-taskexecutor)
  54. ENABLE_TASKEXECUTOR=0
  55. shift
  56. ;;
  57. --enable-mcpserver)
  58. ENABLE_MCP_SERVER=1
  59. shift
  60. ;;
  61. --mcp-host=*)
  62. MCP_HOST="${arg#*=}"
  63. shift
  64. ;;
  65. --mcp-port=*)
  66. MCP_PORT="${arg#*=}"
  67. shift
  68. ;;
  69. --mcp-base-url=*)
  70. MCP_BASE_URL="${arg#*=}"
  71. shift
  72. ;;
  73. --mcp-script-path=*)
  74. MCP_SCRIPT_PATH="${arg#*=}"
  75. shift
  76. ;;
  77. --consumer-no-beg=*)
  78. CONSUMER_NO_BEG="${arg#*=}"
  79. shift
  80. ;;
  81. --consumer-no-end=*)
  82. CONSUMER_NO_END="${arg#*=}"
  83. shift
  84. ;;
  85. --workers=*)
  86. WORKERS="${arg#*=}"
  87. shift
  88. ;;
  89. --host-id=*)
  90. HOST_ID="${arg#*=}"
  91. shift
  92. ;;
  93. *)
  94. usage
  95. ;;
  96. esac
  97. done
  98. # -----------------------------------------------------------------------------
  99. # Replace env variables in the service_conf.yaml file
  100. # -----------------------------------------------------------------------------
  101. CONF_DIR="/ragflow/conf"
  102. TEMPLATE_FILE="${CONF_DIR}/service_conf.yaml.template"
  103. CONF_FILE="${CONF_DIR}/service_conf.yaml"
  104. rm -f "${CONF_FILE}"
  105. while IFS= read -r line || [[ -n "$line" ]]; do
  106. eval "echo \"$line\"" >> "${CONF_FILE}"
  107. done < "${TEMPLATE_FILE}"
  108. export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/"
  109. PY=python3
  110. # -----------------------------------------------------------------------------
  111. # Function(s)
  112. # -----------------------------------------------------------------------------
  113. function task_exe() {
  114. local consumer_id="$1"
  115. local host_id="$2"
  116. JEMALLOC_PATH="$(pkg-config --variable=libdir jemalloc)/libjemalloc.so"
  117. while true; do
  118. LD_PRELOAD="$JEMALLOC_PATH" \
  119. "$PY" rag/svr/task_executor.py "${host_id}_${consumer_id}"
  120. done
  121. }
  122. function start_mcp_server() {
  123. echo "Starting MCP Server on ${MCP_HOST}:${MCP_PORT} with base URL ${MCP_BASE_URL}..."
  124. "$PY" "${MCP_SCRIPT_PATH}" \
  125. --host="${MCP_HOST}" \
  126. --port="${MCP_PORT}" \
  127. --base_url="${MCP_BASE_URL}" &
  128. }
  129. # -----------------------------------------------------------------------------
  130. # Start components based on flags
  131. # -----------------------------------------------------------------------------
  132. if [[ "${ENABLE_WEBSERVER}" -eq 1 ]]; then
  133. echo "Starting nginx..."
  134. /usr/sbin/nginx
  135. echo "Starting ragflow_server..."
  136. while true; do
  137. "$PY" api/ragflow_server.py
  138. done &
  139. fi
  140. if [[ "${ENABLE_MCP_SERVER}" -eq 1 ]]; then
  141. start_mcp_server
  142. fi
  143. if [[ "${ENABLE_TASKEXECUTOR}" -eq 1 ]]; then
  144. if [[ "${CONSUMER_NO_END}" -gt "${CONSUMER_NO_BEG}" ]]; then
  145. echo "Starting task executors on host '${HOST_ID}' for IDs in [${CONSUMER_NO_BEG}, ${CONSUMER_NO_END})..."
  146. for (( i=CONSUMER_NO_BEG; i<CONSUMER_NO_END; i++ ))
  147. do
  148. task_exe "${i}" "${HOST_ID}" &
  149. done
  150. else
  151. # Otherwise, start a fixed number of workers
  152. echo "Starting ${WORKERS} task executor(s) on host '${HOST_ID}'..."
  153. for (( i=0; i<WORKERS; i++ ))
  154. do
  155. task_exe "${i}" "${HOST_ID}" &
  156. done
  157. fi
  158. fi
  159. wait