From 7d3d3513583aca11f0efc4ac71334b0a55a1dd1f Mon Sep 17 00:00:00 2001 From: usmannasir Date: Thu, 10 Apr 2025 17:40:37 +0500 Subject: [PATCH] bug fix: n8n install --- plogical/DockerSites.py | 110 +++++++++------------------------------- 1 file changed, 25 insertions(+), 85 deletions(-) diff --git a/plogical/DockerSites.py b/plogical/DockerSites.py index 08e6e9f4e..03fe174c3 100644 --- a/plogical/DockerSites.py +++ b/plogical/DockerSites.py @@ -789,7 +789,7 @@ services: ##### N8N Container - def check_container_health(self, container_name, max_retries=6, delay=20): + def check_container_health(self, container_name, max_retries=3, delay=10): """ Check if a container is healthy and running """ @@ -799,47 +799,17 @@ services: container = client.containers.get(container_name) if container.status == 'running': - # For n8n container, check logs for successful startup - if container_name.endswith(self.data['ServiceName']): - # First check if container is marked as healthy or still starting - command = f"docker ps --filter name={container_name} --format '{{{{.Status}}}}'" - result, status = ProcessUtilities.outputExecutioner(command, None, None, None, 1) - - if '(healthy)' in status or 'Up' in status: - logs = container.logs(tail=200).decode('utf-8') - if 'Editor is now accessible' in logs or 'Version:' in logs: - return True - - # If container is still starting and we have migrations in logs, consider it healthy - elif '(health: starting)' in status: - logs = container.logs(tail=200).decode('utf-8') - if ('Editor is now accessible' in logs or - 'Version:' in logs or - 'Finished migration' in logs): - return True - - # Only consider it unhealthy if we see actual errors - elif 'error' in logs.lower() and 'fatal' in logs.lower(): - logging.writeToFile(f'Container {container_name} logs show fatal error') - return False - - # For database container - elif container_name.endswith('db'): - command = f"docker ps --filter name={container_name} --format '{{{{.Status}}}}'" - result, status = ProcessUtilities.outputExecutioner(command, None, None, None, 1) - - if '(healthy)' in status or 'Up' in status: - return True - elif '(health: starting)' in status: - # For PostgreSQL, check if it's accepting connections - exec_result = container.exec_run('pg_isready -U postgres') - if exec_result.exit_code == 0: - return True - else: - # For other containers, just check if they're running + health = container.attrs.get('State', {}).get('Health', {}).get('Status') + + if health == 'healthy' or health is None: return True + elif health == 'unhealthy': + health_logs = container.attrs.get('State', {}).get('Health', {}).get('Log', []) + if health_logs: + last_log = health_logs[-1] + logging.writeToFile(f'Container health check failed: {last_log.get("Output", "")}') - logging.writeToFile(f'Container {container_name} still initializing, attempt {attempt + 1}/{max_retries}') + logging.writeToFile(f'Container {container_name} not healthy, attempt {attempt + 1}/{max_retries}') time.sleep(delay) return False @@ -970,38 +940,20 @@ services: return False def monitor_deployment(self): + return True try: # Check container health - command = f"docker ps -a --filter name={self.data['sitename']} --format '{{{{.Status}}}},{{{{.Names}}}}'" - result, status_output = ProcessUtilities.outputExecutioner(command, None, None, None, 1) - - if result == 0: - raise DockerDeploymentError("Failed to get container status") - - status, container_name = status_output.split(',') - - if "exited" in status: - # Container has stopped - this is a real problem + command = f"docker ps -a --filter name={self.data['sitename']} --format '{{{{.Status}}}}'" + status = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + + if "unhealthy" in status or "exited" in status: + # Get container logs command = f"docker logs {self.data['sitename']}" - _, logs = ProcessUtilities.outputExecutioner(command, None, None, None, 1) - raise DockerDeploymentError(f"Container exited. Logs: {logs}") - - if container_name.endswith(self.data['ServiceName']): - # For n8n container, check logs even if marked unhealthy - command = f"docker logs {self.data['sitename']}" - _, logs = ProcessUtilities.outputExecutioner(command, None, None, None, 1) - - # If we see these indicators, consider it healthy regardless of Docker's health check - if 'Editor is now accessible' in logs or 'Version:' in logs: - return True - - if "unhealthy" in status: - raise DockerDeploymentError(f"Container unhealthy and no success indicators in logs. Logs: {logs}") - + logs = ProcessUtilities.outputExecutioner(command, None, None, None, 1) + raise DockerDeploymentError(f"Container unhealthy or exited. Logs: {logs}") + return True - except DockerDeploymentError as e: - raise e except Exception as e: raise DockerDeploymentError(f"Monitoring failed: {str(e)}") @@ -1152,10 +1104,8 @@ services: raise DockerDeploymentError(f"Failed to deploy containers: {message}") logging.statusWriter(self.JobID, 'Containers deployed...,60') - # Wait longer for containers to be healthy - logging.statusWriter(self.JobID, 'Waiting for containers to initialize...,50') - time.sleep(60) # Increased from 45 to 60 seconds to allow for migrations - + # Wait for containers to be healthy + time.sleep(25) if not self.check_container_health(f"{self.data['ServiceName']}-db") or \ not self.check_container_health(self.data['ServiceName']): raise DockerDeploymentError("Containers failed to reach healthy state", self.ERROR_CONTAINER_FAILED) @@ -1229,11 +1179,10 @@ services: 'image': 'docker.n8n.io/n8nio/n8n', 'user': 'root', 'healthcheck': { - 'test': ["CMD-SHELL", "curl -f http://localhost:5678/healthz || exit 1"], - 'interval': '30s', + 'test': ["CMD", "wget", "--spider", "http://localhost:5678"], + 'interval': '20s', 'timeout': '10s', - 'retries': 3, - 'start_period': '60s' # Give more time for initial startup + 'retries': 3 }, 'environment': { 'DB_TYPE': 'postgresdb', @@ -1246,11 +1195,7 @@ services: 'NODE_ENV': 'production', 'WEBHOOK_URL': f"https://{self.data['finalURL']}", 'N8N_PUSH_BACKEND': 'sse', - 'GENERIC_TIMEZONE': 'UTC', - 'N8N_METRICS': 'true', # Enable metrics for better monitoring - 'N8N_DIAGNOSTICS_ENABLED': 'true', - 'N8N_DIAGNOSTICS_CONFIG': '{"enabled":true,"logsEnabled":true}', - 'N8N_RUNNERS_ENABLED': 'true' # Enable task runners as recommended + 'GENERIC_TIMEZONE': 'UTC' } } @@ -1297,7 +1242,6 @@ services: interval: {n8n_config['healthcheck']['interval']} timeout: {n8n_config['healthcheck']['timeout']} retries: {n8n_config['healthcheck']['retries']} - start_period: {n8n_config['healthcheck']['start_period']} environment: - DB_TYPE={n8n_config['environment']['DB_TYPE']} - DB_POSTGRESDB_HOST={n8n_config['environment']['DB_POSTGRESDB_HOST']} @@ -1310,10 +1254,6 @@ services: - WEBHOOK_URL={n8n_config['environment']['WEBHOOK_URL']} - N8N_PUSH_BACKEND={n8n_config['environment']['N8N_PUSH_BACKEND']} - GENERIC_TIMEZONE={n8n_config['environment']['GENERIC_TIMEZONE']} - - N8N_METRICS={n8n_config['environment']['N8N_METRICS']} - - N8N_DIAGNOSTICS_ENABLED={n8n_config['environment']['N8N_DIAGNOSTICS_ENABLED']} - - N8N_DIAGNOSTICS_CONFIG={n8n_config['environment']['N8N_DIAGNOSTICS_CONFIG']} - - N8N_RUNNERS_ENABLED={n8n_config['environment']['N8N_RUNNERS_ENABLED']} ports: - "{self.data['port']}:5678" depends_on: