Merge pull request #855 from tcely/patch-12

Reduce the amount of time the database is locked
This commit is contained in:
meeb 2025-03-18 02:40:15 +11:00 committed by GitHub
commit 497bb0a6c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 44 deletions

View File

@ -254,7 +254,7 @@ def media_post_save(sender, instance, created, **kwargs):
verbose_name = _('Downloading metadata for "{}"')
download_media_metadata(
str(instance.pk),
priority=10,
priority=20,
verbose_name=verbose_name.format(instance.pk),
remove_existing_tasks=True
)

View File

@ -181,7 +181,6 @@ def cleanup_removed_media(source, videos):
@background(schedule=300, remove_existing_tasks=True)
@atomic(durable=True)
def index_source_task(source_id):
'''
Indexes media available from a Source object.
@ -206,6 +205,7 @@ def index_source_task(source_id):
source.save()
log.info(f'Found {len(videos)} media items for source: {source}')
fields = lambda f, m: m.get_metadata_field(f)
with atomic(durable=True):
for video in videos:
# Create or update each video as a Media object
key = video.get(source.key_field, None)
@ -226,6 +226,9 @@ def index_source_task(source_id):
try:
with atomic():
media.save()
except IntegrityError as e:
log.error(f'Index media failed: {source} / {media} with "{e}"')
else:
log.debug(f'Indexed media: {source} / {media}')
# log the new media instances
new_media_instance = (
@ -239,13 +242,12 @@ def index_source_task(source_id):
verbose_name = _('Downloading metadata for "{}"')
download_media_metadata(
str(media.pk),
priority=9,
priority=20,
verbose_name=verbose_name.format(media.pk),
)
except IntegrityError as e:
log.error(f'Index media failed: {source} / {media} with "{e}"')
# Tack on a cleanup of old completed tasks
cleanup_completed_tasks()
with atomic(durable=True):
# Tack on a cleanup of old media
cleanup_old_media()
if source.delete_removed_media: