59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
"""Gunicorn configuration file for ESPHome Gitea Sync Service"""
|
|
import logging
|
|
from threading import Thread
|
|
|
|
# Gunicorn configuration
|
|
bind = "0.0.0.0:5000"
|
|
workers = 2
|
|
timeout = 600
|
|
loglevel = "info"
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Track if file watcher has been started
|
|
_watcher_started = False
|
|
|
|
|
|
def on_starting(server):
|
|
"""
|
|
Called just before the master process is initialized.
|
|
Perfect for one-time setup like git initialization.
|
|
"""
|
|
logger.info("Gunicorn master process starting - initializing git repository")
|
|
|
|
# Import here to avoid circular imports
|
|
from app import initialize_git_repo
|
|
|
|
try:
|
|
initialize_git_repo()
|
|
logger.info("Git repository initialized successfully")
|
|
except Exception as e:
|
|
logger.error(f"Failed to initialize git repository: {e}")
|
|
|
|
|
|
def post_fork(server, worker):
|
|
"""
|
|
Called after a worker has been forked.
|
|
Start the file watcher only in the first worker to avoid duplicates.
|
|
"""
|
|
global _watcher_started
|
|
|
|
# Only start the file watcher in the first worker (age/number would be 0)
|
|
# Check worker.age which is the worker's sequential number
|
|
if worker.age == 0 and not _watcher_started:
|
|
_watcher_started = True
|
|
logger.info(f"Starting file watcher in worker {worker.pid}")
|
|
|
|
# Import here to avoid circular imports
|
|
from app import start_file_watcher
|
|
|
|
try:
|
|
# Start file watcher in a daemon thread
|
|
watcher_thread = Thread(target=start_file_watcher, daemon=True)
|
|
watcher_thread.start()
|
|
logger.info("File watcher started successfully")
|
|
except Exception as e:
|
|
logger.error(f"Failed to start file watcher: {e}")
|
|
else:
|
|
logger.info(f"Worker {worker.pid} started (file watcher already running in another worker)")
|