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:
Stops all running Docker Compose services related to the project.
Deletes individual sandbox containers (Python and Node.js sandboxes) based on a configured pool size defined in a
.envfile.
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
Set strict error handling
set -eensures the script exits immediately if any command fails, preventing partial shutdowns or cleanup.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.
Stop Docker Compose services
docker compose downUses 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.
Delete sandbox containers
Checks if
.envexists and sources it to load environment variables.Iterates over sandbox container indices from
0toSANDBOX_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).
Completion message
Prints confirmation that stopping and cleanup are complete.
Important Variables and Parameters
BASE_DIR
Absolute path to the project root directory, derived dynamically..envfile
A configuration file expected to be located in the project root, which must define at least:SANDBOX_EXECUTOR_MANAGER_POOL_SIZE- an integer representing how many sandbox containers exist per type.
Docker container names
Containers follow the naming pattern:sandbox_python_0,sandbox_python_1, ...sandbox_nodejs_0,sandbox_nodejs_1, ...
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
Error Handling:
The script usesset -eto abort on any command failure, ensuring that partial shutdowns or container removals do not occur silently.Dynamic Cleanup Loop:
The container cleanup loop is dynamically sized according to theSANDBOX_EXECUTOR_MANAGER_POOL_SIZEenvironment variable. This allows scaling the number of sandbox containers without changing the script.Silent Removal:
Container removal commands redirect both stdout and stderr to/dev/nulland use|| trueto ignore errors if containers do not exist. This prevents the script from failing if containers are already removed.Environment Variable Sourcing:
The.envfile is sourced to import environment variables into the script's context, a common pattern in Docker-based projects.
Interaction with Other Parts of the System
Docker Compose Configuration:
Invokesdocker compose downto stop all services defined in the project'sdocker-compose.yml. This file defines the main backend, database, and other services used by InfiniFlow.Sandbox Containers:
The script specifically targets sandbox containers for Python and Node.js executors, which are presumably ephemeral environments used for code execution or testing..env Configuration File:
Relies on environment variables defined in.envto determine the number of sandbox containers to clean. This file is typically used system-wide for environment configuration.Project Directory Structure:
Assumes thatstop.shis located inside a subdirectory of the project root (e.g.,scripts/), as it moves one directory up to setBASE_DIR.
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.