Skip to content

Commit afa1ef4

Browse files
committed
fix: improved docker detection logic and PID handling
1 parent 4a09444 commit afa1ef4

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

conduit-optimizer.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
# ==============================================================================
3+
# Project: Conduit-console | Module: Performance Optimizer
4+
# Author: Babak Sorkhpour (Assisted by Gemini Pro)
5+
# License: MIT
6+
# Version: 1.4.0
7+
# Description: Improved Docker detection (Image & Name) + Execution Fixes.
8+
# ==============================================================================
9+
10+
# --- Configuration ---
11+
PRIORITY_NATIVE_NICE=-5
12+
PRIORITY_DOCKER_NICE=-10 # Higher priority for Docker
13+
TARGET_IONICE_CLASS=2
14+
TARGET_IONICE_LEVEL=0
15+
16+
# Colors
17+
GREEN='\033[0;32m'
18+
BLUE='\033[0;34m'
19+
YELLOW='\033[1;33m'
20+
RED='\033[0;31m'
21+
NC='\033[0m'
22+
23+
# --- Root Check ---
24+
if [[ $EUID -ne 0 ]]; then
25+
echo -e "${RED}[ERROR] Root privileges required.${NC}"
26+
exit 1
27+
fi
28+
29+
echo -e "${GREEN}Starting Conduit Optimizer v1.4.0${NC}"
30+
echo -e "Author: Babak Sorkhpour"
31+
echo "---------------------------------------------------"
32+
33+
# Track PIDs to prevent double optimization (Native vs Docker overlap)
34+
declare -A PROCESSED_PIDS
35+
36+
# ==============================================================================
37+
# SECTION 1: DOCKER OPTIMIZATION (Priority 1)
38+
# We run this FIRST to ensure Docker processes get the higher priority (-10)
39+
# even if they are also detected by the native scanner later.
40+
# ==============================================================================
41+
echo -e "${BLUE}>> Phase 1: Scanning Docker Containers (Image & Name)...${NC}"
42+
43+
if command -v docker &>/dev/null; then
44+
# Search for containers where Image OR Name contains "conduit"
45+
# format: ID | Image | Names
46+
DOCKER_CANDIDATES=$(docker ps --format "{{.ID}} {{.Image}} {{.Names}}" | grep -i "conduit")
47+
48+
if [[ -n "$DOCKER_CANDIDATES" ]]; then
49+
# Read line by line
50+
while read -r LINE; do
51+
CID=$(echo "$LINE" | awk '{print $1}')
52+
C_INFO=$(echo "$LINE" | awk '{$1=""; print $0}') # Rest of the string
53+
54+
# Get Host PID
55+
CPID=$(docker inspect --format '{{.State.Pid}}' "$CID" 2>/dev/null)
56+
57+
if [[ -n "$CPID" && "$CPID" -gt 0 ]]; then
58+
renice -n "$PRIORITY_DOCKER_NICE" -p "$CPID" &>/dev/null
59+
ionice -c "$TARGET_IONICE_CLASS" -n "$TARGET_IONICE_LEVEL" -p "$CPID" &>/dev/null
60+
61+
PROCESSED_PIDS[$CPID]=1
62+
echo -e " [Docker] Container $CID ($C_INFO)"
63+
echo -e " PID: $CPID -> Nice $PRIORITY_DOCKER_NICE: ${GREEN}OK${NC}"
64+
fi
65+
done <<< "$DOCKER_CANDIDATES"
66+
else
67+
echo -e " ${YELLOW}No active containers matching 'conduit' found.${NC}"
68+
echo -e " (Debug: Ensure 'docker ps' shows running containers)"
69+
fi
70+
else
71+
echo -e " Docker not installed or not in PATH."
72+
fi
73+
74+
# ==============================================================================
75+
# SECTION 2: NATIVE OPTIMIZATION (Priority 2)
76+
# ==============================================================================
77+
echo -e "${BLUE}>> Phase 2: Scanning Native Processes...${NC}"
78+
79+
NATIVE_PIDS=$(pgrep -f "/opt/conduit.*/conduit")
80+
81+
if [[ -n "$NATIVE_PIDS" ]]; then
82+
for PID in $NATIVE_PIDS; do
83+
# Skip if already optimized as Docker
84+
if [[ ${PROCESSED_PIDS[$PID]} ]]; then
85+
continue
86+
fi
87+
88+
renice -n "$PRIORITY_NATIVE_NICE" -p "$PID" &>/dev/null
89+
ionice -c "$TARGET_IONICE_CLASS" -n "$TARGET_IONICE_LEVEL" -p "$PID" &>/dev/null
90+
91+
echo -e " [Native] PID: $PID -> Nice $PRIORITY_NATIVE_NICE: ${GREEN}OK${NC}"
92+
PROCESSED_PIDS[$PID]=1
93+
done
94+
else
95+
echo -e " No native processes found."
96+
fi
97+
98+
# ==============================================================================
99+
# SECTION 3: VERIFICATION
100+
# ==============================================================================
101+
echo "---------------------------------------------------"
102+
echo -e "${YELLOW}>> Phase 3: Final Verification${NC}"
103+
COUNT=${#PROCESSED_PIDS[@]}
104+
if [[ $COUNT -gt 0 ]]; then
105+
echo -e "${GREEN}>> SUCCESS: Optimized $COUNT processes.${NC}"
106+
else
107+
echo -e "${RED}>> WARNING: No processes were optimized.${NC}"
108+
fi

0 commit comments

Comments
 (0)