Log a system event and maintain the configured max history. Args: event_type: Type of event (e.g., 'channel_start', 'client_connect') channel_id: Optional UUID of the channel channel_name: Optional name of the channel **details: Additional details to store i
(event_type, channel_id=None, channel_name=None, **details)
| 825 | |
| 826 | |
| 827 | def log_system_event(event_type, channel_id=None, channel_name=None, **details): |
| 828 | """ |
| 829 | Log a system event and maintain the configured max history. |
| 830 | |
| 831 | Args: |
| 832 | event_type: Type of event (e.g., 'channel_start', 'client_connect') |
| 833 | channel_id: Optional UUID of the channel |
| 834 | channel_name: Optional name of the channel |
| 835 | **details: Additional details to store in the event (stored as JSON) |
| 836 | |
| 837 | Example: |
| 838 | log_system_event('channel_start', channel_id=uuid, channel_name='CNN', |
| 839 | stream_url='http://...', user='admin') |
| 840 | """ |
| 841 | from core.models import SystemEvent, CoreSettings |
| 842 | from django.db import close_old_connections |
| 843 | |
| 844 | try: |
| 845 | # Create the event |
| 846 | SystemEvent.objects.create( |
| 847 | event_type=event_type, |
| 848 | channel_id=channel_id, |
| 849 | channel_name=channel_name, |
| 850 | details=details |
| 851 | ) |
| 852 | |
| 853 | # Connect integrations and plugin event hooks (non-blocking on gevent uWSGI) |
| 854 | _dispatch_system_event_integrations( |
| 855 | event_type, |
| 856 | channel_id=channel_id, |
| 857 | channel_name=channel_name, |
| 858 | **details, |
| 859 | ) |
| 860 | |
| 861 | # Get max events from settings (default 100) |
| 862 | try: |
| 863 | from .models import CoreSettings |
| 864 | system_settings = CoreSettings.objects.filter(key='system_settings').first() |
| 865 | if system_settings and isinstance(system_settings.value, dict): |
| 866 | max_events = int(system_settings.value.get('max_system_events', 100)) |
| 867 | else: |
| 868 | max_events = 100 |
| 869 | except Exception: |
| 870 | max_events = 100 |
| 871 | |
| 872 | # Delete old events beyond the limit (keep it efficient with a single query) |
| 873 | total_count = SystemEvent.objects.count() |
| 874 | if total_count > max_events: |
| 875 | # Get the ID of the event at the cutoff point |
| 876 | cutoff_event = SystemEvent.objects.values_list('id', flat=True)[max_events] |
| 877 | # Delete all events with ID less than cutoff (older events) |
| 878 | SystemEvent.objects.filter(id__lt=cutoff_event).delete() |
| 879 | |
| 880 | except Exception as e: |
| 881 | # Don't let event logging break the main application |
| 882 | logger.error(f"Failed to log system event {event_type}: {e}") |
| 883 | finally: |
| 884 | # geventpool keeps checked-out connections until close(); release promptly |