diff --git a/packit_service/worker/handlers/abstract.py b/packit_service/worker/handlers/abstract.py index 2c31f9735..ce89e6b4a 100644 --- a/packit_service/worker/handlers/abstract.py +++ b/packit_service/worker/handlers/abstract.py @@ -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 diff --git a/tests/unit/test_check_rate_limit_remaining.py b/tests/unit/test_check_rate_limit_remaining.py index 243614744..f33c36222 100644 --- a/tests/unit/test_check_rate_limit_remaining.py +++ b/tests/unit/test_check_rate_limit_remaining.py @@ -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