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

chore: handle NoConfirugedAppsAvailable error #758

Merged
merged 1 commit into from
Oct 3, 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
27 changes: 23 additions & 4 deletions tasks/sync_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from app import celery_app
from database.models import Commit, Pull, Repository, Test
from helpers.exceptions import RepositoryWithoutValidBotError
from helpers.exceptions import NoConfiguredAppsAvailable, RepositoryWithoutValidBotError
from helpers.github_installation import get_installation_name_for_owner_for_task
from helpers.metrics import metrics
from rollouts import SYNC_PULL_USE_MERGE_COMMIT_SHA
Expand Down Expand Up @@ -109,6 +109,7 @@ def run_impl_within_lock(
commit_updates_done = {"merged_count": 0, "soft_deleted_count": 0}
repository = db_session.query(Repository).filter_by(repoid=repoid).first()
assert repository
extra_info = dict(pullid=pullid, repoid=repoid)
try:
installation_name_to_use = get_installation_name_for_owner_for_task(
self.name, repository.owner
Expand All @@ -119,7 +120,7 @@ def run_impl_within_lock(
except RepositoryWithoutValidBotError:
log.warning(
"Could not sync pull because there is no valid bot found for that repo",
extra=dict(pullid=pullid, repoid=repoid),
extra=extra_info,
exc_info=True,
)
return {
Expand All @@ -128,6 +129,24 @@ def run_impl_within_lock(
"pull_updated": False,
"reason": "no_bot",
}
except NoConfiguredAppsAvailable as err:
log.error(
"Could not sync pull because there are no configured apps available",
extra={
**extra_info,
"suspended_app_count": err.suspended_count,
"rate_limited_count": err.rate_limited_count,
},
)
if err.rate_limited_count > 0:
log.info("Apps are rate limited. Retrying in 60s", extra=extra_info)
self.retry(max_retries=1, countdown=60)
return {
"notifier_called": False,
"commit_updates_done": {"merged_count": 0, "soft_deleted_count": 0},
"pull_updated": False,
"reason": "no_configured_apps_available",
}
context = OwnerContext(
owner_onboarding_date=repository.owner.createstamp,
owner_plan=repository.owner.plan,
Expand All @@ -146,7 +165,7 @@ def run_impl_within_lock(
if pull is None:
log.info(
"Not syncing pull since we can't find it in the database nor in the provider",
extra=dict(pullid=pullid, repoid=repoid),
extra=extra_info,
)
return {
"notifier_called": False,
Expand All @@ -157,7 +176,7 @@ def run_impl_within_lock(
if enriched_pull.provider_pull is None:
log.info(
"Not syncing pull since we can't find it in the provider. There is nothing to sync",
extra=dict(pullid=pullid, repoid=repoid),
extra=extra_info,
)
return {
"notifier_called": False,
Expand Down
40 changes: 39 additions & 1 deletion tasks/tests/unit/test_sync_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from pathlib import Path

import pytest
from celery.exceptions import Retry
from mock.mock import MagicMock
from redis.exceptions import LockError
from shared.reports.types import Change
from shared.torngit.exceptions import TorngitClientError

from database.tests.factories import CommitFactory, PullFactory, RepositoryFactory
from database.tests.factories.reports import TestFactory
from helpers.exceptions import RepositoryWithoutValidBotError
from helpers.exceptions import NoConfiguredAppsAvailable, RepositoryWithoutValidBotError
from services.repository import EnrichedPull
from services.yaml import UserYaml
from tasks.sync_pull import PullSyncTask
Expand Down Expand Up @@ -306,6 +307,43 @@ def test_call_pullsync_no_bot(self, dbsession, mock_redis, mocker):
"reason": "no_bot",
}

def test_call_pullsync_no_apps_available_rate_limit(
self, dbsession, mock_redis, mocker
):
task = PullSyncTask()
pull = PullFactory.create(state="open")
dbsession.add(pull)
dbsession.flush()
mocker.patch(
"tasks.sync_pull.get_repo_provider_service",
side_effect=NoConfiguredAppsAvailable(
apps_count=1, rate_limited_count=1, suspended_count=0
),
)
with pytest.raises(Retry):
task.run_impl(dbsession, repoid=pull.repoid, pullid=pull.pullid)

def test_call_pullsync_no_apps_available_suspended(
self, dbsession, mock_redis, mocker
):
task = PullSyncTask()
pull = PullFactory.create(state="open")
dbsession.add(pull)
dbsession.flush()
mocker.patch(
"tasks.sync_pull.get_repo_provider_service",
side_effect=NoConfiguredAppsAvailable(
apps_count=1, rate_limited_count=0, suspended_count=1
),
)
res = task.run_impl(dbsession, repoid=pull.repoid, pullid=pull.pullid)
assert res == {
"commit_updates_done": {"merged_count": 0, "soft_deleted_count": 0},
"notifier_called": False,
"pull_updated": False,
"reason": "no_configured_apps_available",
}

def test_call_pullsync_no_permissions_get_compare(
self, dbsession, mock_redis, mocker, mock_repo_provider, mock_storage
):
Expand Down
Loading