Skip to content

Commit

Permalink
Various fixes to the handle-image-data command
Browse files Browse the repository at this point in the history
closes pulp#1573
closes pulp#1575

* taught command to skip content that has annotations/labels populated
* got rid of Paginator. Paginator is lazy, when items are updated, it
  will make an new query for the page, and skip objects.
  • Loading branch information
ipanova authored and lubosmj committed Apr 2, 2024
1 parent 32d6696 commit f377a9c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGES/1573.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed hande-image-data command to skip content that has labels/annotations already populated.
1 change: 1 addition & 0 deletions CHANGES/1575.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed handle-image-data command to update all entries in one run.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

from django.core.exceptions import ObjectDoesNotExist
from django.core.management import BaseCommand
from django.core.paginator import Paginator

from pulp_container.app.models import Manifest

from pulp_container.constants import MEDIA_TYPE

PAGE_CHUNK_SIZE = 1000


class Command(BaseCommand):
"""
Expand All @@ -34,42 +31,45 @@ class Command(BaseCommand):
def handle(self, *args, **options):
manifests_updated_count = 0

manifests = Manifest.objects.exclude(
manifests = Manifest.objects.filter(labels={}, annotations={})
manifests = manifests.exclude(
media_type__in=[MEDIA_TYPE.MANIFEST_LIST, MEDIA_TYPE.INDEX_OCI, MEDIA_TYPE.MANIFEST_V1]
).order_by("pulp_id")
)
manifests_updated_count += self.update_manifests(manifests)

manifest_lists = Manifest.objects.filter(
media_type__in=[MEDIA_TYPE.MANIFEST_LIST, MEDIA_TYPE.INDEX_OCI]
).order_by("pulp_id")
media_type__in=[MEDIA_TYPE.MANIFEST_LIST, MEDIA_TYPE.INDEX_OCI], annotations={}
)
manifests_updated_count += self.update_manifests(manifest_lists)

self.stdout.write(
self.style.SUCCESS("Successfully handled %d manifests." % manifests_updated_count)
self.style.SUCCESS("Successfully updated %d manifests." % manifests_updated_count)
)

def update_manifests(self, manifests_qs):
manifests_updated_count = 0

paginator = Paginator(manifests_qs, PAGE_CHUNK_SIZE)
for page_num in paginator.page_range:
manifests_to_update = []

page = paginator.page(page_num)
for manifest in page.object_list:
# suppress non-existing/already migrated artifacts and corrupted JSON files
with suppress(ObjectDoesNotExist, JSONDecodeError):
has_metadata = manifest.init_metadata()
if has_metadata:
manifests_to_update.append(manifest)

if manifests_to_update:
manifests_to_update = []
for manifest in manifests_qs.iterator():
# suppress non-existing/already migrated artifacts and corrupted JSON files
with suppress(ObjectDoesNotExist, JSONDecodeError):
has_metadata = manifest.init_metadata()
if has_metadata:
manifests_to_update.append(manifest)

if len(manifests_to_update) > 1000:
fields_to_update = ["annotations", "labels", "is_bootable", "is_flatpak"]
manifests_qs.model.objects.bulk_update(
manifests_to_update,
fields_to_update,
)

manifests_updated_count += len(manifests_to_update)
manifests_to_update.clear()
if manifests_to_update:
fields_to_update = ["annotations", "labels", "is_bootable", "is_flatpak"]
manifests_qs.model.objects.bulk_update(
manifests_to_update,
fields_to_update,
)
manifests_updated_count += len(manifests_to_update)

return manifests_updated_count

0 comments on commit f377a9c

Please sign in to comment.