pahvalentines/backend/tasks/celery_app.py
michael 2d9c6ca7a7 refactor: convert polling tasks to event-based architecture
- Remove process_pending_queue and check_credits scheduled tasks
- Add check_submission_timeout task for per-submission timeout handling
- Modify send_to_sonauto to schedule timeout check after successful API call
- Reduce check_timeouts frequency to 30min (safety net only)
- Update submissions endpoint to use apply_async with better error logging
- Remove unused config settings (QUEUE_PROCESSOR_INTERVAL, CREDITS_CHECK_INTERVAL)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 00:28:52 -06:00

52 lines
1.5 KiB
Python

"""Celery application configuration and Beat schedule."""
import os
import sys
from celery import Celery
from celery.schedules import crontab
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.config import settings
# Create Celery app
app = Celery("pah_workers")
# Configure Celery
app.config_from_object(
{
"broker_url": settings.CELERY_BROKER_URL,
"result_backend": settings.CELERY_RESULT_BACKEND,
"task_serializer": "json",
"result_serializer": "json",
"accept_content": ["json"],
"timezone": "UTC",
"task_track_started": True,
"task_acks_late": True,
"worker_prefetch_multiplier": 1,
# Queue routing: video generation runs on dedicated queue
"task_routes": {
"tasks.workers.create_video": {"queue": "video"},
},
"task_default_queue": "celery",
}
)
# Beat schedule for periodic tasks
# Note: Most tasks are now event-based. check_timeouts remains as a safety net
# for edge cases (e.g., scheduled timeout tasks that failed to run).
app.conf.beat_schedule = {
"check-stale-submissions": {
"task": "tasks.workers.check_timeouts",
"schedule": 1800.0, # Every 30 minutes (safety net only)
},
"cleanup-old-files": {
"task": "tasks.workers.cleanup_old_files",
"schedule": crontab(hour=3, minute=0), # Daily at 3 AM
},
}
# Auto-discover tasks (module is tasks.workers)
app.autodiscover_tasks(["tasks"], related_name="workers")