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:

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 Variable Overrides and Defaults


Signal Handling and Cleanup

cleanup function

Handles termination signals (SIGINT, SIGTERM) by:

cleanup()

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
    ...
}

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()

Example:

run_server &

Main Execution Flow

  1. Load environment variables.

  2. Unset proxy environment variables.

  3. Set environment variables for Python, jemalloc, and NLTK data.

  4. Determine worker count WS.

  5. Start WS instances of task_executor.py in the background, storing their PIDs.

  6. Start the main server in the background, storing its PID.

  7. Wait for all child processes to exit.

  8. On termination signals, cleanup is triggered to kill all child processes.


Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Example

To launch the backend service with 3 worker executors:

export WS=3
./launch_backend_service.sh

This will:


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.