forked from codecov/umbrella
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcelery_config.py
More file actions
154 lines (131 loc) · 5.95 KB
/
Copy pathcelery_config.py
File metadata and controls
154 lines (131 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# https://docs.celeryq.dev/en/stable/userguide/configuration.html
import gc
import logging
from datetime import timedelta
from celery import signals
from celery.beat import BeatLazyFunc
from celery.schedules import crontab
from celery_task_router import route_task
from helpers.clock import get_utc_now_as_iso_format
from helpers.health_check import get_health_check_interval_seconds
from shared.celery_config import (
BaseCeleryConfig,
brolly_stats_rollup_task_name,
gh_app_webhook_check_task_name,
health_check_task_name,
partition_management_task_name,
process_owners_to_be_deleted_cron_task_name,
)
from shared.config import get_config
from shared.helpers.cache import RedisBackend, cache
from shared.helpers.redis import get_redis_connection
log = logging.getLogger(__name__)
@signals.worker_before_create_process.connect
def prefork_gc_freeze(**kwargs) -> None:
"""Freeze GC to reduce memory usage in worker subprocesses.
Reference: https://github.com/getsentry/sentry/pull/63001
"""
gc.freeze()
@signals.setup_logging.connect
def initialize_logging(loglevel=logging.INFO, **kwargs):
"""Initialize Celery logging configuration."""
celery_logger = logging.getLogger("celery")
celery_logger.setLevel(loglevel)
log.info("Initialized celery logging")
return celery_logger
@signals.worker_process_init.connect
def initialize_cache(**kwargs):
"""Initialize Redis cache backend for worker processes."""
log.info("Initialized cache")
redis_cache_backend = RedisBackend(get_redis_connection())
cache.configure(redis_cache_backend)
# Cron task names
hourly_check_task_name = "app.cron.hourly_check.HourlyCheckTask"
daily_plan_manager_task_name = "app.cron.daily.PlanManagerTask"
regular_cleanup_cron_task_name = "app.cron.cleanup.RegularCleanup"
cache_rollup_cron_task_name = "app.cron.cache_rollup.CacheRollupTask"
trial_expiration_cron_task_name = "app.cron.plan.TrialExpirationCronTask"
# Regular task names
notify_error_task_name = "app.tasks.notify.NotifyErrorTask"
trial_expiration_task_name = "app.tasks.plan.TrialExpirationTask"
# GitHub App backfill task names
backfill_existing_gh_app_installations_name = "app.tasks.backfill_existing_gh_app_installations.BackfillExistingGHAppInstallationsTask"
backfill_existing_individual_gh_app_installation_name = "app.tasks.backfill_existing_individual_gh_app_installation.BackfillExistingIndividualGHAppInstallationTask"
backfill_owners_without_gh_app_installations_name = "app.tasks.backfill_owners_without_gh_app_installations.BackfillOwnersWithoutGHAppInstallationsTask"
backfill_owners_without_gh_app_installation_individual_name = "app.tasks.backfill_owners_without_gh_app_installation_individual.BackfillOwnersWithoutGHAppInstallationIndividualTask"
def _beat_schedule():
"""Build Celery beat schedule configuration for periodic tasks."""
beat_schedule = {
"hourly_check": {
"task": hourly_check_task_name,
"schedule": crontab(minute="0"),
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
"github_app_webhooks_task": {
"task": gh_app_webhook_check_task_name,
"schedule": crontab(minute="0", hour="0,6,12,18"), # Every 6 hours
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
"trial_expiration_cron": {
"task": trial_expiration_cron_task_name,
"schedule": crontab(minute="0", hour="4"), # 4 AM UTC (12 AM EDT)
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
"regular_cleanup": {
"task": regular_cleanup_cron_task_name,
"schedule": crontab(minute="0", hour="4"), # 4 AM UTC (12 AM EDT)
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
"partition_management": {
"task": partition_management_task_name,
"schedule": crontab(minute="0", hour="5"), # 5 AM UTC
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
"process_owners_to_be_deleted": {
"task": process_owners_to_be_deleted_cron_task_name,
# Top of hour at 07:00, 08:00 UTC (~2-3 AM Central when UTC-5; wall time drifts with DST).
"schedule": crontab(minute="0", hour="7,8"),
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
}
if get_config("setup", "health_check", "enabled", default=False):
beat_schedule["health_check_task"] = {
"task": health_check_task_name,
"schedule": timedelta(seconds=get_health_check_interval_seconds()),
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
}
if get_config("setup", "telemetry", "enabled", default=True):
beat_schedule["brolly_stats_rollup"] = {
"task": brolly_stats_rollup_task_name,
"schedule": crontab(minute="0", hour="2"),
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
}
if get_config("setup", "cache_rollup", "enabled", default=False):
beat_schedule["cache_rollup"] = {
"task": cache_rollup_cron_task_name,
"schedule": crontab(minute="0", hour="2"),
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
}
return beat_schedule
class CeleryWorkerConfig(BaseCeleryConfig):
"""Celery worker configuration with beat schedule and task routing."""
beat_schedule = _beat_schedule()
task_routes = route_task