Add a command to remove zombie scrobbles

This commit is contained in:
2023-04-06 13:53:22 -04:00
parent 8042c726b0
commit e487f50683
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,29 @@
from django.core.management.base import BaseCommand, no_translations
from vrobbler.apps.scrobbles.utils import delete_zombie_scrobbles
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--delete",
action="store_true",
help="Delete poll instead of closing it",
)
def handle(self, *args, **options):
dry_run = True
if options["delete"]:
dry_run = False
scrobbles_found = delete_zombie_scrobbles(dry_run)
if not scrobbles_found:
print(f"No zombie scrobbles found to delete")
return
if not dry_run:
print(f"Deleted {scrobbles_found} zombie scrobbles")
return
print(
f"Found {scrobbles_found} zombie scrobbles, use --delete to remove them"
)

View File

@ -1,4 +1,5 @@
import logging
from datetime import timedelta
from urllib.parse import unquote
from dateutil.parser import ParserError, parse
@ -212,3 +213,29 @@ def import_lastfm_for_all_users():
)
continue
process_lastfm_import.delay(lfm_import.id)
def delete_zombie_scrobbles(dry_run=True):
"""Look for any scrobble over a day old that is not paused and still in progress and delete it"""
Scrobble = apps.get_model("scrobbles", "Scrobble")
now = timezone.now()
three_days_ago = now - timedelta(days=3)
# TODO This should be part of a custom manager
zombie_scrobbles = Scrobble.objects.filter(
timestamp__lte=three_days_ago,
is_paused=False,
played_to_completion=False,
)
zombies_found = zombie_scrobbles.count()
if not dry_run:
logger.info(f"Deleted {zombies_found} zombie scrobbles")
zombie_scrobbles.delete()
return zombies_found
logger.info(
f"Found {zombies_found} zombie scrobbles to delete, use dry_run=False to proceed"
)
return zombies_found