Skip to content

Commit 72d473c

Browse files
committed
LCORE-1869: Add graceful teardown and automatic cleanup for llama-stack container
**Key Changes:** - Auto-stop container when exiting `make run` (Ctrl+C) via trap handler - Graceful shutdown with 10s timeout before force-kill - Preserve logs to `/tmp/llama-stack-*.log` on shutdown/removal **Developer Experience:** - No manual cleanup needed - just Ctrl+C - Logs preserved after container removal - Clearer feedback during shutdown Signed-off-by: Anik Bhattacharjee <anbhatta@redhat.com>
1 parent e5baf4a commit 72d473c

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

Makefile

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ LLAMA_STACK_IMAGE ?= lightspeed-llama-stack:local
1717
LLAMA_STACK_PORT ?= 8321
1818
CONTAINER_RUNTIME ?= $(shell command -v podman 2>/dev/null || command -v docker 2>/dev/null)
1919

20-
.PHONY: run run-stack build-llama-stack-image remove-llama-stack-container start-llama-stack-container wait-for-llama-stack-health clean-llama-stack
20+
.PHONY: run run-stack build-llama-stack-image remove-llama-stack-container stop-llama-stack-container start-llama-stack-container wait-for-llama-stack-health clean-llama-stack
2121

2222
run-stack: ## Run lightspeed-stack directly, without building dependent service/s
2323
uv run src/lightspeed_stack.py -c $(CONFIG)
2424

2525
run: start-llama-stack-container ## Run the service locally with dependent services
2626
@echo "Starting Lightspeed Core Stack..."
27+
@trap 'echo ""; echo "Stopping services..."; $(MAKE) stop-llama-stack-container' EXIT INT TERM; \
2728
$(MAKE) run-stack
2829

2930
build-llama-stack-image: remove-llama-stack-container ## Build llama-stack container image
@@ -34,10 +35,26 @@ build-llama-stack-image: remove-llama-stack-container ## Build llama-stack conta
3435
fi
3536
$(CONTAINER_RUNTIME) build -f deploy/llama-stack/test.containerfile -t $(LLAMA_STACK_IMAGE) .
3637

37-
remove-llama-stack-container: ## Remove existing llama-stack container
38+
stop-llama-stack-container: ## Gracefully stop llama-stack container
3839
@if [ -n "$(CONTAINER_RUNTIME)" ] && $(CONTAINER_RUNTIME) inspect $(LLAMA_STACK_CONTAINER_NAME) >/dev/null 2>&1; then \
39-
echo "Removing existing llama-stack container..."; \
40+
echo "Stopping llama-stack container (timeout: 10s)..."; \
41+
if $(CONTAINER_RUNTIME) stop -t 10 $(LLAMA_STACK_CONTAINER_NAME) 2>/dev/null; then \
42+
echo "✓ Container stopped gracefully"; \
43+
else \
44+
echo "⚠ Container did not stop gracefully, capturing logs..."; \
45+
$(CONTAINER_RUNTIME) logs $(LLAMA_STACK_CONTAINER_NAME) > /tmp/llama-stack-failure.log 2>&1 || true; \
46+
echo "Logs saved to /tmp/llama-stack-failure.log"; \
47+
$(CONTAINER_RUNTIME) kill $(LLAMA_STACK_CONTAINER_NAME) 2>/dev/null || true; \
48+
fi; \
49+
fi
50+
51+
remove-llama-stack-container: ## Remove llama-stack container (saves logs first)
52+
@if [ -n "$(CONTAINER_RUNTIME)" ] && $(CONTAINER_RUNTIME) inspect $(LLAMA_STACK_CONTAINER_NAME) >/dev/null 2>&1; then \
53+
echo "Saving container logs before removal..."; \
54+
$(CONTAINER_RUNTIME) logs $(LLAMA_STACK_CONTAINER_NAME) > /tmp/llama-stack-last-run.log 2>&1 || true; \
55+
echo "Removing llama-stack container..."; \
4056
$(CONTAINER_RUNTIME) rm -f $(LLAMA_STACK_CONTAINER_NAME); \
57+
echo "✓ Container removed (logs saved to /tmp/llama-stack-last-run.log)"; \
4158
fi
4259

4360
start-llama-stack-container: build-llama-stack-image ## Start llama-stack container

0 commit comments

Comments
 (0)