Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

launch_backend_service.sh 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. PY=python3
  9. # Set default number of workers if WS is not set or less than 1
  10. if [[ -z "$WS" || $WS -lt 1 ]]; then
  11. WS=1
  12. fi
  13. # Maximum number of retries for each task executor and server
  14. MAX_RETRIES=5
  15. # Flag to control termination
  16. STOP=false
  17. # Array to keep track of child PIDs
  18. PIDS=()
  19. # Function to handle termination signals
  20. cleanup() {
  21. echo "Termination signal received. Shutting down..."
  22. STOP=true
  23. # Terminate all child processes
  24. for pid in "${PIDS[@]}"; do
  25. if kill -0 "$pid" 2>/dev/null; then
  26. echo "Killing process $pid"
  27. kill "$pid"
  28. fi
  29. done
  30. exit 0
  31. }
  32. # Trap SIGINT and SIGTERM to invoke cleanup
  33. trap cleanup SIGINT SIGTERM
  34. # Function to execute task_executor with retry logic
  35. task_exe(){
  36. local task_id=$1
  37. local retry_count=0
  38. while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
  39. echo "Starting task_executor.py for task $task_id (Attempt $((retry_count+1)))"
  40. $PY rag/svr/task_executor.py "$task_id"
  41. EXIT_CODE=$?
  42. if [ $EXIT_CODE -eq 0 ]; then
  43. echo "task_executor.py for task $task_id exited successfully."
  44. break
  45. else
  46. echo "task_executor.py for task $task_id failed with exit code $EXIT_CODE. Retrying..." >&2
  47. retry_count=$((retry_count + 1))
  48. sleep 2
  49. fi
  50. done
  51. if [ $retry_count -ge $MAX_RETRIES ]; then
  52. echo "task_executor.py for task $task_id failed after $MAX_RETRIES attempts. Exiting..." >&2
  53. cleanup
  54. fi
  55. }
  56. # Function to execute ragflow_server with retry logic
  57. run_server(){
  58. local retry_count=0
  59. while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
  60. echo "Starting ragflow_server.py (Attempt $((retry_count+1)))"
  61. $PY api/ragflow_server.py
  62. EXIT_CODE=$?
  63. if [ $EXIT_CODE -eq 0 ]; then
  64. echo "ragflow_server.py exited successfully."
  65. break
  66. else
  67. echo "ragflow_server.py 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 "ragflow_server.py failed after $MAX_RETRIES attempts. Exiting..." >&2
  74. cleanup
  75. fi
  76. }
  77. # Start task executors
  78. for ((i=0;i<WS;i++))
  79. do
  80. task_exe "$i" &
  81. PIDS+=($!)
  82. done
  83. # Start the main server
  84. run_server &
  85. PIDS+=($!)
  86. # Wait for all background processes to finish
  87. wait