Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

launch_backend_service.sh 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. # Exit immediately if a command exits with a non-zero status
  3. set -e
  4. # Function to load environment variables from .env file
  5. load_env_file() {
  6. # Get the directory of the current script
  7. local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  8. local env_file="$script_dir/.env"
  9. # Check if .env file exists
  10. if [ -f "$env_file" ]; then
  11. echo "Loading environment variables from: $env_file"
  12. # Source the .env file
  13. set -a
  14. source "$env_file"
  15. set +a
  16. else
  17. echo "Warning: .env file not found at: $env_file"
  18. fi
  19. }
  20. # Load environment variables
  21. load_env_file
  22. # Unset HTTP proxies that might be set by Docker daemon
  23. export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""
  24. export PYTHONPATH=$(pwd)
  25. export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/
  26. JEMALLOC_PATH=$(pkg-config --variable=libdir jemalloc)/libjemalloc.so
  27. PY=python3
  28. # Set default number of workers if WS is not set or less than 1
  29. if [[ -z "$WS" || $WS -lt 1 ]]; then
  30. WS=1
  31. fi
  32. # Maximum number of retries for each task executor and server
  33. MAX_RETRIES=5
  34. # Flag to control termination
  35. STOP=false
  36. # Array to keep track of child PIDs
  37. PIDS=()
  38. # Set the path to the NLTK data directory
  39. export NLTK_DATA="./nltk_data"
  40. # Function to handle termination signals
  41. cleanup() {
  42. echo "Termination signal received. Shutting down..."
  43. STOP=true
  44. # Terminate all child processes
  45. for pid in "${PIDS[@]}"; do
  46. if kill -0 "$pid" 2>/dev/null; then
  47. echo "Killing process $pid"
  48. kill "$pid"
  49. fi
  50. done
  51. exit 0
  52. }
  53. # Trap SIGINT and SIGTERM to invoke cleanup
  54. trap cleanup SIGINT SIGTERM
  55. # Function to execute task_executor with retry logic
  56. task_exe(){
  57. local task_id=$1
  58. local retry_count=0
  59. while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
  60. echo "Starting task_executor.py for task $task_id (Attempt $((retry_count+1)))"
  61. LD_PRELOAD=$JEMALLOC_PATH $PY rag/svr/task_executor.py "$task_id"
  62. EXIT_CODE=$?
  63. if [ $EXIT_CODE -eq 0 ]; then
  64. echo "task_executor.py for task $task_id exited successfully."
  65. break
  66. else
  67. echo "task_executor.py for task $task_id failed with exit code $EXIT_CODE. Retrying..." >&2
  68. retry_count=$((retry_count + 1))
  69. sleep 2
  70. fi
  71. done
  72. if [ $retry_count -ge $MAX_RETRIES ]; then
  73. echo "task_executor.py for task $task_id failed after $MAX_RETRIES attempts. Exiting..." >&2
  74. cleanup
  75. fi
  76. }
  77. # Function to execute ragflow_server with retry logic
  78. run_server(){
  79. local retry_count=0
  80. while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
  81. echo "Starting ragflow_server.py (Attempt $((retry_count+1)))"
  82. $PY api/ragflow_server.py
  83. EXIT_CODE=$?
  84. if [ $EXIT_CODE -eq 0 ]; then
  85. echo "ragflow_server.py exited successfully."
  86. break
  87. else
  88. echo "ragflow_server.py failed with exit code $EXIT_CODE. Retrying..." >&2
  89. retry_count=$((retry_count + 1))
  90. sleep 2
  91. fi
  92. done
  93. if [ $retry_count -ge $MAX_RETRIES ]; then
  94. echo "ragflow_server.py failed after $MAX_RETRIES attempts. Exiting..." >&2
  95. cleanup
  96. fi
  97. }
  98. # Start task executors
  99. for ((i=0;i<WS;i++))
  100. do
  101. task_exe "$i" &
  102. PIDS+=($!)
  103. done
  104. # Start the main server
  105. run_server &
  106. PIDS+=($!)
  107. # Wait for all background processes to finish
  108. wait