#!/bin/bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)" SESSION_NAME="${SESSION_NAME:-social-dev}" COMPOSE_FILE="$ROOT_DIR/infra/docker/docker-compose.yml" ENV_FILE="$ROOT_DIR/.env" usage() { echo "Usage: $0 {start|stop}" echo "" echo "Commands:" echo " start Start web + worker processes in tmux" echo " stop Stop and clean up tmux session" exit 1 } start() { echo "=== App Up ===" echo "This script starts web + worker processes in tmux." echo "NOTE: Bootstrap (migrate + init-data) must be run separately." echo "" if ! command -v tmux >/dev/null 2>&1; then echo "Error: tmux is required." >&2 exit 1 fi if [ ! -f "$ENV_FILE" ]; then echo "Error: env file not found at $ENV_FILE" >&2 exit 1 fi if [ ! -f "$COMPOSE_FILE" ]; then echo "Error: compose file not found at $COMPOSE_FILE" >&2 exit 1 fi set -a # shellcheck disable=SC1090 . "$ENV_FILE" set +a UVICORN_LOG_LEVEL="${SOCIAL_RUNTIME__LOG_LEVEL:-info}" UVICORN_LOG_LEVEL="${UVICORN_LOG_LEVEL,,}" WEB_PORT="${SOCIAL_WEB__PORT:-5775}" if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then echo "Error: tmux session '$SESSION_NAME' already exists." >&2 echo "Hint: tmux kill-session -t $SESSION_NAME" >&2 exit 1 fi if command -v ss >/dev/null 2>&1; then if ss -ltn | awk '{print $4}' | grep -qE "[:.]${WEB_PORT}$"; then echo "Error: web port ${WEB_PORT} is already in use." >&2 echo "Hint: run '$0 stop' or change SOCIAL_WEB__PORT in .env" >&2 exit 1 fi fi echo "Starting web + worker processes in tmux session '$SESSION_NAME'..." WEB_CMD="cd '$ROOT_DIR' && PYTHONPATH=backend/src SOCIAL_RUNTIME__SERVICE_NAME=web uv run uvicorn app:app --host \ ${SOCIAL_WEB__HOST:-0.0.0.0} --port ${WEB_PORT} --workers \ ${SOCIAL_WEB__WORKERS:-2} --log-level ${UVICORN_LOG_LEVEL}" WORKER_CRITICAL_CMD="cd '$ROOT_DIR' && PYTHONPATH=backend/src SOCIAL_RUNTIME__SERVICE_NAME=worker-critical uv run taskiq worker core.taskiq.app:critical_broker core.agent.infrastructure.queue.tasks --workers ${SOCIAL_WORKER__GROUPS__CRITICAL__CONCURRENCY:-2}" WORKER_DEFAULT_CMD="cd '$ROOT_DIR' && PYTHONPATH=backend/src SOCIAL_RUNTIME__SERVICE_NAME=worker-default uv run taskiq worker core.taskiq.app:default_broker core.agent.infrastructure.queue.tasks --workers ${SOCIAL_WORKER__GROUPS__DEFAULT__CONCURRENCY:-2}" WORKER_BULK_CMD="cd '$ROOT_DIR' && PYTHONPATH=backend/src SOCIAL_RUNTIME__SERVICE_NAME=worker-bulk uv run taskiq worker core.taskiq.app:bulk_broker core.agent.infrastructure.queue.tasks --workers ${SOCIAL_WORKER__GROUPS__BULK__CONCURRENCY:-1}" tmux new-session -d -s "$SESSION_NAME" -n web "bash -lc \"$WEB_CMD; echo '[web] exited'; exec bash\"" tmux new-window -t "$SESSION_NAME" -n worker-critical "bash -lc \"$WORKER_CRITICAL_CMD; echo '[worker-critical] exited'; exec bash\"" tmux new-window -t "$SESSION_NAME" -n worker-default "bash -lc \"$WORKER_DEFAULT_CMD; echo '[worker-default] exited'; exec bash\"" tmux new-window -t "$SESSION_NAME" -n worker-bulk "bash -lc \"$WORKER_BULK_CMD; echo '[worker-bulk] exited'; exec bash\"" echo "" echo "=== App Started ===" echo "Log files will be created in logs/ directory:" echo " - web.log, web.error.log" echo " - worker-critical.log, worker-critical.error.log" echo " - worker-default.log, worker-default.error.log" echo " - worker-bulk.log, worker-bulk.error.log" echo "" echo "tmux attach -t $SESSION_NAME" echo "tmux list-windows -t $SESSION_NAME" } stop() { echo "=== App Down ===" if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then echo "Stopping tmux session '$SESSION_NAME'..." tmux kill-session -t "$SESSION_NAME" else echo "No tmux session '$SESSION_NAME' found." fi echo "Checking for orphaned processes..." if pgrep -f "uvicorn.*app:app" > /dev/null 2>&1; then echo "Killing orphaned uvicorn processes..." pkill -f "uvicorn.*app:app" fi if pgrep -f "gunicorn.*app:app" > /dev/null 2>&1; then echo "Killing orphaned gunicorn processes..." pkill -f "gunicorn.*app:app" fi if pgrep -f "taskiq.*worker" > /dev/null 2>&1; then echo "Killing orphaned taskiq processes..." pkill -f "taskiq.*worker" fi echo "Session stopped and cleaned up." } case "${1:-}" in start) start ;; stop) stop ;; *) usage ;; esac