Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle analysis handle rate limit error #455

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion services/bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from shared.bundle_analysis.storage import get_bucket_name
from shared.reports.enums import UploadState
from shared.storage import get_appropriate_storage_service
from shared.storage.exceptions import FileNotInStorageError
from shared.storage.exceptions import FileNotInStorageError, PutRequestRateLimitError
from shared.torngit.base import TorngitBaseAdapter
from shared.torngit.exceptions import TorngitClientError
from shared.yaml import UserYaml
Expand Down Expand Up @@ -162,6 +162,24 @@ def process_upload(self, commit: Commit, upload: Upload) -> ProcessingResult:
is_retryable=True,
),
)
except PutRequestRateLimitError as e:
plugin_name = getattr(e, "bundle_analysis_plugin_name", "unknown")
sentry_metrics.incr(
"bundle_analysis_upload",
tags={
"result": "rate_limit_error",
"plugin_name": plugin_name,
},
)
return ProcessingResult(
upload=upload,
commit=commit,
error=ProcessingError(
code="rate_limit_error",
params={"location": upload.storage_path},
is_retryable=True,
),
)
except Exception as e:
# Metrics to count number of parsing errors of bundle files by plugins
plugin_name = getattr(e, "bundle_analysis_plugin_name", "unknown")
Expand Down
66 changes: 66 additions & 0 deletions tasks/tests/unit/test_bundle_analysis_processor_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from redis.exceptions import LockError
from shared.bundle_analysis.storage import get_bucket_name
from shared.storage.exceptions import PutRequestRateLimitError

from database.models import CommitReport
from database.tests.factories import CommitFactory, UploadFactory
Expand Down Expand Up @@ -298,3 +299,68 @@ def test_bundle_analysis_processor_task_locked(

assert upload.state == "started"
retry.assert_called_once_with(countdown=ANY, max_retries=5)


def test_bundle_analysis_process_upload_rate_limit_error(
mocker,
mock_configuration,
dbsession,
mock_storage,
mock_redis,
celery_app,
):
storage_path = (
f"v1/repos/testing/ed1bdd67-8fd2-4cdb-ac9e-39b99e4a3892/bundle_report.sqlite"
)
mock_storage.write_file(get_bucket_name(), storage_path, "test-content")

mocker.patch.object(BundleAnalysisProcessorTask, "app", celery_app)

commit = CommitFactory.create(state="pending")
dbsession.add(commit)
dbsession.flush()

commit_report = CommitReport(commit_id=commit.id_)
dbsession.add(commit_report)
dbsession.flush()

upload = UploadFactory.create(storage_path=storage_path, report=commit_report)
dbsession.add(upload)
dbsession.flush()

task = BundleAnalysisProcessorTask()
retry = mocker.patch.object(task, "retry")

ingest = mocker.patch("shared.bundle_analysis.BundleAnalysisReport.ingest")
ingest.side_effect = PutRequestRateLimitError()

result = task.run_impl(
dbsession,
{"results": [{"previous": "result"}]},
repoid=commit.repoid,
commitid=commit.commitid,
commit_yaml={},
params={
"upload_pk": upload.id_,
"commit": commit.commitid,
},
)
assert result == {
"results": [
{"previous": "result"},
{
"error": {
"code": "rate_limit_error",
"params": {
"location": "v1/repos/testing/ed1bdd67-8fd2-4cdb-ac9e-39b99e4a3892/bundle_report.sqlite"
},
},
"session_id": None,
"upload_id": upload.id_,
},
],
}

assert commit.state == "error"
assert upload.state == "error"
retry.assert_called_once_with(countdown=20, max_retries=5)
Loading