73 lines
1.6 KiB
Bash
73 lines
1.6 KiB
Bash
#!/bin/sh
|
|
set -e
|
|
|
|
# Configuration
|
|
MAX_MYSQL_WAIT=60
|
|
APP_NAME="nodeapp"
|
|
|
|
# Cleanup function
|
|
cleanup() {
|
|
echo "Cleaning up..."
|
|
tmux kill-session -t $APP_NAME 2>/dev/null || true
|
|
redis-cli shutdown || true
|
|
mysqladmin -u root shutdown || true
|
|
exit 0
|
|
}
|
|
|
|
# Setup cleanup trap
|
|
trap cleanup SIGTERM SIGINT
|
|
|
|
# Start Redis
|
|
echo "Starting Redis..."
|
|
redis-server --daemonize yes
|
|
if ! redis-cli ping > /dev/null 2>&1; then
|
|
echo "Failed to start Redis"
|
|
exit 1
|
|
fi
|
|
|
|
# Start MySQL
|
|
echo "Starting MySQL..."
|
|
mysqld --user=mysql --datadir=/var/lib/mysql &
|
|
|
|
# Wait for MySQL with timeout
|
|
echo "Waiting for MySQL to be ready..."
|
|
COUNTER=0
|
|
while ! mysqladmin ping -h localhost --silent; do
|
|
if [ $COUNTER -gt $MAX_MYSQL_WAIT ]; then
|
|
echo "MySQL failed to start within $MAX_MYSQL_WAIT seconds"
|
|
exit 1
|
|
fi
|
|
echo "Still waiting..."
|
|
sleep 2
|
|
COUNTER=$((COUNTER+2))
|
|
done
|
|
echo "MySQL is ready!"
|
|
|
|
# Run migrations with error handling
|
|
echo "Running database migrations..."
|
|
if ! npx mikro-orm migration:up; then
|
|
echo "Migration failed"
|
|
exit 1
|
|
fi
|
|
|
|
# Start application in tmux
|
|
echo "Starting application..."
|
|
tmux new-session -d -s $APP_NAME "npm run start"
|
|
echo "App is running in tmux session. Attach with: tmux attach-session -t $APP_NAME"
|
|
|
|
# Monitor key processes
|
|
while true; do
|
|
if ! redis-cli ping > /dev/null 2>&1; then
|
|
echo "Redis died"
|
|
exit 1
|
|
fi
|
|
if ! mysqladmin ping -h localhost --silent; then
|
|
echo "MySQL died"
|
|
exit 1
|
|
fi
|
|
if ! tmux has-session -t $APP_NAME 2>/dev/null; then
|
|
echo "Application died"
|
|
exit 1
|
|
fi
|
|
sleep 30
|
|
done |