Fix plugin sync issues (#4930)

# What this PR does
- Fix incorrect response for error message on sync
- Remove sleep delay from sync (natural latency provides enough delay)

## Which issue(s) this PR closes

Related to [issue link here]

<!--
*Note*: If you want the issue to be auto-closed once the PR is merged,
change "Related to" to "Closes" in the line above.
If you have more than one GitHub issue that this PR closes, be sure to
preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
This commit is contained in:
Michael Derynck 2024-08-26 13:12:18 -06:00 committed by GitHub
parent 0ac7c40671
commit 9655a90f23
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 9 deletions

View file

@ -1,6 +1,4 @@
import logging
import math
from time import sleep
from celery.utils.log import get_task_logger
from django.utils import timezone
@ -38,8 +36,7 @@ def sync_organizations_v2(org_ids=None):
logger.debug(f"Found {len(active_instance_ids)} active instances")
organization_qs = organization_qs.filter(stack_id__in=active_instance_ids)
orgs_per_second = math.ceil(len(organization_qs) / SYNC_PERIOD.seconds)
logger.info(f"Syncing {len(organization_qs)} organizations @ {orgs_per_second} per 1s pause")
logger.info(f"Syncing {len(organization_qs)} organizations")
for idx, org in enumerate(organization_qs):
if GrafanaAPIClient.validate_grafana_token_format(org.api_token):
client = GrafanaAPIClient(api_url=org.grafana_url, api_token=org.api_token)
@ -48,9 +45,8 @@ def sync_organizations_v2(org_ids=None):
logger.error(
f"Failed to request sync stack_slug={org.stack_slug} status_code={status['status_code']} url={status['url']} message={status['message']}"
)
if idx % orgs_per_second == 0:
logger.info(f"Sleep 1s after {idx + 1} organizations processed")
sleep(1)
if idx % 1000 == 0:
logger.info(f"{idx + 1} organizations processed")
else:
logger.info(f"Skipping stack_slug={org.stack_slug}, api_token format is invalid or not set")
else:

View file

@ -1,5 +1,5 @@
import logging
from dataclasses import asdict
from dataclasses import asdict, is_dataclass
from django.conf import settings
from rest_framework import status
@ -49,6 +49,9 @@ class SyncV2View(APIView):
try:
self.do_sync(request)
except SyncException as e:
return Response(data=asdict(e.error_data), status=status.HTTP_400_BAD_REQUEST)
return Response(
data=asdict(e.error_data) if is_dataclass(e.error_data) else e.error_data,
status=status.HTTP_400_BAD_REQUEST,
)
return Response(status=status.HTTP_200_OK)