diff --git a/plogical/DockerSites.py b/plogical/DockerSites.py index 975fa93db..e76bb8a20 100644 --- a/plogical/DockerSites.py +++ b/plogical/DockerSites.py @@ -948,13 +948,19 @@ services: def monitor_deployment(self): try: + # Format container names + n8n_container_name = f"{self.data['ServiceName']}-{self.data['ServiceName']}-1" + db_container_name = f"{self.data['ServiceName']}-{self.data['ServiceName']}-db-1" + + logging.writeToFile(f'Monitoring containers: {n8n_container_name} and {db_container_name}') + # Check container health command = f"docker ps -a --filter name={self.data['ServiceName']} --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['ServiceName']}" + command = f"docker logs {n8n_container_name}" logs = ProcessUtilities.outputExecutioner(command, None, None, None, 1) raise DockerDeploymentError(f"Container unhealthy or exited. Logs: {logs}") @@ -965,21 +971,29 @@ services: while retry_count < max_retries: # Check if database container is ready - command = f"docker exec {self.data['ServiceName']}-db pg_isready -U postgres" + command = f"docker exec {db_container_name} pg_isready -U postgres" result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) if "accepting connections" in result: db_ready = True break + # Check container status + command = f"docker inspect --format='{{{{.State.Status}}}}' {db_container_name}" + db_status = ProcessUtilities.outputExecutioner(command, None, None, None, 1).strip() + + if db_status not in ['running', 'starting']: + raise DockerDeploymentError(f"Database container is in {db_status} state") + retry_count += 1 time.sleep(2) + logging.writeToFile(f'Waiting for database to be ready, attempt {retry_count}/{max_retries}') if not db_ready: raise DockerDeploymentError("Database failed to become ready within timeout period") # Verify database connection from n8n container - command = f"docker exec {self.data['ServiceName']} node -e \"const pg = require('pg'); const client = new pg.Client({{ user: 'postgres', password: '{self.data['MySQLPassword']}', host: '{self.data['ServiceName']}-db', port: 5432, database: '{self.data['MySQLDBName']}' }}); client.connect().then(() => {{ console.log('Connected successfully'); process.exit(0); }}).catch(err => {{ console.error(err); process.exit(1); }});\"" + command = f"docker exec {n8n_container_name} node -e \"const pg = require('pg'); const client = new pg.Client({{ user: 'postgres', password: '{self.data['MySQLPassword']}', host: '{self.data['ServiceName']}-db', port: 5432, database: '{self.data['MySQLDBName']}' }}); client.connect().then(() => {{ console.log('Connected successfully'); process.exit(0); }}).catch(err => {{ console.error(err); process.exit(1); }});\"" result = ProcessUtilities.outputExecutioner(command, None, None, None, 1) if "Connected successfully" not in result: