# =============================================================================
# Supervisor Configuration for Adtlas Platform
# =============================================================================
# This configuration manages multiple processes in the Docker container:
# - Django application server (Gunicorn)
# - Celery worker for background tasks
# - Celery beat for scheduled tasks
# - Log rotation and monitoring
# =============================================================================

[supervisord]
nodaemon=true
user=adtlas
logfile=/app/logs/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/tmp/supervisord.pid

[unix_http_server]
file=/tmp/supervisor.sock
chmod=0700
chown=adtlas:adtlas

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

# =============================================================================
# Django Application Server (Gunicorn)
# =============================================================================
[program:gunicorn]
command=gunicorn --bind 0.0.0.0:8000 --workers 4 --worker-class gevent --worker-connections 1000 --max-requests 1000 --max-requests-jitter 100 --timeout 30 --keep-alive 2 --log-level info --access-logfile - --error-logfile - core.wsgi:application
directory=/app/src
user=adtlas
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/app/logs/gunicorn.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10
stopwaitsecs=30
killasgroup=true
stopsignal=TERM
environment=DJANGO_SETTINGS_MODULE="core.settings"

# =============================================================================
# Celery Worker for Background Tasks
# =============================================================================
[program:celery_worker]
command=celery -A core worker --loglevel=info --concurrency=2 --max-tasks-per-child=1000
directory=/app/src
user=adtlas
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/app/logs/celery_worker.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10
stopwaitsecs=60
killasgroup=true
stopsignal=TERM
environment=DJANGO_SETTINGS_MODULE="core.settings"
priority=1000

# =============================================================================
# Celery Beat for Scheduled Tasks
# =============================================================================
[program:celery_beat]
command=celery -A core beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler
directory=/app/src
user=adtlas
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/app/logs/celery_beat.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10
stopwaitsecs=30
killasgroup=true
stopsignal=TERM
environment=DJANGO_SETTINGS_MODULE="core.settings"
priority=1001

# =============================================================================
# Log Rotation Service
# =============================================================================
[program:logrotate]
command=/usr/sbin/logrotate -f /app/docker/logrotate.conf
directory=/app
user=root
autostart=false
autorestart=false
startsecs=0
stdout_logfile=/dev/null
stderr_logfile=/dev/null

# =============================================================================
# Health Check Monitor
# =============================================================================
[program:healthcheck]
command=/usr/local/bin/healthcheck.sh
directory=/app
user=adtlas
autostart=false
autorestart=false
startsecs=0
stdout_logfile=/app/logs/healthcheck.log
stderr_logfile=/app/logs/healthcheck.log

# =============================================================================
# Event Listeners
# =============================================================================
[eventlistener:memmon]
command=memmon -a 400MB -m dev@adtlas.com
events=PROCESS_STATE_RUNNING
buffer_size=10
user=adtlas
autostart=true
autorestart=true

# =============================================================================
# Group Configuration
# =============================================================================
[group:adtlas]
programs=gunicorn,celery_worker,celery_beat
priority=999

# =============================================================================
# Configuration Notes
# =============================================================================
# Process Management:
# - All processes run as non-root user 'adtlas'
# - Graceful shutdown with appropriate timeouts
# - Automatic restart on failure
# - Resource monitoring and alerting
#
# Logging:
# - Centralized log management
# - Log rotation to prevent disk space issues
# - Structured logging for better monitoring
#
# Security:
# - Unix socket for supervisor communication
# - Proper file permissions
# - Process isolation
# =============================================================================