Skip to content

Instantly share code, notes, and snippets.

@regiellis
Last active October 24, 2024 16:08
Show Gist options
  • Save regiellis/7406d62ac47b6e9926b3231d34d0a553 to your computer and use it in GitHub Desktop.
Save regiellis/7406d62ac47b6e9926b3231d34d0a553 to your computer and use it in GitHub Desktop.
ComfyUI hot-reload for custom nodes development
#!/bin/bash
# Get the main directory
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
# Set paths relative
WATCH_DIR="$SCRIPT_DIR/ComfyUI/custom_nodes/"
RUN_COMFY_SCRIPT="$SCRIPT_DIR/run_comfy.sh" # custom bootstrap or use comfyui cli
PID_FILE="$SCRIPT_DIR/comfyui.pid"
LOG_FILE="$SCRIPT_DIR/comfyui.log"
DEBOUNCE_SECONDS=5
# Define colors
GREEN='\033[0;32m' # Green for success messages
RED='\033[0;31m' # Red for error messages
YELLOW='\033[0;33m' # Yellow for warnings
NC='\033[0m' # No Color
is_comfyui_running() {
[[ -f "$PID_FILE" ]] && kill -0 $(cat "$PID_FILE") 2>/dev/null
}
start_comfyui() {
echo -e "${GREEN}Starting ComfyUI...${NC}"
"$RUN_COMFY_SCRIPT" background
sleep 2 # Give it a moment to start
}
stop_comfyui() {
if is_comfyui_running; then
echo -e "${YELLOW}Stopping ComfyUI...${NC}"
"$RUN_COMFY_SCRIPT" stop
sleep 2 # Give it a moment to stop
fi
}
restart_comfyui() {
stop_comfyui
start_comfyui
}
watch_for_changes() {
local last_restart=$(date +%s)
while true; do
if find "$WATCH_DIR" -type f \( -name "*.py" -o -name "*.js" -o -name "*.css" \) -newermt "-${DEBOUNCE_SECONDS} seconds" | grep -q .; then
current_time=$(date +%s)
if ((current_time - last_restart >= DEBOUNCE_SECONDS)); then
echo -e "${YELLOW}Changes detected. Restarting ComfyUI...${NC}"
restart_comfyui
last_restart=$current_time
fi
fi
sleep 1
done
}
# Start ComfyUI if it's not already running
if ! is_comfyui_running; then
start_comfyui
fi
echo -e "${GREEN}Watching $WATCH_DIR for changes...${NC}"
watch_for_changes
#!/usr/bin/env zsh
SCRIPT_DIR="${0:A:h}"
COMFY_DIR="$SCRIPT_DIR/ComfyUI"
VENV_PATH="$COMFY_DIR/.venv"
PID_FILE="$SCRIPT_DIR/comfyui.pid"
LOG_FILE="$SCRIPT_DIR/comfyui.log"
MAX_WAIT_TIME=60 # Maximum wait time in seconds
start_background() {
echo "Starting ComfyUI in the background..."
source "$VENV_PATH/bin/activate"
nohup python -s "$COMFY_DIR/main.py" --listen --preview-method auto > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "ComfyUI started in background. PID: $(cat "$PID_FILE")"
wait_for_comfyui_ready
}
start_foreground() {
echo "Starting ComfyUI in the foreground..."
source "$VENV_PATH/bin/activate"
python -s "$COMFY_DIR/main.py" --listen --preview-method auto
}
stop() {
if [[ -f "$PID_FILE" ]]; then
PID=$(cat "$PID_FILE")
echo "Stopping ComfyUI (PID: $PID)..."
kill "$PID"
wait_for_comfyui_stop
rm "$PID_FILE"
echo "ComfyUI stopped."
else
echo "ComfyUI is not running in the background."
fi
}
wait_for_comfyui_ready() {
echo "Waiting for ComfyUI to be fully operational..."
local start_time=$(date +%s)
while true; do
if grep -q "Starting server" "$LOG_FILE"; then
echo "ComfyUI is now fully operational."
return 0
fi
local current_time=$(date +%s)
local elapsed_time=$((current_time - start_time))
if [[ $elapsed_time -ge $MAX_WAIT_TIME ]]; then
echo "Timeout: ComfyUI did not start within $MAX_WAIT_TIME seconds."
return 1
fi
sleep 1
done
}
wait_for_comfyui_stop() {
echo "Waiting for ComfyUI to stop..."
local start_time=$(date +%s)
while kill -0 "$PID" 2>/dev/null; do
local current_time=$(date +%s)
local elapsed_time=$((current_time - start_time))
if [[ $elapsed_time -ge $MAX_WAIT_TIME ]]; then
echo "Timeout: ComfyUI did not stop within $MAX_WAIT_TIME seconds. Forcing stop."
kill -9 "$PID"
break
fi
sleep 1
done
}
case "$1" in
start)
start_foreground
;;
background)
start_background
;;
stop)
stop
;;
restart)
stop
start_background
;;
*)
echo "Usage: $0 {start|background|stop|restart}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment