Skip to content

Commit f279149

Browse files
committed
Improve scaling logic in manage-clients-dynamic.sh to handle instance count more robustly
1 parent 2795833 commit f279149

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

manage-clients-dynamic.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ function scale_instances() {
186186

187187
# Get currently running instances
188188
local running_containers=$(docker ps -q -f name="${PROJECT_NAME}-client-")
189-
local current_count=$(echo "$running_containers" | grep -c . || echo 0)
189+
local current_count=0
190+
if [ -n "$running_containers" ]; then
191+
current_count=$(echo "$running_containers" | wc -l)
192+
fi
190193

191194
echo "Current instances: $current_count, Target: $target_instances"
192195

@@ -243,12 +246,15 @@ function scale_instances() {
243246
# Need to stop some instances
244247
echo "Stopping excess instances (keeping first $target_instances)..."
245248

246-
for i in $(seq $((target_instances + 1)) $current_count); do
247-
local container_name="${PROJECT_NAME}-client-$i"
248-
echo " Stopping Instance $i"
249-
docker stop "$container_name" 2>/dev/null || true
250-
docker rm "$container_name" 2>/dev/null || true
251-
done
249+
# Only run seq if we have valid numbers and current_count > target_instances
250+
if [ "$current_count" -gt "$target_instances" ]; then
251+
for i in $(seq $((target_instances + 1)) $current_count); do
252+
local container_name="${PROJECT_NAME}-client-$i"
253+
echo " Stopping Instance $i"
254+
docker stop "$container_name" 2>/dev/null || true
255+
docker rm "$container_name" 2>/dev/null || true
256+
done
257+
fi
252258
fi
253259

254260
echo "Scaling complete!"

0 commit comments

Comments
 (0)