ADAPT-Chase's picture
Add files using upload-large-folder tool
503b0e9 verified
#!/bin/bash
# NATS-Pulsar Bridge Startup Script - SignalCore CommsOps
# Configuration
BRIDGE_DIR="/data/adaptai/platform/signalcore/commsops"
LOG_FILE="$BRIDGE_DIR/bridge.log"
CONFIG_FILE="$BRIDGE_DIR/bridge_config.json"
PYTHON_SCRIPT="$BRIDGE_DIR/nats_pulsar_bridge.py"
VENV_PATH="/opt/commsops-venv"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Log function
log() {
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Error function
error() {
echo -e "${RED}$(date '+%Y-%m-%d %H:%M:%S') - ERROR: $1${NC}" | tee -a "$LOG_FILE"
exit 1
}
# Check if running as root
if [ "$EUID" -eq 0 ]; then
error "Please do not run as root"
fi
# Check dependencies
check_dependencies() {
log "Checking dependencies..."
# Check Python
if ! command -v python3 &> /dev/null; then
error "Python 3 not found"
fi
# Check virtual environment
if [ ! -d "$VENV_PATH" ]; then
log "Virtual environment not found, creating..."
python3 -m venv "$VENV_PATH" || error "Failed to create virtual environment"
fi
# Activate virtual environment
source "$VENV_PATH/bin/activate" || error "Failed to activate virtual environment"
# Check Python packages
REQUIRED_PACKAGES=("nats-py" "pulsar-client")
for package in "${REQUIRED_PACKAGES[@]}"; do
if ! python3 -c "import ${package%%==*}" &> /dev/null; then
log "Installing missing package: $package"
pip install "$package" || error "Failed to install $package"
fi
done
log "All dependencies satisfied"
}
# Check if bridge is already running
is_running() {
if pgrep -f "nats_pulsar_bridge.py" > /dev/null; then
return 0
else
return 1
fi
}
# Start bridge
start_bridge() {
if is_running; then
log "Bridge is already running"
return 0
fi
log "Starting NATS-Pulsar bridge..."
# Activate virtual environment
source "$VENV_PATH/bin/activate"
# Start bridge in background
nohup python3 "$PYTHON_SCRIPT" >> "$LOG_FILE" 2>&1 &
# Wait for startup
sleep 2
if is_running; then
BRIDGE_PID=$(pgrep -f "nats_pulsar_bridge.py")
log "${GREEN}Bridge started successfully (PID: $BRIDGE_PID)${NC}"
echo "$BRIDGE_PID" > "$BRIDGE_DIR/bridge.pid"
return 0
else
error "Failed to start bridge"
fi
}
# Stop bridge
stop_bridge() {
if ! is_running; then
log "Bridge is not running"
return 0
fi
log "Stopping NATS-Pulsar bridge..."
# Get PID
if [ -f "$BRIDGE_DIR/bridge.pid" ]; then
BRIDGE_PID=$(cat "$BRIDGE_DIR/bridge.pid")
kill "$BRIDGE_PID" 2>/dev/null
# Wait for process to stop
for i in {1..10}; do
if ! kill -0 "$BRIDGE_PID" 2>/dev/null; then
break
fi
sleep 1
done
# Force kill if still running
if kill -0 "$BRIDGE_PID" 2>/dev/null; then
kill -9 "$BRIDGE_PID" 2>/dev/null
log "Bridge force stopped"
else
log "Bridge stopped gracefully"
fi
rm -f "$BRIDGE_DIR/bridge.pid"
else
# Fallback: kill by process name
pkill -f "nats_pulsar_bridge.py" 2>/dev/null
sleep 2
pkill -9 -f "nats_pulsar_bridge.py" 2>/dev/null
log "Bridge stopped (fallback method)"
fi
log "${GREEN}Bridge stopped successfully${NC}"
}
# Restart bridge
restart_bridge() {
stop_bridge
sleep 2
start_bridge
}
# Status check
status_bridge() {
if is_running; then
BRIDGE_PID=$(pgrep -f "nats_pulsar_bridge.py")
log "${GREEN}Bridge is running (PID: $BRIDGE_PID)${NC}"
# Show recent logs
log "Recent log entries:"
tail -10 "$LOG_FILE" | while read line; do
echo " $line"
done
return 0
else
log "${YELLOW}Bridge is not running${NC}"
return 1
fi
}
# Monitor bridge
monitor_bridge() {
log "Starting bridge monitor..."
while true; do
if ! is_running; then
log "${RED}Bridge is down, attempting restart...${NC}"
start_bridge
fi
# Check every 30 seconds
sleep 30
done
}
# Main script logic
case "$1" in
start)
check_dependencies
start_bridge
;;
stop)
stop_bridge
;;
restart)
check_dependencies
restart_bridge
;;
status)
status_bridge
;;
monitor)
check_dependencies
monitor_bridge
;;
*)
echo "Usage: $0 {start|stop|restart|status|monitor}"
echo ""
echo "Options:"
echo " start - Start the NATS-Pulsar bridge"
echo " stop - Stop the bridge"
echo " restart - Restart the bridge"
echo " status - Check bridge status"
echo " monitor - Continuous monitoring and auto-restart"
exit 1
;;
esac