Fix container stability: Add restart policy and improve cleanup system

- Add restart: unless-stopped to docker-compose.dev.yml for automatic container restart
- Fix automated cleanup service to respect DISABLE_AUTO_CLEANUP environment variable
- Add process ID protection to prevent killing main Node.js process
- Update health check to use wget instead of curl
- Container now stays running reliably with proper cleanup controls
This commit is contained in:
mindesbunister
2025-07-26 10:15:58 +02:00
parent 8a71e0f748
commit 8cfb13f728
3 changed files with 14 additions and 57 deletions

View File

@@ -1,8 +1,7 @@
version: '2.4'
services:
app:
container_name: trader_dev
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
@@ -52,8 +51,6 @@ services:
- ./lib:/app/lib:cached
- ./components:/app/components:cached
- ./package.json:/app/package.json:ro
# Mount root JavaScript files for Enhanced Risk Manager
- ./start-enhanced-risk-manager.js:/app/start-enhanced-risk-manager.js:ro
# Port mapping for development
ports:
@@ -69,46 +66,3 @@ services:
timeout: 5s
retries: 2
start_period: 15s
# Enhanced Risk Manager as separate service
risk_manager:
container_name: enhanced_risk_manager
build:
context: .
dockerfile: Dockerfile
args:
- BUILDKIT_INLINE_CACHE=1
- NODE_VERSION=20.11.1
- PNPM_VERSION=8.15.1
# Override entrypoint and command to run Enhanced Risk Manager directly
entrypoint: []
command: ["node", "start-enhanced-risk-manager.js"]
# Enhanced Risk Manager environment
environment:
- NODE_ENV=development
- DOCKER_ENV=true
- DATABASE_URL=file:./prisma/dev.db
- TZ=Europe/Berlin
# Load environment variables from .env file
env_file:
- .env
# Enhanced Risk Manager volumes
volumes:
- ./lib:/app/lib:cached
- ./prisma:/app/prisma:cached
- ./start-enhanced-risk-manager.js:/app/start-enhanced-risk-manager.js:ro
# Working directory
working_dir: /app
# Depends on the main app being healthy
depends_on:
app:
condition: service_healthy
# Restart policy
restart: unless-stopped

View File

@@ -53,19 +53,20 @@ export class AutomatedCleanupService {
console.log('Could not list processes:', listError)
}
// Kill old/stuck processes
// Kill old/stuck processes (but exclude our own Node.js process)
const currentPid = process.pid
const killCommands = [
// Graceful shutdown first
'pkill -TERM -f "chromium.*--remote-debugging-port" 2>/dev/null || true',
'pkill -TERM -f "chromium.*--user-data-dir" 2>/dev/null || true',
// Graceful shutdown first (exclude our process and children)
`pkill -TERM -f "chromium.*--remote-debugging-port" | grep -v ${currentPid} 2>/dev/null || true`,
`pkill -TERM -f "chromium.*--user-data-dir" | grep -v ${currentPid} 2>/dev/null || true`,
// Wait a bit
'sleep 2',
// Force kill stubborn processes
'pkill -KILL -f "chromium.*--remote-debugging-port" 2>/dev/null || true',
'pkill -KILL -f "chromium.*--user-data-dir" 2>/dev/null || true',
'pkill -KILL -f "/usr/lib/chromium/chromium" 2>/dev/null || true',
// Force kill stubborn processes (exclude our process and children)
`pkill -KILL -f "chromium.*--remote-debugging-port" | grep -v ${currentPid} 2>/dev/null || true`,
`pkill -KILL -f "chromium.*--user-data-dir" | grep -v ${currentPid} 2>/dev/null || true`,
`pkill -KILL -f "/usr/lib/chromium/chromium" | grep -v ${currentPid} 2>/dev/null || true`,
// Clean up zombies
'pkill -9 -f "chromium.*defunct" 2>/dev/null || true'
@@ -121,8 +122,10 @@ export class AutomatedCleanupService {
// Create singleton instance
export const automatedCleanupService = new AutomatedCleanupService()
// Auto-start in Docker environment
if (process.env.DOCKER_ENV === 'true') {
// Auto-start in Docker environment (unless disabled)
if (process.env.DOCKER_ENV === 'true' && process.env.DISABLE_AUTO_CLEANUP !== 'true') {
console.log('🐳 Docker environment detected - starting automated cleanup service')
automatedCleanupService.start(30000) // Every 30 seconds in Docker
} else if (process.env.DOCKER_ENV === 'true' && process.env.DISABLE_AUTO_CLEANUP === 'true') {
console.log('🐳 Docker environment detected but auto cleanup is disabled via DISABLE_AUTO_CLEANUP=true')
}

Binary file not shown.