2525logger = logging .getLogger (__name__ )
2626
2727TRANSACTION_PREFIX = "cleanup"
28+ DELETES_BY_PROJECT_CHUNK_SIZE = 100
2829
2930if TYPE_CHECKING :
3031 from sentry .db .models .base import BaseModel
@@ -85,28 +86,15 @@ def multiprocess_worker(task_queue: _WorkQueue) -> None:
8586 # Configure within each Process
8687 import logging
8788
88- from sentry .utils .imports import import_string
89-
9089 logger = logging .getLogger ("sentry.cleanup" )
9190
9291 from sentry .runner import configure
9392
9493 configure ()
9594
96- from sentry import deletions , models , options , similarity
95+ from sentry import options
9796 from sentry .utils import metrics
9897
99- skip_child_relations_models = [
100- # Handled by other parts of cleanup
101- models .EventAttachment ,
102- models .UserReport ,
103- models .Group ,
104- models .GroupEmailThread ,
105- models .GroupRuleStatus ,
106- # Handled by TTL
107- similarity ,
108- ]
109-
11098 while True :
11199 j = task_queue .get ()
112100 if j == _STOP_WORKER :
@@ -124,21 +112,7 @@ def multiprocess_worker(task_queue: _WorkQueue) -> None:
124112 with sentry_sdk .start_transaction (
125113 op = "cleanup" , name = f"{ TRANSACTION_PREFIX } .multiprocess_worker"
126114 ):
127- model = import_string (model_name )
128- task = deletions .get (
129- model = model ,
130- query = {"id__in" : chunk },
131- skip_models = skip_child_relations_models ,
132- transaction_id = uuid4 ().hex ,
133- )
134-
135- while True :
136- debug_output (f"Processing chunk of { len (chunk )} { model_name } objects" )
137- metrics .incr (
138- "cleanup.chunk_processed" , tags = {"model" : model_name }, amount = len (chunk )
139- )
140- if not task .chunk (apply_filter = True ):
141- break
115+ task_execution (model_name , chunk )
142116 except Exception :
143117 metrics .incr (
144118 "cleanup.error" ,
@@ -154,6 +128,37 @@ def multiprocess_worker(task_queue: _WorkQueue) -> None:
154128 task_queue .task_done ()
155129
156130
131+ def task_execution (model_name : str , chunk : tuple [int , ...]) -> None :
132+ from sentry import deletions , models , similarity
133+ from sentry .utils import metrics
134+ from sentry .utils .imports import import_string
135+
136+ skip_child_relations_models = [
137+ # Handled by other parts of cleanup
138+ models .EventAttachment ,
139+ models .UserReport ,
140+ models .Group ,
141+ models .GroupEmailThread ,
142+ models .GroupRuleStatus ,
143+ # Handled by TTL
144+ similarity ,
145+ ]
146+
147+ model = import_string (model_name )
148+ task = deletions .get (
149+ model = model ,
150+ query = {"id__in" : chunk },
151+ skip_models = skip_child_relations_models ,
152+ transaction_id = uuid4 ().hex ,
153+ )
154+
155+ while True :
156+ debug_output (f"Processing chunk of { len (chunk )} { model_name } objects" )
157+ metrics .incr ("cleanup.chunk_processed" , tags = {"model" : model_name }, amount = len (chunk ))
158+ if not task .chunk (apply_filter = True ):
159+ break
160+
161+
157162@click .command ()
158163@click .option ("--days" , default = 30 , show_default = True , help = "Numbers of days to truncate on." )
159164@click .option ("--project" , help = "Limit truncation to only entries from project." )
@@ -760,7 +765,7 @@ def run_bulk_deletes_by_project(
760765 order_by = order_by ,
761766 )
762767
763- for chunk in q .iterator (chunk_size = 100 ):
768+ for chunk in q .iterator (chunk_size = DELETES_BY_PROJECT_CHUNK_SIZE ):
764769 task_queue .put ((imp , chunk ))
765770 except Exception :
766771 capture_exception (
0 commit comments