[tasks] Finish task based on timestamp, not now

This commit is contained in:
2025-03-07 08:59:37 -05:00
parent b660e47bc2
commit 42699f84d2
2 changed files with 9 additions and 8 deletions

View File

@ -1140,9 +1140,9 @@ class Scrobble(TimeStampedModel):
if existing_locations := location.in_proximity(named=True): if existing_locations := location.in_proximity(named=True):
existing_location = existing_locations.first() existing_location = existing_locations.first()
ts = int(pendulum.now().timestamp()) ts = int(pendulum.now().timestamp())
scrobble.log[ scrobble.log[ts] = (
ts f"Location {location.id} too close to this scrobble"
] = f"Location {location.id} too close to this scrobble" )
scrobble.save(update_fields=["log"]) scrobble.save(update_fields=["log"])
logger.info( logger.info(
f"[scrobbling] finished - found existing named location", f"[scrobbling] finished - found existing named location",
@ -1216,8 +1216,8 @@ class Scrobble(TimeStampedModel):
send_notifications_for_scrobble(scrobble.id) send_notifications_for_scrobble(scrobble.id)
return scrobble return scrobble
def stop(self, force_finish=False) -> None: def stop(self, timestamp=None, force_finish=False) -> None:
self.stop_timestamp = timezone.now() self.stop_timestamp = timestamp or timezone.now()
self.played_to_completion = True self.played_to_completion = True
self.in_progress = False self.in_progress = False

View File

@ -1,3 +1,4 @@
from datetime import datetime
import logging import logging
import re import re
from typing import Optional from typing import Optional
@ -326,7 +327,7 @@ def manual_scrobble_from_url(
def todoist_scrobble_task_finish( def todoist_scrobble_task_finish(
todoist_task: dict, user_id: int todoist_task: dict, user_id: int, timestamp: datetime
) -> Optional[Scrobble]: ) -> Optional[Scrobble]:
scrobble = Scrobble.objects.filter( scrobble = Scrobble.objects.filter(
user_id=user_id, user_id=user_id,
@ -341,7 +342,7 @@ def todoist_scrobble_task_finish(
) )
return return
scrobble.stop(force_finish=True) scrobble.stop(timestamp=timestamp, force_finish=True)
return scrobble return scrobble
@ -447,7 +448,7 @@ def todoist_scrobble_task(
"todoist_id": todoist_task["todoist_id"], "todoist_id": todoist_task["todoist_id"],
}, },
) )
return todoist_scrobble_task_finish(todoist_task, user_id) return todoist_scrobble_task_finish(todoist_task, user_id, timestamp)
# Default to create new scrobble "if not in_progress_scrobble and in_progress_in_todoist" # Default to create new scrobble "if not in_progress_scrobble and in_progress_in_todoist"
# TODO Should use updated_at from TOdoist, but parsing isn't working # TODO Should use updated_at from TOdoist, but parsing isn't working