feat: Implement single sync job management and progress tracking

This commit is contained in:
2025-10-11 18:36:19 -07:00
parent 3819e4f5e2
commit 723ca04aa8
51 changed files with 1625 additions and 596 deletions

View File

@@ -2,9 +2,13 @@ import logging
import uuid
from typing import Any, Dict, Optional
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
from ..jobs import job_store
from ..services.garmin_client_service import GarminClientService
logger = logging.getLogger(__name__)
@@ -13,10 +17,11 @@ logger = logging.getLogger(__name__)
GARMIN_RETRY_STRATEGY = retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type(Exception), # Broad exception for now, refine later
reraise=True
retry=retry_if_exception_type(Exception), # Broad exception for now, refine later
reraise=True,
)
class GarminWorkoutService:
def __init__(self, garmin_client_service: GarminClientService):
self.garmin_client_service = garmin_client_service
@@ -27,8 +32,11 @@ class GarminWorkoutService:
# Get workout from CentralDB
from ..config import settings
from .central_db_service import CentralDBService
central_db = CentralDBService(base_url=settings.CENTRAL_DB_URL)
workout = await central_db.get_workout_by_id(workout_id) # Assuming this method exists
workout = await central_db.get_workout_by_id(
workout_id
) # Assuming this method exists
if not workout:
logger.error(f"Workout with ID {workout_id} not found in CentralDB.")
@@ -42,7 +50,7 @@ class GarminWorkoutService:
f"Simulating upload of workout {workout.name} (ID: {workout_id}) "
"to Garmin Connect."
)
garmin_workout_id = f"GARMIN_WORKOUT_{workout_id}" # Mock ID
garmin_workout_id = f"GARMIN_WORKOUT_{workout_id}" # Mock ID
# Here we would update the workout in CentralDB with the garmin_workout_id
# await central_db.update_workout(
@@ -68,34 +76,21 @@ class GarminWorkoutService:
async def upload_workout_in_background(
self,
job_id: str,
user_id: int,
current_sync_job_manager,
workout_id: uuid.UUID,
):
try:
job_store.update_job(job_id, status="in_progress", progress=0.0)
uploaded_workout = await self.upload_workout(workout_id)
if uploaded_workout:
job_store.update_job(
job_id,
status="completed",
progress=1.0,
details={
"uploaded_workout_id": str(uploaded_workout.id),
"garmin_workout_id": uploaded_workout.garmin_workout_id
}
)
await current_sync_job_manager.complete_sync()
else:
job_store.update_job(
job_id,
status="failed",
progress=1.0,
await current_sync_job_manager.fail_sync(
error_message=f"Failed to upload workout {workout_id}"
)
except Exception as e:
logger.error(
f"Error during workout upload for job {job_id}: {e}", exc_info=True
)
job_store.update_job(job_id, status="failed", progress=1.0, error_message=str(e))
logger.error(f"Error during workout upload: {e}", exc_info=True)
await current_sync_job_manager.fail_sync(error_message=str(e))