Improve application restart process by robustly locating PM2

Add logic to reliably find and use the PM2 executable for application restarts, ensuring consistency across different environments.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 42d8028a-fa71-4ec2-938c-e43eedf7df01
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/42d8028a-fa71-4ec2-938c-e43eedf7df01/eg9yHD6
This commit is contained in:
marco370 2025-10-16 14:21:26 +00:00
parent 729850fbc6
commit 0fe3a51887
2 changed files with 19 additions and 12 deletions

View File

@ -19,10 +19,6 @@ externalPort = 80
localPort = 33035
externalPort = 3001
[[ports]]
localPort = 36403
externalPort = 3003
[[ports]]
localPort = 41343
externalPort = 3000

View File

@ -69,24 +69,35 @@ echo "🧹 Pulizia devDependencies (mantiene solo production)..."
npm prune --production
# =================== RESTART APPLICATION ===================
# Trova PM2 (potrebbe essere in vari percorsi)
PM2_CMD=$(which pm2 2>/dev/null || echo "/usr/local/bin/pm2")
if [ ! -x "$PM2_CMD" ]; then
PM2_CMD="/usr/bin/pm2"
fi
if [ ! -x "$PM2_CMD" ]; then
PM2_CMD="$(npm root -g)/pm2/bin/pm2"
fi
echo "🔍 Usando PM2: $PM2_CMD"
# Restart applicazione con PM2
echo "🔄 Restart applicazione..."
if pm2 show $APP_NAME > /dev/null 2>&1; then
pm2 reload $APP_NAME --update-env
if $PM2_CMD show $APP_NAME > /dev/null 2>&1; then
$PM2_CMD reload $APP_NAME --update-env
else
pm2 start npm --name $APP_NAME -- start
pm2 save
$PM2_CMD start npm --name $APP_NAME -- start
$PM2_CMD save
fi
# Health check
echo "🏥 Health check..."
sleep 5
if pm2 show $APP_NAME | grep -q "online"; then
if $PM2_CMD show $APP_NAME | grep -q "online"; then
echo "✅ Deployment completato con successo!"
pm2 logs $APP_NAME --lines 20 --nostream
$PM2_CMD logs $APP_NAME --lines 20 --nostream
else
echo "❌ Errore: applicazione non online"
pm2 logs $APP_NAME --lines 50 --nostream
$PM2_CMD logs $APP_NAME --lines 50 --nostream
# Rollback: ripristina ultimo backup
echo "🔄 Tentativo rollback backup..."
@ -103,7 +114,7 @@ fi
# =================== STATUS ===================
echo ""
echo "📊 Status PM2:"
pm2 status
$PM2_CMD status
echo ""
echo "📈 Ultimi backup disponibili:"