diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..2e38040 --- /dev/null +++ b/start.sh @@ -0,0 +1,100 @@ +# Function to cleanup on exit +cleanup() { + log "Shutting down..." + stop_server + exit 0 +} + +# Trap Ctrl+C (SIGINT) and other termination signals +trap cleanup SIGINT SIGTERM + +#!/bin/bash + +# Log function for better visibility +log() { + echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" +} + +# Function to check if node server is running +is_server_running() { + if pgrep -f "npm run start" > /dev/null; then + return 0 # Server is running + else + return 1 # Server is not running + fi +} + +# Function to stop the server +stop_server() { + if is_server_running; then + log "Stopping Node server..." + pkill -f "npm run start" + sleep 2 # Wait for the server to stop + + if is_server_running; then + log "ERROR: Failed to stop Node server" + exit 1 + else + log "Node server stopped successfully" + fi + else + log "Node server is not running" + fi +} + +# Function to start the server +start_server() { + if ! is_server_running; then + log "Starting Node server..." + npm run start & + sleep 2 # Wait for the server to start + + if is_server_running; then + log "Node server started successfully" + else + log "ERROR: Failed to start Node server" + exit 1 + fi + else + log "Node server is already running" + fi +} + +# Function to restart the server +restart_server() { + log "Restarting Node server..." + stop_server + start_server +} + +# Pull updates from main branch +log "Pulling updates from main branch..." +if ! git pull origin main; then + log "ERROR: Failed to pull updates from Git" + exit 1 +fi +log "Git pull completed successfully" + +# Stop the server +stop_server + +# Run database migrations +log "Running database migrations..." +if ! npx mikro-orm-esm migration:up; then + log "ERROR: Failed to run database migrations" + exit 1 +fi +log "Database migrations completed successfully" + +# Start the server +start_server + +# Monitor server and restart if it stops +log "Starting server monitoring..." +while true; do + if ! is_server_running; then + log "Server stopped unexpectedly. Restarting..." + start_server + fi + sleep 10 # Check every 10 seconds +done \ No newline at end of file