Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (60 loc) · 2.58 KB
/
Copy pathmain.py
File metadata and controls
71 lines (60 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Application entry point for OpenStreamRotator.
Configures logging, loads environment variables, and runs the
AutomationController async main loop.
"""
import asyncio
import logging
import sys
import os
import io
import threading
# Force UTF-8 encoding for console output to handle Unicode characters
os.environ['PYTHONIOENCODING'] = 'utf-8'
# Frozen-aware project root (PyInstaller bundles to temp dir)
if getattr(sys, 'frozen', False):
_app_root = os.path.dirname(sys.executable)
else:
_app_root = os.path.dirname(os.path.abspath(__file__))
log_dir = _app_root
log_file = os.path.join(log_dir, 'osr.log')
# Setup logging with explicit formatter on handlers
formatter = logging.Formatter(
fmt='%(asctime)s [%(levelname)s] %(name)s - %(message)s',
datefmt='%H:%M:%S'
)
file_handler = logging.FileHandler(log_file, encoding='utf-8')
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG) # File gets all levels
console_handler = logging.StreamHandler(io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8'))
console_handler.setFormatter(formatter)
console_handler.setLevel(logging.DEBUG) # Console gets all levels
# Configure root logger
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG) # Root logger captures everything
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
# Suppress DEBUG logs from libraries to reduce spam
logging.getLogger('obsws_python').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Import this last to ensure logging is configured first
from controllers.automation_controller import AutomationController
if __name__ == "__main__":
try:
controller = AutomationController()
asyncio.run(controller.run())
except (KeyboardInterrupt, asyncio.CancelledError):
logger.info("Shutdown complete")
except Exception as e:
logger.error(f"Fatal error: {e}", exc_info=True)
sys.exit(1)
finally:
# Force-exit if a background download thread is still blocking after a grace period.
# yt-dlp's extract_info() cannot be interrupted from another thread,
# so os._exit is the last resort to avoid hanging indefinitely.
alive_non_daemon = [t for t in threading.enumerate()
if t.is_alive() and not t.daemon and t.name != 'MainThread']
if alive_non_daemon:
logger.warning(f"Force-exiting: {len(alive_non_daemon)} non-daemon threads still alive: "
f"{[t.name for t in alive_non_daemon]}")
os._exit(0)