Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

entrypoint.sh 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. MCP_MODE="self-host"
  35. MCP_HOST_API_KEY=""
  36. MCP_TRANSPORT_SSE_FLAG="--transport-sse-enabled"
  37. MCP_TRANSPORT_STREAMABLE_HTTP_FLAG="--transport-streamable-http-enabled"
  38. MCP_JSON_RESPONSE_FLAG="--json-response"
  39. # -----------------------------------------------------------------------------
  40. # Host ID logic:
  41. # 1. By default, use the system hostname if length <= 32
  42. # 2. Otherwise, use the full MD5 hash of the hostname (32 hex chars)
  43. # -----------------------------------------------------------------------------
  44. CURRENT_HOSTNAME="$(hostname)"
  45. if [ ${#CURRENT_HOSTNAME} -le 32 ]; then
  46. DEFAULT_HOST_ID="$CURRENT_HOSTNAME"
  47. else
  48. DEFAULT_HOST_ID="$(echo -n "$CURRENT_HOSTNAME" | md5sum | cut -d ' ' -f 1)"
  49. fi
  50. HOST_ID="$DEFAULT_HOST_ID"
  51. # Parse arguments
  52. for arg in "$@"; do
  53. case $arg in
  54. --disable-webserver)
  55. ENABLE_WEBSERVER=0
  56. shift
  57. ;;
  58. --disable-taskexecutor)
  59. ENABLE_TASKEXECUTOR=0
  60. shift
  61. ;;
  62. --enable-mcpserver)
  63. ENABLE_MCP_SERVER=1
  64. shift
  65. ;;
  66. --mcp-host=*)
  67. MCP_HOST="${arg#*=}"
  68. shift
  69. ;;
  70. --mcp-port=*)
  71. MCP_PORT="${arg#*=}"
  72. shift
  73. ;;
  74. --mcp-base-url=*)
  75. MCP_BASE_URL="${arg#*=}"
  76. shift
  77. ;;
  78. --mcp-mode=*)
  79. MCP_MODE="${arg#*=}"
  80. shift
  81. ;;
  82. --mcp-host-api-key=*)
  83. MCP_HOST_API_KEY="${arg#*=}"
  84. shift
  85. ;;
  86. --mcp-script-path=*)
  87. MCP_SCRIPT_PATH="${arg#*=}"
  88. shift
  89. ;;
  90. --no-transport-sse-enabled)
  91. MCP_TRANSPORT_SSE_FLAG="--no-transport-sse-enabled"
  92. shift
  93. ;;
  94. --no-transport-streamable-http-enabled)
  95. MCP_TRANSPORT_STREAMABLE_HTTP_FLAG="--no-transport-streamable-http-enabled"
  96. shift
  97. ;;
  98. --no-json-response)
  99. MCP_JSON_RESPONSE_FLAG="--no-json-response"
  100. shift
  101. ;;
  102. --consumer-no-beg=*)
  103. CONSUMER_NO_BEG="${arg#*=}"
  104. shift
  105. ;;
  106. --consumer-no-end=*)
  107. CONSUMER_NO_END="${arg#*=}"
  108. shift
  109. ;;
  110. --workers=*)
  111. WORKERS="${arg#*=}"
  112. shift
  113. ;;
  114. --host-id=*)
  115. HOST_ID="${arg#*=}"
  116. shift
  117. ;;
  118. *)
  119. usage
  120. ;;
  121. esac
  122. done
  123. # -----------------------------------------------------------------------------
  124. # Replace env variables in the service_conf.yaml file
  125. # -----------------------------------------------------------------------------
  126. CONF_DIR="/ragflow/conf"
  127. TEMPLATE_FILE="${CONF_DIR}/service_conf.yaml.template"
  128. CONF_FILE="${CONF_DIR}/service_conf.yaml"
  129. rm -f "${CONF_FILE}"
  130. while IFS= read -r line || [[ -n "$line" ]]; do
  131. eval "echo \"$line\"" >> "${CONF_FILE}"
  132. done < "${TEMPLATE_FILE}"
  133. export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/"
  134. PY=python3
  135. # -----------------------------------------------------------------------------
  136. # Function(s)
  137. # -----------------------------------------------------------------------------
  138. function task_exe() {
  139. local consumer_id="$1"
  140. local host_id="$2"
  141. JEMALLOC_PATH="$(pkg-config --variable=libdir jemalloc)/libjemalloc.so"
  142. while true; do
  143. LD_PRELOAD="$JEMALLOC_PATH" \
  144. "$PY" rag/svr/task_executor.py "${host_id}_${consumer_id}"
  145. done
  146. }
  147. function start_mcp_server() {
  148. echo "Starting MCP Server on ${MCP_HOST}:${MCP_PORT} with base URL ${MCP_BASE_URL}..."
  149. "$PY" "${MCP_SCRIPT_PATH}" \
  150. --host="${MCP_HOST}" \
  151. --port="${MCP_PORT}" \
  152. --base-url="${MCP_BASE_URL}" \
  153. --mode="${MCP_MODE}" \
  154. --api-key="${MCP_HOST_API_KEY}" \
  155. "${MCP_TRANSPORT_SSE_FLAG}" \
  156. "${MCP_TRANSPORT_STREAMABLE_HTTP_FLAG}" \
  157. "${MCP_JSON_RESPONSE_FLAG}" &
  158. }
  159. # -----------------------------------------------------------------------------
  160. # Start components based on flags
  161. # -----------------------------------------------------------------------------
  162. if [[ "${ENABLE_WEBSERVER}" -eq 1 ]]; then
  163. echo "Starting nginx..."
  164. /usr/sbin/nginx
  165. echo "Starting ragflow_server..."
  166. while true; do
  167. "$PY" api/ragflow_server.py
  168. done &
  169. fi
  170. if [[ "${ENABLE_MCP_SERVER}" -eq 1 ]]; then
  171. start_mcp_server
  172. fi
  173. if [[ "${ENABLE_TASKEXECUTOR}" -eq 1 ]]; then
  174. if [[ "${CONSUMER_NO_END}" -gt "${CONSUMER_NO_BEG}" ]]; then
  175. echo "Starting task executors on host '${HOST_ID}' for IDs in [${CONSUMER_NO_BEG}, ${CONSUMER_NO_END})..."
  176. for (( i=CONSUMER_NO_BEG; i<CONSUMER_NO_END; i++ ))
  177. do
  178. task_exe "${i}" "${HOST_ID}" &
  179. done
  180. else
  181. # Otherwise, start a fixed number of workers
  182. echo "Starting ${WORKERS} task executor(s) on host '${HOST_ID}'..."
  183. for (( i=0; i<WORKERS; i++ ))
  184. do
  185. task_exe "${i}" "${HOST_ID}" &
  186. done
  187. fi
  188. fi
  189. wait