"""Check configured infrastructure without changing production data.""" import json,uuid from django.core.management.base import BaseCommand,CommandError from django.db import connection from django.db.migrations.executor import MigrationExecutor from django.core.cache import cache from config.celery import app class Command(BaseCommand): help='Prüft Datenbank, Migrationen, Redis, Broker und optional Worker.' def add_arguments(self,p):p.add_argument('--workers',action='store_true') def handle(self,*args,**options): results={} try: with connection.cursor() as c:c.execute('SELECT 1');c.fetchone() results['database']=connection.vendor executor=MigrationExecutor(connection) pending=executor.migration_plan(executor.loader.graph.leaf_nodes()) results['migrations']='pending' if pending else 'ok' except Exception as e:results['database']='ERROR '+type(e).__name__ try: key='doctor:'+str(uuid.uuid4());cache.set(key,'ok',30) results['cache']='ok' if cache.get(key)=='ok' else 'ERROR readback' cache.delete(key) except Exception as e:results['cache']='ERROR '+type(e).__name__ try: with app.connection_for_read() as conn:conn.ensure_connection(max_retries=0,timeout=5) results['broker']='ok' except Exception as e:results['broker']='ERROR '+type(e).__name__ if options['workers']: try:results['workers']='ok' if app.control.ping(timeout=3) else 'ERROR no response' except Exception as e:results['workers']='ERROR '+type(e).__name__ self.stdout.write(json.dumps(results,indent=2)) if any(v.startswith('ERROR') or v=='pending' for v in results.values()): raise CommandError('Infrastrukturprüfung fehlgeschlagen; keine Zugangsdaten werden ausgegeben.')