[Unit]
Description=MES Dashboard Trace Worker (RQ, Conda Runtime)
Documentation=https://github.com/your-org/mes-dashboard
After=network.target redis-server.service
Requires=redis-server.service

[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/mes-dashboard
EnvironmentFile=-/opt/mes-dashboard/.env
Environment="PYTHONPATH=/opt/mes-dashboard/src"

ExecStart=/usr/bin/env bash -lc 'exec "${CONDA_BIN:-/opt/miniconda3/bin/conda}" run --no-capture-output -n "${CONDA_ENV_NAME:-mes-dashboard}" rq worker "${TRACE_WORKER_QUEUE:-trace-events}" --url "${REDIS_URL:-redis://localhost:6379/0}"'

KillSignal=SIGTERM
TimeoutStopSec=60
Restart=always
RestartSec=10

StandardOutput=journal
StandardError=journal
SyslogIdentifier=mes-dashboard-trace-worker

# Memory protection: trace worker handles large queries independently.
# MemoryMax prevents single large job from killing the VM.
MemoryHigh=3G
MemoryMax=4G

NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ReadWritePaths=/opt/mes-dashboard/logs

[Install]
WantedBy=multi-user.target
