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.

launch_backend_service.sh 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # Function to handle termination signals
  39. cleanup() {
  40. echo "Termination signal received. Shutting down..."
  41. STOP=true
  42. # Terminate all child processes
  43. for pid in "${PIDS[@]}"; do
  44. if kill -0 "$pid" 2>/dev/null; then
  45. echo "Killing process $pid"
  46. kill "$pid"
  47. fi
  48. done
  49. exit 0
  50. }
  51. # Trap SIGINT and SIGTERM to invoke cleanup
  52. trap cleanup SIGINT SIGTERM
  53. # Function to execute task_executor with retry logic
  54. task_exe(){
  55. local task_id=$1
  56. local retry_count=0
  57. while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
  58. echo "Starting task_executor.py for task $task_id (Attempt $((retry_count+1)))"
  59. LD_PRELOAD=$JEMALLOC_PATH $PY rag/svr/task_executor.py "$task_id"
  60. EXIT_CODE=$?
  61. if [ $EXIT_CODE -eq 0 ]; then
  62. echo "task_executor.py for task $task_id exited successfully."
  63. break
  64. else
  65. echo "task_executor.py for task $task_id failed with exit code $EXIT_CODE. Retrying..." >&2
  66. retry_count=$((retry_count + 1))
  67. sleep 2
  68. fi
  69. done
  70. if [ $retry_count -ge $MAX_RETRIES ]; then
  71. echo "task_executor.py for task $task_id failed after $MAX_RETRIES attempts. Exiting..." >&2
  72. cleanup
  73. fi
  74. }
  75. # Function to execute ragflow_server with retry logic
  76. run_server(){
  77. local retry_count=0
  78. while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
  79. echo "Starting ragflow_server.py (Attempt $((retry_count+1)))"
  80. $PY api/ragflow_server.py
  81. EXIT_CODE=$?
  82. if [ $EXIT_CODE -eq 0 ]; then
  83. echo "ragflow_server.py exited successfully."
  84. break
  85. else
  86. echo "ragflow_server.py failed with exit code $EXIT_CODE. Retrying..." >&2
  87. retry_count=$((retry_count + 1))
  88. sleep 2
  89. fi
  90. done
  91. if [ $retry_count -ge $MAX_RETRIES ]; then
  92. echo "ragflow_server.py failed after $MAX_RETRIES attempts. Exiting..." >&2
  93. cleanup
  94. fi
  95. }
  96. # Start task executors
  97. for ((i=0;i<WS;i++))
  98. do
  99. task_exe "$i" &
  100. PIDS+=($!)
  101. done
  102. # Start the main server
  103. run_server &
  104. PIDS+=($!)
  105. # Wait for all background processes to finish
  106. wait