Coverage for celery_app.py: 100.00%

10 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 18:53 +0000

1""" 

2Celery Application Configuration 

3 

4This module configures the Celery application for distributed task processing. 

5Workers can run separately from the FastAPI application, allowing for: 

6- Async processing of match data from match-scraper 

7- Horizontal scaling of workers 

8- Fault tolerance and automatic retries 

9- Task result tracking via Redis 

10 

11Usage: 

12 # Start worker 

13 celery -A celery_app worker --loglevel=info 

14 

15 # Submit task from application 

16 from celery_app import process_match_data 

17 task = process_match_data.delay(match_data) 

18""" 

19 

20import os 

21 

22from celery import Celery 

23from celery.schedules import crontab 

24from kombu import Exchange, Queue 

25 

26# Celery broker and result backend configuration 

27# These can be overridden with environment variables 

28# Default to cluster-internal names, fallback to localhost for local development 

29BROKER_URL = os.getenv( 

30 "CELERY_BROKER_URL", 

31 os.getenv("RABBITMQ_URL", "amqp://admin:admin123@localhost:5672//"), # pragma: allowlist secret 

32) 

33RESULT_BACKEND = os.getenv("CELERY_RESULT_BACKEND", os.getenv("REDIS_URL", "redis://localhost:6379/0")) 

34 

35# MT Power Rating (SB-1807). Its own queue so the prod worker consumes it 

36# explicitly (`--queues=matches.prod,power_rating`); the worker runs the beat 

37# embedded, single replica (k3s/worker/deployment-prod.yaml). 

38POWER_RATING_QUEUE = "power_rating" 

39 

40# Celery beat. Nothing here runs unless a beat process does: the k3s prod 

41# worker starts one with `--beat`. 

42BEAT_SCHEDULE = { 

43 # Design default 10:00 UTC; move to just after the nightly reconcile 

44 # (SB-1787) once that schedule is fixed. 

45 "power-rating-nightly": { 

46 "task": "power_rating.recompute_all", 

47 "schedule": crontab(hour=10, minute=0), 

48 "options": {"queue": POWER_RATING_QUEUE}, 

49 }, 

50} 

51 

52# Create Celery app instance 

53app = Celery( 

54 "missing_table", 

55 broker=BROKER_URL, 

56 backend=RESULT_BACKEND, 

57) 

58 

59# Celery configuration 

60app.conf.update( 

61 # Serialization 

62 task_serializer="json", 

63 accept_content=["json"], 

64 result_serializer="json", 

65 # Timezone 

66 timezone="UTC", 

67 enable_utc=True, 

68 # Task tracking 

69 task_track_started=True, 

70 task_time_limit=300, # 5 minutes hard limit 

71 task_soft_time_limit=240, # 4 minutes soft limit 

72 # Results 

73 result_expires=3600, # Results expire after 1 hour 

74 result_extended=True, # Store additional task metadata 

75 # Retries 

76 task_acks_late=True, # Acknowledge task after completion, not on receive 

77 task_reject_on_worker_lost=True, # Requeue task if worker dies 

78 # Worker configuration 

79 worker_prefetch_multiplier=1, # Worker fetches 1 task at a time (fair distribution) 

80 worker_max_tasks_per_child=1000, # Restart worker after 1000 tasks (prevent memory leaks) 

81 # Task routing 

82 task_routes={ 

83 "celery_tasks.match_tasks.*": {"queue": "match_processing"}, 

84 "celery_tasks.validation_tasks.*": {"queue": "validation"}, 

85 "power_rating.*": {"queue": POWER_RATING_QUEUE}, 

86 }, 

87 # Queue configuration 

88 task_queues=( 

89 Queue("match_processing", Exchange("match_processing"), routing_key="match.*"), 

90 Queue("validation", Exchange("validation"), routing_key="validation.*"), 

91 Queue("celery", Exchange("celery"), routing_key="celery"), # Default queue 

92 # Legacy direct queue: MSA pods configured with AGENT_QUEUE_NAME=matches.prod 

93 # bypass the fanout exchange and publish here. Worker must consume both until 

94 # all pods are switched back to using the matches-fanout exchange. 

95 Queue("matches.prod"), 

96 Queue(POWER_RATING_QUEUE), 

97 ), 

98 # Scheduled tasks (Celery beat) 

99 beat_schedule=BEAT_SCHEDULE, 

100 # Monitoring 

101 task_send_sent_event=True, # Enable task-sent events 

102 worker_send_task_events=True, # Enable worker task events 

103) 

104 

105# Auto-discover tasks in celery_tasks module 

106app.autodiscover_tasks(["celery_tasks"]) 

107 

108if __name__ == "__main__": 

109 app.start()