Refactor PM2 instance management in deployment script and enhance configuration for multiple instances
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 43s

This commit introduces a new function to streamline the process of starting and restarting PM2 instances, improving error handling and logging. It also updates the harheimertc.config.cjs file to utilize a helper function for environment variable management, allowing for better organization and support for multiple instances. The deployment script now checks the status of both PM2 instances, providing clearer feedback on their operational state, which enhances overall deployment reliability.
This commit is contained in:
Torsten Schulz (local)
2026-01-17 18:46:15 +01:00
parent ee6b031c7b
commit 5cf12d1838
2 changed files with 127 additions and 68 deletions

View File

@@ -357,34 +357,67 @@ if ! command -v pm2 &> /dev/null; then
exit 1 exit 1
fi fi
# Prüfe, ob der Prozess existiert # Funktion zum Starten/Neustarten einer PM2-Instanz
if ! pm2 describe harheimertc &> /dev/null; then restart_pm2_instance() {
echo "WARNING: PM2-Prozess 'harheimertc' existiert nicht." local instance_name=$1
echo "Versuche, den Prozess zu starten..." if ! pm2 describe "$instance_name" &> /dev/null; then
pm2 start harheimertc.config.cjs --update-env || pm2 start harheimertc.simple.cjs --update-env || { echo " WARNING: PM2-Prozess '$instance_name' existiert nicht."
echo "ERROR: Konnte PM2-Prozess nicht starten." echo " Versuche, den Prozess zu starten..."
echo "Bitte manuell starten: pm2 start harheimertc.config.cjs" if pm2 start harheimertc.config.cjs --only "$instance_name" --update-env; then
exit 1 echo " ✓ PM2-Prozess '$instance_name' gestartet"
} return 0
echo " ✓ PM2-Prozess gestartet"
else
# Restart mit --update-env, um Umgebungsvariablen zu aktualisieren
echo " Restarting harheimertc with --update-env..."
if pm2 restart harheimertc --update-env; then
echo " ✓ PM2-Prozess neu gestartet"
else else
echo "ERROR: PM2-Restart fehlgeschlagen!" echo " ERROR: Konnte PM2-Prozess '$instance_name' nicht starten."
echo "Bitte manuell prüfen: pm2 logs harheimertc" return 1
exit 1
fi fi
else
echo " Restarting $instance_name with --update-env..."
if pm2 restart "$instance_name" --update-env; then
echo " ✓ PM2-Prozess '$instance_name' neu gestartet"
return 0
else
echo " ERROR: PM2-Restart für '$instance_name' fehlgeschlagen!"
return 1
fi
fi
}
# Starte/Neustarte beide Instanzen
INSTANCE_ERRORS=0
if ! restart_pm2_instance "harheimertc"; then
INSTANCE_ERRORS=$((INSTANCE_ERRORS + 1))
fi fi
# Prüfe, ob der Prozess läuft if ! restart_pm2_instance "harheimertc-3102"; then
INSTANCE_ERRORS=$((INSTANCE_ERRORS + 1))
fi
# Prüfe, ob beide Prozesse laufen
sleep 2 sleep 2
echo ""
echo " Checking PM2 instances status..."
if pm2 describe harheimertc | grep -q "online"; then if pm2 describe harheimertc | grep -q "online"; then
echo " ✓ PM2-Prozess läuft (online)" echo " ✓ PM2-Prozess 'harheimertc' läuft (online)"
else else
echo "WARNING: PM2-Prozess ist nicht online. Prüfe Logs: pm2 logs harheimertc" echo " WARNING: PM2-Prozess 'harheimertc' ist nicht online. Prüfe Logs: pm2 logs harheimertc"
INSTANCE_ERRORS=$((INSTANCE_ERRORS + 1))
fi
if pm2 describe harheimertc-3102 | grep -q "online"; then
echo " ✓ PM2-Prozess 'harheimertc-3102' läuft (online)"
else
echo " WARNING: PM2-Prozess 'harheimertc-3102' ist nicht online. Prüfe Logs: pm2 logs harheimertc-3102"
INSTANCE_ERRORS=$((INSTANCE_ERRORS + 1))
fi
if [ "$INSTANCE_ERRORS" -gt 0 ]; then
echo ""
echo "WARNING: Einige PM2-Instanzen haben Probleme. Bitte manuell prüfen:"
echo " pm2 status"
echo " pm2 logs harheimertc"
echo " pm2 logs harheimertc-3102"
fi fi
echo "" echo ""
@@ -392,7 +425,10 @@ echo "=== Deployment completed successfully! ==="
echo "The application is now running with the latest code and your production data preserved." echo "The application is now running with the latest code and your production data preserved."
echo "" echo ""
echo "Useful commands:" echo "Useful commands:"
echo " pm2 logs harheimertc # View logs" echo " pm2 logs harheimertc # View logs (Port 3100)"
echo " pm2 logs harheimertc-3102 # View logs (Port 3102)"
echo " pm2 status # View status" echo " pm2 status # View status"
echo " pm2 restart harheimertc # Restart manually" echo " pm2 restart harheimertc # Restart instance on port 3100"
echo " pm2 restart harheimertc-3102 # Restart instance on port 3102"
echo " pm2 restart all # Restart all instances"

View File

@@ -6,20 +6,11 @@ try {
// If dotenv isn't available or .env missing, continue (process.env may be set elsewhere) // If dotenv isn't available or .env missing, continue (process.env may be set elsewhere)
} }
module.exports = { // Helper function to create env object
apps: [{ function createEnv(port) {
name: 'harheimertc', return {
// Nuxt 4 production build: direkt den Node-Server starten (kein "preview mode")
script: 'node',
args: '.output/server/index.mjs',
cwd: '/var/www/harheimertc',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production', NODE_ENV: 'production',
PORT: 3100, PORT: port,
// Secrets/Config (loaded from .env above, if present) // Secrets/Config (loaded from .env above, if present)
ENCRYPTION_KEY: process.env.ENCRYPTION_KEY, ENCRYPTION_KEY: process.env.ENCRYPTION_KEY,
JWT_SECRET: process.env.JWT_SECRET, JWT_SECRET: process.env.JWT_SECRET,
@@ -45,10 +36,42 @@ module.exports = {
WEBAUTHN_RP_ID: process.env.WEBAUTHN_RP_ID, WEBAUTHN_RP_ID: process.env.WEBAUTHN_RP_ID,
WEBAUTHN_RP_NAME: process.env.WEBAUTHN_RP_NAME, WEBAUTHN_RP_NAME: process.env.WEBAUTHN_RP_NAME,
WEBAUTHN_REQUIRE_UV: process.env.WEBAUTHN_REQUIRE_UV WEBAUTHN_REQUIRE_UV: process.env.WEBAUTHN_REQUIRE_UV
}, }
}
module.exports = {
apps: [
{
name: 'harheimertc',
// Nuxt 4 production build: direkt den Node-Server starten (kein "preview mode")
script: 'node',
args: '.output/server/index.mjs',
cwd: '/var/www/harheimertc',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: createEnv(3100),
error_file: '/var/log/pm2/harheimertc-error.log', error_file: '/var/log/pm2/harheimertc-error.log',
out_file: '/var/log/pm2/harheimertc-out.log', out_file: '/var/log/pm2/harheimertc-out.log',
log_file: '/var/log/pm2/harheimertc-combined.log', log_file: '/var/log/pm2/harheimertc-combined.log',
time: true time: true
}] },
{
name: 'harheimertc-3102',
// Zweite Instanz auf Port 3102
script: 'node',
args: '.output/server/index.mjs',
cwd: '/var/www/harheimertc',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: createEnv(3102),
error_file: '/var/log/pm2/harheimertc-3102-error.log',
out_file: '/var/log/pm2/harheimertc-3102-out.log',
log_file: '/var/log/pm2/harheimertc-3102-combined.log',
time: true
}
]
} }