|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set -e |
|
|
|
|
|
echo "๐ Setting up Celery Workers for 24/7 Auto-Investigation" |
|
|
echo "==========================================================" |
|
|
|
|
|
|
|
|
if [ "$EUID" -ne 0 ]; then |
|
|
echo "โ Please run as root (sudo)" |
|
|
exit 1 |
|
|
fi |
|
|
|
|
|
|
|
|
echo "๐ฆ Updating system..." |
|
|
apt-get update |
|
|
apt-get upgrade -y |
|
|
|
|
|
|
|
|
echo "๐ฆ Installing dependencies..." |
|
|
apt-get install -y python3 python3-pip python3-venv redis-server supervisor git |
|
|
|
|
|
|
|
|
echo "๐ด Starting Redis..." |
|
|
systemctl start redis |
|
|
systemctl enable redis |
|
|
|
|
|
|
|
|
echo "๐ค Creating app user..." |
|
|
useradd -m -s /bin/bash cidadao-ai || true |
|
|
|
|
|
|
|
|
echo "๐ฅ Cloning repository..." |
|
|
cd /opt |
|
|
git clone https://github.com/anderson-ufrj/cidadao.ai-backend.git || true |
|
|
cd cidadao-ai-backend |
|
|
chown -R cidadao-ai:cidadao-ai /opt/cidadao-ai-backend |
|
|
|
|
|
|
|
|
echo "๐ Setting up Python environment..." |
|
|
su - cidadao-ai -c "cd /opt/cidadao-ai-backend && python3 -m venv venv" |
|
|
su - cidadao-ai -c "cd /opt/cidadao-ai-backend && venv/bin/pip install --upgrade pip" |
|
|
su - cidadao-ai -c "cd /opt/cidadao-ai-backend && venv/bin/pip install -r requirements.txt" |
|
|
|
|
|
|
|
|
echo "โ๏ธ Creating .env file..." |
|
|
cat > /opt/cidadao-ai-backend/.env << 'EOF' |
|
|
|
|
|
REDIS_URL=redis://localhost:6379/0 |
|
|
|
|
|
|
|
|
SUPABASE_URL=your-supabase-url-here |
|
|
SUPABASE_SERVICE_ROLE_KEY=your-supabase-key-here |
|
|
|
|
|
|
|
|
TRANSPARENCY_API_KEY=your-api-key-here |
|
|
|
|
|
|
|
|
GROQ_API_KEY=your-groq-key-here |
|
|
|
|
|
|
|
|
ENVIRONMENT=production |
|
|
ENABLE_CELERY_WORKERS=true |
|
|
EOF |
|
|
|
|
|
echo "โ ๏ธ IMPORTANT: Edit /opt/cidadao-ai-backend/.env with your credentials!" |
|
|
echo " - SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY (same as HF Spaces)" |
|
|
echo " - TRANSPARENCY_API_KEY (optional but recommended)" |
|
|
echo " - GROQ_API_KEY (for AI agents)" |
|
|
|
|
|
|
|
|
echo "๐ Creating Supervisor config for Worker..." |
|
|
cat > /etc/supervisor/conf.d/cidadao-ai-worker.conf << 'EOF' |
|
|
[program:cidadao-ai-worker] |
|
|
command=/opt/cidadao-ai-backend/venv/bin/celery -A src.infrastructure.queue.celery_app worker --loglevel=info --queues=critical,high,default,low,background --concurrency=4 |
|
|
directory=/opt/cidadao-ai-backend |
|
|
user=cidadao-ai |
|
|
autostart=true |
|
|
autorestart=true |
|
|
stopasgroup=true |
|
|
killasgroup=true |
|
|
redirect_stderr=true |
|
|
stdout_logfile=/var/log/celery/worker.log |
|
|
stdout_logfile_maxbytes=50MB |
|
|
stdout_logfile_backups=10 |
|
|
stderr_logfile=/var/log/celery/worker.err.log |
|
|
environment=PATH="/opt/cidadao-ai-backend/venv/bin" |
|
|
EOF |
|
|
|
|
|
|
|
|
echo "๐ Creating Supervisor config for Beat..." |
|
|
cat > /etc/supervisor/conf.d/cidadao-ai-beat.conf << 'EOF' |
|
|
[program:cidadao-ai-beat] |
|
|
command=/opt/cidadao-ai-backend/venv/bin/celery -A src.infrastructure.queue.celery_app beat --loglevel=info |
|
|
directory=/opt/cidadao-ai-backend |
|
|
user=cidadao-ai |
|
|
autostart=true |
|
|
autorestart=true |
|
|
redirect_stderr=true |
|
|
stdout_logfile=/var/log/celery/beat.log |
|
|
stdout_logfile_maxbytes=50MB |
|
|
stdout_logfile_backups=10 |
|
|
stderr_logfile=/var/log/celery/beat.err.log |
|
|
environment=PATH="/opt/cidadao-ai-backend/venv/bin" |
|
|
EOF |
|
|
|
|
|
|
|
|
mkdir -p /var/log/celery |
|
|
chown cidadao-ai:cidadao-ai /var/log/celery |
|
|
|
|
|
|
|
|
echo "๐ Reloading Supervisor..." |
|
|
supervisorctl reread |
|
|
supervisorctl update |
|
|
|
|
|
|
|
|
echo "โถ๏ธ Starting Celery services..." |
|
|
supervisorctl start cidadao-ai-worker |
|
|
supervisorctl start cidadao-ai-beat |
|
|
|
|
|
|
|
|
sleep 3 |
|
|
echo "" |
|
|
echo "๐ Service Status:" |
|
|
supervisorctl status cidadao-ai-worker cidadao-ai-beat |
|
|
|
|
|
echo "" |
|
|
echo "โ
Setup complete!" |
|
|
echo "" |
|
|
echo "๐ Next steps:" |
|
|
echo "1. Edit /opt/cidadao-ai-backend/.env with your credentials" |
|
|
echo "2. Restart services: supervisorctl restart cidadao-ai-worker cidadao-ai-beat" |
|
|
echo "3. Monitor logs: tail -f /var/log/celery/worker.log" |
|
|
echo "" |
|
|
echo "๐ฏ Workers will automatically investigate contracts 24/7 and save to Supabase" |
|
|
echo "๐ Your HuggingFace API will continue working normally at:" |
|
|
echo " https://neural-thinker-cidadao-ai-backend.hf.space" |
|
|
echo "" |
|
|
echo "๐ฐ Estimated cost: $5-10/month for basic VPS" |
|
|
|