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.

entrypoint.sh 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 " --consumer-no-beg=<num> Start range for consumers (if using range-based)."
  12. echo " --consumer-no-end=<num> End range for consumers (if using range-based)."
  13. echo " --workers=<num> Number of task executors to run (if range is not used)."
  14. echo " --host-id=<string> Unique ID for the host (defaults to \`hostname\`)."
  15. echo
  16. echo "Examples:"
  17. echo " $0 --disable-taskexecutor"
  18. echo " $0 --disable-webserver --consumer-no-beg=0 --consumer-no-end=5"
  19. echo " $0 --disable-webserver --workers=2 --host-id=myhost123"
  20. exit 1
  21. }
  22. ENABLE_WEBSERVER=1 # Default to enable web server
  23. ENABLE_TASKEXECUTOR=1 # Default to enable task executor
  24. CONSUMER_NO_BEG=0
  25. CONSUMER_NO_END=0
  26. WORKERS=1
  27. # -----------------------------------------------------------------------------
  28. # Host ID logic:
  29. # 1. By default, use the system hostname if length <= 32
  30. # 2. Otherwise, use the full MD5 hash of the hostname (32 hex chars)
  31. # -----------------------------------------------------------------------------
  32. CURRENT_HOSTNAME="$(hostname)"
  33. if [ ${#CURRENT_HOSTNAME} -le 32 ]; then
  34. DEFAULT_HOST_ID="$CURRENT_HOSTNAME"
  35. else
  36. DEFAULT_HOST_ID="$(echo -n "$CURRENT_HOSTNAME" | md5sum | cut -d ' ' -f 1)"
  37. fi
  38. HOST_ID="$DEFAULT_HOST_ID"
  39. # Parse arguments
  40. for arg in "$@"; do
  41. case $arg in
  42. --disable-webserver)
  43. ENABLE_WEBSERVER=0
  44. shift
  45. ;;
  46. --disable-taskexecutor)
  47. ENABLE_TASKEXECUTOR=0
  48. shift
  49. ;;
  50. --consumer-no-beg=*)
  51. CONSUMER_NO_BEG="${arg#*=}"
  52. shift
  53. ;;
  54. --consumer-no-end=*)
  55. CONSUMER_NO_END="${arg#*=}"
  56. shift
  57. ;;
  58. --workers=*)
  59. WORKERS="${arg#*=}"
  60. shift
  61. ;;
  62. --host-id=*)
  63. HOST_ID="${arg#*=}"
  64. shift
  65. ;;
  66. *)
  67. usage
  68. ;;
  69. esac
  70. done
  71. # -----------------------------------------------------------------------------
  72. # Replace env variables in the service_conf.yaml file
  73. # -----------------------------------------------------------------------------
  74. CONF_DIR="/ragflow/conf"
  75. TEMPLATE_FILE="${CONF_DIR}/service_conf.yaml.template"
  76. CONF_FILE="${CONF_DIR}/service_conf.yaml"
  77. rm -f "${CONF_FILE}"
  78. while IFS= read -r line || [[ -n "$line" ]]; do
  79. eval "echo \"$line\"" >> "${CONF_FILE}"
  80. done < "${TEMPLATE_FILE}"
  81. export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/"
  82. PY=python3
  83. # -----------------------------------------------------------------------------
  84. # Function(s)
  85. # -----------------------------------------------------------------------------
  86. function task_exe() {
  87. local consumer_id="$1"
  88. local host_id="$2"
  89. JEMALLOC_PATH="$(pkg-config --variable=libdir jemalloc)/libjemalloc.so"
  90. while true; do
  91. LD_PRELOAD="$JEMALLOC_PATH" \
  92. "$PY" rag/svr/task_executor.py "${host_id}_${consumer_id}"
  93. done
  94. }
  95. # -----------------------------------------------------------------------------
  96. # Start components based on flags
  97. # -----------------------------------------------------------------------------
  98. if [[ "${ENABLE_WEBSERVER}" -eq 1 ]]; then
  99. echo "Starting nginx..."
  100. /usr/sbin/nginx
  101. echo "Starting ragflow_server..."
  102. while true; do
  103. "$PY" api/ragflow_server.py
  104. done &
  105. fi
  106. if [[ "${ENABLE_TASKEXECUTOR}" -eq 1 ]]; then
  107. if [[ "${CONSUMER_NO_END}" -gt "${CONSUMER_NO_BEG}" ]]; then
  108. echo "Starting task executors on host '${HOST_ID}' for IDs in [${CONSUMER_NO_BEG}, ${CONSUMER_NO_END})..."
  109. for (( i=CONSUMER_NO_BEG; i<CONSUMER_NO_END; i++ ))
  110. do
  111. task_exe "${i}" "${HOST_ID}" &
  112. done
  113. else
  114. # Otherwise, start a fixed number of workers
  115. echo "Starting ${WORKERS} task executor(s) on host '${HOST_ID}'..."
  116. for (( i=0; i<WORKERS; i++ ))
  117. do
  118. task_exe "${i}" "${HOST_ID}" &
  119. done
  120. fi
  121. fi
  122. wait