Skip to content

Commit

Permalink
Show progress when running command
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Oct 28, 2023
1 parent 69472dc commit 7db537a
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions packagedb/management/commands/update_maven_download_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import logging
import sys

from django.db import transaction

import requests

from minecode.management.commands import VerboseCommand
Expand Down Expand Up @@ -68,19 +70,23 @@ def handle(self, *args, **options):
unsaved_packages = []
processed_packages_count = 0
for i, package in enumerate(MemorySavingQuerysetIterator(maven_packages)):
if i % 2000 and unsaved_packages:
Package.objects.bulk_update(
objs=unsaved_packages,
fields=[
'download_url',
]
)
if not i % 1000:
logger.info(f'Checked {i:,} / {maven_packages_count:,} Maven Package download URLs')
if not i % 2000 and unsaved_packages:
with transaction.atomic():
Package.objects.bulk_update(
objs=unsaved_packages,
fields=[
'download_url',
]
)
processed_packages_count += unsaved_packages.count()
unsaved_packages = []
logger.info(f'Updated {processed_packages_count:,} Maven Packages')
# If the package's download URL is not valid, then we update it
if not check_download_url(package.download_url):
package_url = PackageURL(
type=package.type,
namespace=package.namespace,
name=package.name,
version=package.version,
Expand All @@ -104,17 +110,19 @@ def handle(self, *args, **options):
logger.info(f'Error: cannot generate a valid download_url for package {package.package_uid}')

if unsaved_packages:
Package.objects.bulk_update(
objs=unsaved_packages,
fields=[
'download_url',
]
)
with transaction.atomic():
Package.objects.bulk_update(
objs=unsaved_packages,
fields=[
'download_url',
]
)
processed_packages_count += unsaved_packages.count()
unsaved_packages = []
logger.info(f'Updated {processed_packages_count:,} Maven Packages')

if packages_to_delete:
pks = [p.pk for p in packages_to_delete]
Package.objects.filter(pk__in=pks).delete()
with transaction.atomic():
Package.objects.filter(pk__in=pks).delete()
logger.info(f'Deleted {pks.count():,} Maven Package duplicates')

0 comments on commit 7db537a

Please sign in to comment.