quantum-ai-v2/agents/management/commands/cleanup_expired_sessions.py
Claude d750857e4e Implement chat session timeout system
- Add expires_at field to ChatSession model with 2-hour default
- Sessions automatically expire after 2 hours of inactivity
- Active conversations extend session by 2 hours per message
- Add session expiration checks in chat views
- Create cleanup management command for expired sessions
- Add session status indicators in chat UI
- Fix null safety for existing sessions without expiration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-01 15:45:16 +05:30

30 lines
911 B
Python

from django.core.management.base import BaseCommand
from django.utils import timezone
from agents.models import ChatSession
class Command(BaseCommand):
help = 'Mark expired chat sessions as expired'
def handle(self, *args, **options):
now = timezone.now()
# Find active sessions that have expired
expired_sessions = ChatSession.objects.filter(
status='active',
expires_at__lt=now
)
count = expired_sessions.count()
if count > 0:
# Mark them as expired
expired_sessions.update(
status='expired',
completed_at=now
)
self.stdout.write(
self.style.SUCCESS(f'✅ Marked {count} expired sessions as expired')
)
else:
self.stdout.write('✅ No expired sessions found')