-
Notifications
You must be signed in to change notification settings - Fork 10
/
celery_config.py
130 lines (106 loc) · 4.8 KB
/
celery_config.py
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
# http://docs.celeryq.org/en/latest/configuration.html#configuration
import gc
import logging
import logging.config
from datetime import timedelta
from celery import signals
from celery.beat import BeatLazyFunc
from celery.schedules import crontab
from shared.celery_config import (
BaseCeleryConfig,
brolly_stats_rollup_task_name,
gh_app_webhook_check_task_name,
health_check_task_name,
profiling_finding_task_name,
)
from shared.config import get_config
from shared.helpers.cache import RedisBackend
from celery_task_router import route_task
from helpers.cache import cache
from helpers.clock import get_utc_now_as_iso_format
from helpers.health_check import get_health_check_interval_seconds
from services.redis import get_redis_connection
log = logging.getLogger(__name__)
@signals.worker_before_create_process.connect
def prefork_gc_freeze(**kwargs) -> None:
# This comes from https://github.com/getsentry/sentry/pull/63001
# More info https://www.youtube.com/watch?v=Hgw_RlCaIds
# The idea is to save memory in the worker subprocesses
# By freezing all the stuff we can just read from
gc.freeze()
@signals.setup_logging.connect
def initialize_logging(loglevel=logging.INFO, **kwargs):
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):
log.info("Initialized cache")
redis_cache_backend = RedisBackend(get_redis_connection())
cache.configure(redis_cache_backend)
hourly_check_task_name = "app.cron.hourly_check.HourlyCheckTask"
daily_plan_manager_task_name = "app.cron.daily.PlanManagerTask"
notify_error_task_name = "app.tasks.notify.NotifyErrorTask"
# Backfill GH Apps
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"
trial_expiration_task_name = "app.tasks.plan.TrialExpirationTask"
trial_expiration_cron_task_name = "app.cron.plan.TrialExpirationCronTask"
update_branches_task_name = "app.cron.branches.UpdateBranchesTask"
update_branches_task_name = "app.cron.test_instances.BackfillTestInstancesTask"
def _beat_schedule():
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"),
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
"trial_expiration_cron": {
"task": trial_expiration_cron_task_name,
# 4 UTC is 12am EDT
"schedule": crontab(minute="0", hour="4"),
"kwargs": {
"cron_task_generation_time_iso": BeatLazyFunc(get_utc_now_as_iso_format)
},
},
}
if get_config("setup", "find_uncollected_profilings", "enabled", default=True):
beat_schedule["find_uncollected_profilings"] = {
"task": profiling_finding_task_name,
"schedule": crontab(minute="0,15,30,45"),
"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)
},
}
return beat_schedule
class CeleryWorkerConfig(BaseCeleryConfig):
beat_schedule = _beat_schedule()
task_routes = route_task