100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
"""
|
|
VideoDetect - Video Classification System
|
|
|
|
Main entry point for the worker service.
|
|
Initializes all components and starts the processing pipeline.
|
|
"""
|
|
|
|
import logging
|
|
import signal
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
# Add src to path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
from config_loader import get_config
|
|
from db_connector import DBConnector
|
|
from logging_config import setup_logging
|
|
from orchestrator import WorkerPool
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def graceful_shutdown(signum, frame):
|
|
"""Handle shutdown signals gracefully."""
|
|
logger.info("Received signal %d, initiating graceful shutdown...", signum)
|
|
sys.exit(0)
|
|
|
|
|
|
def main():
|
|
"""Initialize and start the VideoDetect worker."""
|
|
# Register signal handlers
|
|
signal.signal(signal.SIGTERM, graceful_shutdown)
|
|
signal.signal(signal.SIGINT, graceful_shutdown)
|
|
|
|
# Load configuration
|
|
config = get_config()
|
|
log_config = config.get_section("logging")
|
|
setup_logging(
|
|
level=log_config.get("level", "INFO"),
|
|
log_format=log_config.get("format", "json"),
|
|
rotation_max_bytes=log_config.get("rotation_max_bytes", 104857600),
|
|
rotation_backup_count=log_config.get("rotation_backup_count", 10),
|
|
)
|
|
|
|
logger.info("VideoDetect Worker starting...")
|
|
logger.info("Config: %s", config)
|
|
|
|
# Initialize database connection
|
|
db_config = config.get_section("database")
|
|
db = DBConnector(
|
|
host=db_config.get("host", "mariadb"),
|
|
port=db_config.get("port", 3306),
|
|
database=db_config.get("name", "videodetect"),
|
|
user=db_config.get("user", "videodetect"),
|
|
password=db_config.get("password", "videodetect123"),
|
|
pool_size=db_config.get("pool_size", 20),
|
|
pool_min=db_config.get("pool_min", 5),
|
|
pool_recycle=db_config.get("pool_recycle", 3600),
|
|
)
|
|
|
|
# Verify database connectivity
|
|
if not db.health_check():
|
|
logger.error("Cannot connect to database. Exiting.")
|
|
sys.exit(1)
|
|
logger.info("Database connection established.")
|
|
|
|
# Initialize schema if needed
|
|
schema_path = Path(__file__).parent.parent / "db" / "schema.sql"
|
|
if schema_path.exists():
|
|
db.initialize_schema(str(schema_path))
|
|
logger.info("Schema initialized.")
|
|
|
|
# Verify GPU availability
|
|
try:
|
|
import torch
|
|
if torch.cuda.is_available():
|
|
gpu_count = torch.cuda.device_count()
|
|
gpu_name = torch.cuda.get_device_name(0)
|
|
logger.info("GPU available: %d GPUs, primary: %s", gpu_count, gpu_name)
|
|
else:
|
|
logger.warning("CUDA is not available! Processing will be slow.")
|
|
except ImportError:
|
|
logger.warning("PyTorch not installed. GPU features disabled.")
|
|
|
|
logger.info("Worker initialization complete. Starting processing loop...")
|
|
|
|
pool = WorkerPool(db, config.data, max_workers=1)
|
|
|
|
try:
|
|
pool.start()
|
|
except KeyboardInterrupt:
|
|
logger.info("Worker shutting down.")
|
|
pool.stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|