mirror of
https://github.com/sstent/FitTrack_GarminSync.git
synced 2026-01-25 08:35:23 +00:00
5.0 KiB
5.0 KiB
004-single-sync-job: Simplify Sync Job Management with Progress Tracking
Goal
To simplify the synchronization job management by allowing only one sync job to run at a time, and to provide progress tracking for this single active sync job via a polling API. This aligns with the system's single-user nature while offering better user feedback.
Motivation
The current system is designed for a single user. While the previous iteration aimed to remove all job management complexity, the need for user feedback on ongoing sync operations necessitates a simplified progress tracking mechanism. This approach avoids a full-fledged job queue and ID system but still provides visibility into the single active sync.
Proposed Changes
1. Reintroduction of a Simplified Sync Job Model
- Define
SyncJobModel: Reintroduce aPydanticmodel forSyncJob(e.g., in a newbackend/src/sync_status.pyfile or similar). This model will represent the state of the single active sync job and will include fields such as:status: str(e.g., "idle", "in_progress", "completed", "failed")progress: float(0.0 to 1.0)start_time: datetimeend_time: Optional[datetime]error_message: Optional[str]job_type: Optional[str](e.g., "activities", "health", "workouts")
2. Introduce CurrentSyncJobManager
- Create
CurrentSyncJobManager: Implement a singleton class or a global object (e.g., inbackend/src/sync_manager.py) to manage the state of theSyncJob. This manager will hold the singleSyncJobinstance and provide methods for its lifecycle.start_sync(job_type: str) -> SyncJob: Initializes a newSyncJobwith "in_progress" status and the given type. Sets thestart_time. Returns theSyncJobinstance.update_progress(progress: float, details: Optional[str] = None): Updates theprogressand optionallydetailsof the currentSyncJob.complete_sync(): Sets thestatusto "completed" andend_timefor the currentSyncJob.fail_sync(error_message: str): Sets thestatusto "failed",error_message, andend_timefor the currentSyncJob.get_current_sync_status() -> SyncJob: Returns the currentSyncJobinstance.is_sync_active() -> bool: ReturnsTrueif a sync is currently "in_progress",Falseotherwise.
3. Modification of Sync Service Functions
- The
_in_backgroundfunctions inbackend/src/services/garmin_health_service.py,backend/src/services/garmin_activity_service.py, andbackend/src/services/garmin_workout_service.pywill be modified:- They will no longer accept a
job_idparameter. - Instead, they will receive the
SyncJobinstance (or a reference to theCurrentSyncJobManager) for the current operation. - They will use the
CurrentSyncJobManager'supdate_progress,complete_sync, andfail_syncmethods to report their status.
- They will no longer accept a
4. Modification of API Endpoints (backend/src/api/garmin_sync.py)
- Remove
job_storeandSyncJobimports from the old system. - Import
CurrentSyncJobManagerandSyncJobfrom the new system. - Remove
/status/{job_id}endpoint: This endpoint is no longer needed as there are no individual job IDs to query. - Modify sync initiation endpoints (
/garmin/activities,/garmin/workouts,/garmin/health):- Before initiating a sync, check
CurrentSyncJobManager.is_sync_active(). IfTrue, return an error response (e.g.,409 Conflict) indicating that a sync is already in progress. - Call
CurrentSyncJobManager.start_sync(job_type="...")to initialize the sync job. - Pass the
SyncJobinstance (or a reference to theCurrentSyncJobManager) to the background task. - Change the
response_modelto return the initialSyncJobstatus or a simple success message.
- Before initiating a sync, check
- Add New API Endpoint for Status Polling:
GET /garmin/sync/status:- Response Model:
SyncJob - Description: Returns the current status of the single active sync job. If no sync is active, it returns an "idle" status
SyncJob.
- Response Model:
5. Update Dependencies and Main Application
backend/src/dependencies.py: Remove any references to the oldjob_storeandSyncStatusService.backend/src/main.py: Ensure theCurrentSyncJobManageris properly initialized and accessible (e.g., as a global or via dependency injection if preferred).
Impact
- Progress Tracking: Users can now poll a dedicated API endpoint to get real-time progress updates for the single active sync job.
- Clearer State: The system's state regarding sync operations is more transparent.
- Controlled Concurrency: Only one sync operation can run at a time, preventing resource contention in a single-user environment.
- Simplified API: No need to manage multiple job IDs. The status endpoint always refers to the current operation.
- Minimal Overhead: Avoids the complexity of a full-blown job queue and distributed job management system, suitable for a single-user application.