Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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