stop.sh


Overview

stop.sh is a shell script designed to gracefully stop and clean up the InfiniFlow project's Docker-based services and sandbox containers. It performs two main tasks:

  1. Stops all running Docker Compose services related to the project.

  2. Deletes individual sandbox containers (Python and Node.js sandboxes) based on a configured pool size defined in a .env file.

This script is primarily intended for developers or deployment automation to ensure a clean shutdown and removal of temporary sandbox environments before restarting or redeploying the system.


Detailed Breakdown

Script Behavior and Flow

  1. Set strict error handling
    set -e ensures the script exits immediately if any command fails, preventing partial shutdowns or cleanup.

  2. Determine base directory

    BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)"
    cd "$BASE_DIR"
    

    The script calculates the absolute path to the project root directory (one level up from the script location) and changes the working directory to it. This ensures subsequent commands run relative to the project root.

  3. Stop Docker Compose services

    docker compose down
    

    Uses Docker Compose to stop and remove all containers, networks, and other resources defined in the Compose file. This is the main shutdown step for the entire project services.

  4. Delete sandbox containers

    • Checks if .env exists and sources it to load environment variables.

    • Iterates over sandbox container indices from 0 to SANDBOX_EXECUTOR_MANAGER_POOL_SIZE - 1.

    • For each index, attempts to forcibly remove two containers:

      • sandbox_python_<index>

      • sandbox_nodejs_<index>

    • Suppresses errors if containers do not exist (using || true).

  5. Completion message
    Prints confirmation that stopping and cleanup are complete.


Important Variables and Parameters


Usage Examples

Run the script from anywhere (it automatically resolves project root):

./scripts/stop.sh

Expected output example (assuming SANDBOX_EXECUTOR_MANAGER_POOL_SIZE=2):

🛑 Stopping all services...
Stopping infiflow_database ... done
Stopping infiflow_backend  ... done
Removing infiflow_database ... done
Removing infiflow_backend  ... done

🧹 Deleting sandbox containers...
🧹 Deleting sandbox_python_0...
🧹 Deleting sandbox_nodejs_0...
🧹 Deleting sandbox_python_1...
🧹 Deleting sandbox_nodejs_1...

✅ Stopping and cleanup complete

If .env file is missing:

🛑 Stopping all services...
docker compose down output...

⚠️ .env not found, skipping container cleanup

✅ Stopping and cleanup complete

Implementation Details & Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[Start stop.sh script] --> B[Set -e (Exit on error)]
    B --> C[Determine BASE_DIR and cd to it]
    C --> D[Run 'docker compose down']
    D --> E{Check if .env exists}
    E -- Yes --> F[Source .env variables]
    F --> G[Loop i = 0 to SANDBOX_EXECUTOR_MANAGER_POOL_SIZE - 1]
    G --> H[For each i: Remove sandbox_python_i container]
    H --> I[For each i: Remove sandbox_nodejs_i container]
    I --> J[Print "Stopping and cleanup complete"]
    E -- No --> K[Print warning about missing .env]
    K --> J
    J --> L[End]

Summary

stop.sh is a utility script crucial for safely stopping all InfiniFlow Docker services and cleaning up sandbox executor containers. It ensures that resources are released, and no stale sandbox containers remain, which could interfere with fresh deployments or testing cycles. By dynamically adapting to environment configurations and handling errors gracefully, this script simplifies the development and operational workflow of the InfiniFlow project.