fix(import): correct counter bug and improve script reliability

- fix credentials counter bug caused by posix sh subshell behavior
- add trap for temp file cleanup on exit
- make import_workflows.sh executable
- add explanatory comments in restart.sh for down/up logic
- extract sleep value to EXTERNAL_SERVICE_INIT_DELAY constant
This commit is contained in:
Yury Kossakovsky
2026-01-02 16:24:40 -07:00
parent 6a67456db4
commit 44d36b8be8
2 changed files with 27 additions and 12 deletions

22
scripts/import_workflows.sh Normal file → Executable file
View File

@@ -10,17 +10,28 @@ fi
set -e
# Temp file for counter (pipes create subshells in POSIX sh)
COUNTER_FILE=$(mktemp)
trap 'rm -f "$COUNTER_FILE"' EXIT
echo "0" > "$COUNTER_FILE"
# Import credentials first
echo 'Importing credentials...'
CRED_FILES=$(find /backup/credentials -maxdepth 1 -type f -not -name '.gitkeep' 2>/dev/null || true)
if [ -n "$CRED_FILES" ]; then
CRED_COUNT=$(echo "$CRED_FILES" | wc -l | tr -d ' ')
CURRENT=0
echo "0" > "$COUNTER_FILE"
echo "$CRED_FILES" | while IFS= read -r file; do
CURRENT=$(cat "$COUNTER_FILE")
CURRENT=$((CURRENT + 1))
echo "$CURRENT" > "$COUNTER_FILE"
filename=$(basename "$file")
echo "[$CURRENT/$CRED_COUNT] Importing credential: $filename"
n8n import:credentials --input="$file" 2>/dev/null || echo " Error importing: $filename"
printf "[%2d/%d] %s" "$CURRENT" "$CRED_COUNT" "$filename"
if n8n import:credentials --input="$file" >/dev/null 2>&1; then
echo " OK"
else
echo " FAILED"
fi
done
fi
@@ -43,8 +54,7 @@ TOTAL=$(echo "$WORKFLOW_FILES" | wc -l | tr -d ' ')
echo "Importing $TOTAL of $TOTAL_FOUND workflows"
echo ''
# Use a counter file since pipes create subshells
COUNTER_FILE=$(mktemp)
# Reset counter for workflows
echo "0" > "$COUNTER_FILE"
echo "$WORKFLOW_FILES" | while IFS= read -r file; do
@@ -62,7 +72,5 @@ echo "$WORKFLOW_FILES" | while IFS= read -r file; do
fi
done
rm -f "$COUNTER_FILE"
echo ''
echo 'Import complete!'

View File

@@ -27,35 +27,42 @@ load_env
PROJECT_NAME="localai"
# Time to wait for external services (Supabase, Dify) to initialize before starting main stack
EXTERNAL_SERVICE_INIT_DELAY=10
# Build compose files array (sets global COMPOSE_FILES)
build_compose_files_array
log_info "Restarting services..."
log_info "Using compose files: ${COMPOSE_FILES[*]}"
# Stop all services
# Stop all services using ALL compose files (including external stacks)
# This ensures clean shutdown of everything before restart
docker compose -p "$PROJECT_NAME" "${COMPOSE_FILES[@]}" down
# Start services in correct order (matching start_services.py behavior)
# Supabase must be started separately due to relative path resolution in its compose file
# NOTE: External stacks (Supabase, Dify) must be started SEPARATELY because their
# compose files use relative paths for volumes/configs. When combined with main
# docker-compose.yml via -f flags, the relative paths resolve incorrectly.
# Solution: Start external stacks first from their own directories, then start main stack.
if is_profile_active "supabase"; then
SUPABASE_COMPOSE="$PROJECT_ROOT/supabase/docker/docker-compose.yml"
if [ -f "$SUPABASE_COMPOSE" ]; then
log_info "Starting Supabase services..."
docker compose -p "$PROJECT_NAME" -f "$SUPABASE_COMPOSE" up -d
log_info "Waiting for Supabase to initialize..."
sleep 10
sleep "$EXTERNAL_SERVICE_INIT_DELAY"
fi
fi
# Start Dify separately (same relative path issue)
if is_profile_active "dify"; then
DIFY_COMPOSE="$PROJECT_ROOT/dify/docker/docker-compose.yaml"
if [ -f "$DIFY_COMPOSE" ]; then
log_info "Starting Dify services..."
docker compose -p "$PROJECT_NAME" -f "$DIFY_COMPOSE" up -d
log_info "Waiting for Dify to initialize..."
sleep 10
sleep "$EXTERNAL_SERVICE_INIT_DELAY"
fi
fi