Spaces:
Running
Running
AuthorBot
Production hardening: JWT blacklist, TOTP, Pydantic schemas, Prometheus, SSRF fix, CSP, Redis auth, Celery backup β 35 items across P0-P5
131d826 | """Author RAG Chatbot SaaS β Celery Application Factory. | |
| All background tasks are registered here. | |
| Scheduled tasks (beat) are also configured here. | |
| """ | |
| from celery import Celery | |
| from celery.schedules import crontab | |
| from app.config import get_settings | |
| cfg = get_settings() | |
| celery_app = Celery( | |
| "authorbot", | |
| broker=cfg.CELERY_BROKER_URL, | |
| backend=cfg.CELERY_RESULT_BACKEND, | |
| include=[ | |
| "app.tasks.ingestion_task", | |
| "app.tasks.analytics_task", | |
| "app.tasks.email_task", | |
| "app.tasks.expiry_check_task", | |
| "app.tasks.link_health_task", | |
| "app.tasks.geo_update_task", | |
| "app.tasks.backup_task", # R-131: Backup task must be in include list | |
| ], | |
| ) | |
| celery_app.conf.update( | |
| task_serializer="json", | |
| result_serializer="json", | |
| accept_content=["json"], | |
| timezone="UTC", | |
| enable_utc=True, | |
| task_track_started=True, | |
| task_acks_late=True, # Re-queue if worker crashes | |
| worker_prefetch_multiplier=1, # One task at a time per worker slot | |
| ) | |
| # ββ Scheduled Tasks (Celery Beat) βββββββββββββββββββββββββ | |
| celery_app.conf.beat_schedule = { | |
| # Analytics rollup: runs every hour | |
| "analytics-hourly-rollup": { | |
| "task": "app.tasks.analytics_task.aggregate_hourly", | |
| "schedule": crontab(minute=0), | |
| }, | |
| # Subscription expiry check: runs every day at 8am UTC | |
| "subscription-expiry-check": { | |
| "task": "app.tasks.expiry_check_task.check_expiring_subscriptions", | |
| "schedule": crontab(hour=8, minute=0), | |
| }, | |
| # Link health check: runs every day at 3am UTC | |
| "link-health-check": { | |
| "task": "app.tasks.link_health_task.check_all_links", | |
| "schedule": crontab(hour=3, minute=0), | |
| }, | |
| # MaxMind GeoIP DB update: runs every Sunday at 2am UTC | |
| "geoip-db-update": { | |
| "task": "app.tasks.geo_update_task.update_geoip_database", | |
| "schedule": crontab(hour=2, minute=0, day_of_week=0), | |
| }, | |
| # Weekly digest emails: every Monday at 9am UTC | |
| "weekly-digest-emails": { | |
| "task": "app.tasks.email_task.send_weekly_digests", | |
| "schedule": crontab(hour=9, minute=0, day_of_week=1), | |
| }, | |
| # R-106/R-131: Database backup: daily at 4am UTC | |
| "daily-database-backup": { | |
| "task": "app.tasks.backup_task.run_backup", | |
| "schedule": crontab(hour=4, minute=0), | |
| }, | |
| } | |