from django.apps import AppConfig
from . import services
import signal, sys


class SseConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'sse'

    def ready(self):
        def cleanup_on_shutdown():
            """在接收到终止信号时设置关闭事件"""
            from .views import _shutdown_event
            _shutdown_event.set()
            services.cleanup_all_connections()
            sys.exit(0)

        signal.signal(signal.SIGINT, lambda s, f: cleanup_on_shutdown())
        signal.signal(signal.SIGTERM, lambda s, f: cleanup_on_shutdown())
