Skip to content
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
14 changes: 13 additions & 1 deletion packit_service/worker/handlers/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,24 @@ def check_rate_limit_remaining(self) -> None:
logger.warning("No current task found, skipping rate limit check.")
return
try:
# project could be both upstream and downstream
# it depends on the handler
if not (project := self.project):
raise ValueError("There is no project associated with the task")
except (ValueError, OgrException, PackitConfigException) as ex:
logger.warning(f"Failed to get project for rate limit check: {ex}")
return
remaining = project.service.get_rate_limit_remaining()
try:
remaining = project.service.get_rate_limit_remaining(
namespace=project.namespace, repo=project.repo
)
except Exception as ex:
# Safely get namespace and repo for logging, in case project is a mock
namespace = getattr(project, "namespace", "unknown")
repo = getattr(project, "repo", "unknown")
instance = f" ({namespace}/{repo})"
logger.debug(f"Failed to get rate limit for {project.service}{instance}: {ex}")
return
if remaining and remaining < RATE_LIMIT_THRESHOLD:
# Check if the task is already running from the rate-limited queue
# by checking the routing_key from delivery_info
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_check_rate_limit_remaining.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ def test_check_rate_limit_remaining_high_rate_limit(handler, mock_celery_app_wit

def test_check_rate_limit_remaining_low_rate_limit_reschedule(handler, monkeypatch):
"""Test that method reschedules task when rate limit is low"""
mock_service = flexmock(get_rate_limit_remaining=lambda: RATE_LIMIT_THRESHOLD - 50)
mock_project = flexmock(service=mock_service)
mock_service = flexmock(
get_rate_limit_remaining=lambda namespace=None, repo=None: RATE_LIMIT_THRESHOLD - 50
)
mock_project = flexmock(service=mock_service, namespace="test", repo="repo")
handler._project = mock_project

from packit_service.worker.handlers import abstract
Expand Down
Loading