Skip to content

Commit 8a3b261

Browse files
refactor: Rename LockMaxRetriesExceeded to LockMaxRetriesExceededError and enhance its initialization with detailed parameters
1 parent 7d1588a commit 8a3b261

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

apps/worker/services/lock_manager.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,24 @@ def __init__(self, countdown: int):
3939
self.countdown = countdown
4040

4141

42-
class LockMaxRetriesExceeded(Exception):
42+
class LockMaxRetriesExceededError(Exception):
4343
"""Raised when lock acquisition exceeds max retries."""
4444

45+
def __init__(
46+
self,
47+
retry_num: int,
48+
max_attempts: int,
49+
lock_name: str,
50+
repoid: int,
51+
commitid: str,
52+
):
53+
self.retry_num = retry_num
54+
self.max_attempts = max_attempts
55+
self.lock_name = lock_name
56+
self.repoid = repoid
57+
self.commitid = commitid
58+
super().__init__("Lock acquisition failed after exceeding max retries")
59+
4560

4661
class LockManager:
4762
def __init__(
@@ -131,9 +146,12 @@ def locked(
131146
# None means no max retries (infinite retries)
132147
max_attempts = max_retries + 1 if max_retries is not None else None
133148
if max_attempts is not None and retry_num >= max_attempts:
134-
error_msg = (
135-
f"Lock acquisition failed after {retry_num} attempts (max: {max_attempts}). "
136-
f"Lock: {lock_name}, Repo: {self.repoid}, Commit: {self.commitid}"
149+
error = LockMaxRetriesExceededError(
150+
retry_num=retry_num,
151+
max_attempts=max_attempts,
152+
lock_name=lock_name,
153+
repoid=self.repoid,
154+
commitid=self.commitid,
137155
)
138156
log.error(
139157
"Not retrying since we already had too many retries",
@@ -150,7 +168,7 @@ def locked(
150168
exc_info=True,
151169
)
152170
sentry_sdk.capture_exception(
153-
LockMaxRetriesExceeded(error_msg),
171+
error,
154172
contexts={
155173
"lock_acquisition": {
156174
"blocking_timeout": self.blocking_timeout,
@@ -171,6 +189,7 @@ def locked(
171189
"lock_type": lock_type.value,
172190
},
173191
)
192+
# TODO: should we raise this, or would a return be ok?
174193
raise LockRetry(countdown)
175194

176195
log.warning(

apps/worker/tasks/bundle_analysis_processor.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def process_impl_within_lock(
189189
str | None, params.get("bundle_analysis_compare_sha")
190190
)
191191

192+
result: ProcessingResult | None = None
192193
try:
193194
log.info(
194195
"Processing bundle analysis upload",
@@ -204,9 +205,7 @@ def process_impl_within_lock(
204205
)
205206
assert params.get("commit") == commit.commitid
206207

207-
result: ProcessingResult = report_service.process_upload(
208-
commit, upload, compare_sha
209-
)
208+
result = report_service.process_upload(commit, upload, compare_sha)
210209
if result.error and result.error.is_retryable:
211210
if self._has_exceeded_max_attempts(self.max_retries):
212211
attempts = self.attempts
@@ -232,7 +231,10 @@ def process_impl_within_lock(
232231
"result": result.as_dict(),
233232
},
234233
)
235-
self.retry(max_retries=self.max_retries, countdown=30 * (2**self.request.retries))
234+
self.retry(
235+
max_retries=self.max_retries,
236+
countdown=30 * (2**self.request.retries),
237+
)
236238
result.update_upload(carriedforward=carriedforward)
237239
db_session.commit()
238240

@@ -270,9 +272,9 @@ def process_impl_within_lock(
270272
)
271273
raise
272274
finally:
273-
if "result" in locals() and result.bundle_report:
275+
if result is not None and result.bundle_report:
274276
result.bundle_report.cleanup()
275-
if "result" in locals() and result.previous_bundle_report:
277+
if result is not None and result.previous_bundle_report:
276278
result.previous_bundle_report.cleanup()
277279

278280
# Create task to save bundle measurements

apps/worker/tasks/tests/unit/test_bundle_analysis_processor_task.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def test_bundle_analysis_processor_task_error(
144144

145145
task = BundleAnalysisProcessorTask()
146146
retry = mocker.patch.object(task, "retry")
147+
task.request.retries = 0
147148

148149
result = task.run_impl(
149150
dbsession,
@@ -171,7 +172,7 @@ def test_bundle_analysis_processor_task_error(
171172

172173
assert commit.state == "error"
173174
assert upload.state == "error"
174-
retry.assert_called_once_with(countdown=30)
175+
retry.assert_called_once_with(max_retries=5, countdown=30)
175176

176177

177178
def test_bundle_analysis_processor_task_general_error(
@@ -390,6 +391,7 @@ def test_bundle_analysis_process_upload_rate_limit_error(
390391

391392
task = BundleAnalysisProcessorTask()
392393
retry = mocker.patch.object(task, "retry")
394+
task.request.retries = 0
393395

394396
ingest = mocker.patch("shared.bundle_analysis.BundleAnalysisReport.ingest")
395397
ingest.side_effect = PutRequestRateLimitError()
@@ -422,7 +424,7 @@ def test_bundle_analysis_process_upload_rate_limit_error(
422424

423425
assert commit.state == "error"
424426
assert upload.state == "error"
425-
retry.assert_called_once_with(countdown=30)
427+
retry.assert_called_once_with(max_retries=5, countdown=30)
426428

427429

428430
def test_bundle_analysis_process_associate_no_parent_commit_id(

0 commit comments

Comments
 (0)