launch_backend_service.sh
Overview
launch_backend_service.sh is a Bash script designed to initialize and manage the backend service environment for an application. Its primary responsibilities are to:
Load environment variables from a
.envfile if present.Configure necessary environment settings such as proxy variables, library paths, and Python environment.
Launch multiple instances of a Python task executor (
task_executor.py) in parallel, with retry logic on failure.Launch the main backend server (
ragflow_server.py) with retry logic.Handle graceful termination and cleanup of all subprocesses on receiving termination signals.
The script is robust against transient failures of subprocesses, restarting them up to a configured maximum retry count, thereby improving service reliability.
Detailed Explanation
Environment Setup and Configuration
Environment Variables Loading (
load_env_file)This function checks for a
.envfile located in the same directory as the script and loads its environment variables into the current shell session.load_env_file()Parameters: None
Returns: None
Usage: Called once at script start to import environment variables.
Behavior:
Prints a message about loading or warning if
.envis missing.Uses
set -ato export all variables from.env.
Example:
load_env_fileThis will load environment variables like
DATABASE_URL,API_KEY, etc., if defined in.env.
Environment Variable Overrides and Defaults
HTTP proxy variables are explicitly unset to avoid interference from Docker or other proxy settings:
export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""PYTHONPATHis set to the current directory to ensure Python modules are properly located.LD_LIBRARY_PATHis set to a common Linux library directory for dynamic linking.JEMALLOC_PATHis set viapkg-configto preload the jemalloc library, optimizing memory allocation for subprocesses.PYvariable stores the Python interpreter command, defaulting topython3.Number of worker processes (
WS) defaults to 1 if unset or less than 1.Maximum retry attempts for subprocesses is set as 5 (
MAX_RETRIES=5).STOPflag andPIDSarray are initialized to control process termination and track child PIDs.NLTK_DATAenvironment variable points to a local directory for NLTK data.
Signal Handling and Cleanup
cleanup function
Handles termination signals (SIGINT, SIGTERM) by:
Printing a shutdown message.
Setting
STOP=trueto halt retry loops.Iterating over all tracked child PIDs and sending
killsignals.Exiting the script cleanly.
cleanup()
Parameters: None
Returns: None
Usage: Registered as a trap handler for signals.
Example:
trap cleanup SIGINT SIGTERM
Task Executor Management
task_exe function
Manages launching a single instance of the Python task executor script rag/svr/task_executor.py with retry logic.
task_exe() {
local task_id=$1
...
}
Parameters:
task_id(integer) — identifies the executor instance number.
Returns: None (runs until success or max retries reached)
Behavior:
Attempts to run
task_executor.pywithLD_PRELOADset to jemalloc and the task ID as an argument.On failure, retries up to
MAX_RETRIEStimes with 2-second delays.On exceeding retries, calls
cleanupto terminate all processes.
Example:
task_exe 0 &
This starts the first task executor in the background.
Server Management
run_server function
Runs the main backend server script api/ragflow_server.py with retry logic analogous to task_exe.
run_server()
Parameters: None
Returns: None
Behavior:
Runs the server script.
Retries on failure up to
MAX_RETRIES.Calls
cleanupon repeated failure.
Example:
run_server &
Main Execution Flow
Load environment variables.
Unset proxy environment variables.
Set environment variables for Python, jemalloc, and NLTK data.
Determine worker count
WS.Start
WSinstances oftask_executor.pyin the background, storing their PIDs.Start the main server in the background, storing its PID.
Wait for all child processes to exit.
On termination signals, cleanup is triggered to kill all child processes.
Implementation Details and Algorithms
Retry Loop: Both
task_exeandrun_serverimplement a retry loop that attempts to restart the Python scripts up toMAX_RETRIEStimes if they exit with a non-zero code, with a 2-second delay between retries.Process Management: Uses Bash arrays to track PIDs of background processes, allowing for targeted termination during cleanup.
Signal Trapping: Uses
trapto interceptSIGINTandSIGTERMsignals to perform graceful shutdown instead of abrupt termination.Environment Isolation: Prevents interference from external HTTP proxies by unsetting related environment variables, ensuring subprocesses run in a controlled environment.
Memory Optimization: Preloads jemalloc for improved memory allocation performance in subprocesses.
Interaction with Other Parts of the System
Python Scripts:
rag/svr/task_executor.py: Task executor instances that perform discrete backend tasks.api/ragflow_server.py: The main backend server providing API endpoints or service orchestration.
Environment Configuration:
.envfile for environment-specific variables.nltk_datalocal directory for NLP resources utilized by Python scripts.
System Libraries:
Uses jemalloc via
LD_PRELOADto enhance memory management for Python subprocesses.
Docker or Container Environments:
Unsets proxy variables that Docker daemon might inject, avoiding proxy-related side effects.
Usage Example
To launch the backend service with 3 worker executors:
export WS=3
./launch_backend_service.sh
This will:
Load
.envif present.Start 3 parallel instances of
task_executor.pywith task IDs 0, 1, and 2.Start the main
ragflow_server.py.Manage retries and clean shutdown.
Mermaid Flowchart Diagram
flowchart TD
A[Start Script] --> B[Load .env File]
B --> C[Unset Proxy Variables]
C --> D[Set Environment Variables]
D --> E{WS Workers?}
E -->|For each worker| F[Start task_executor.py (task_id)]
F --> G[Add PID to PIDS]
G --> E
E --> H[Start ragflow_server.py]
H --> I[Add PID to PIDS]
I --> J[Wait for all Background Processes]
J --> K{Signal?}
K -->|SIGINT or SIGTERM| L[Call cleanup()]
L --> M[Kill all Child Processes]
M --> N[Exit Script]
K -->|No Signal| J
F --> O[Retry on Failure up to MAX_RETRIES]
H --> P[Retry on Failure up to MAX_RETRIES]
Summary
launch_backend_service.sh is a critical utility script for launching and managing backend service components with robustness and graceful handling of failures and termination signals. It abstracts complexities of environment setup, concurrency, and monitoring of subprocesses, enabling easier deployment and maintenance of backend services.