many changes
Traceability Validation / Check Requirement Traces (push) Failing after 1m18s
🏗️ Build and Test JellyTau / Build APK and Run Tests (push) Has been cancelled

This commit is contained in:
2026-02-14 00:09:47 +01:00
parent 6d1c618a3a
commit e3797f32ca
74 changed files with 6718 additions and 771 deletions
+30 -219
View File
@@ -1,13 +1,12 @@
// Sync service - processes queued mutations when connectivity is restored
// Sync service - manages offline mutation queueing
//
// This service handles:
// - Queueing mutations (favorites, playback progress) when offline
// - Processing queued mutations when connectivity is restored
// - Retry with exponential backoff for failed operations
// Simplified service that coordinates with the Rust backend.
// The Rust backend handles sync queue persistence and processing logic.
// This service provides a thin TypeScript API for queuing mutations.
//
// TRACES: UR-002, UR-017, UR-025 | DR-014
import { invoke } from "@tauri-apps/api/core";
import { get } from "svelte/store";
import { isServerReachable, connectivity } from "$lib/stores/connectivity";
import { auth } from "$lib/stores/auth";
// Types matching Rust structs
@@ -25,62 +24,24 @@ export interface SyncQueueItem {
export type SyncOperation =
| "mark_played"
| "mark_unplayed"
| "mark_favorite"
| "unmark_favorite"
| "update_progress"
| "report_playback_start"
| "report_playback_stopped";
// Maximum retries before giving up on an operation
const MAX_RETRIES = 5;
// Delay between sync attempts (exponential backoff)
const BASE_RETRY_DELAY_MS = 1000;
// Batch size for processing queue
const BATCH_SIZE = 10;
/**
* Simplified sync service - handles offline mutation queueing
*
* The Rust backend maintains the sync queue in SQLite and is responsible
* for processing queued items. This service provides a TypeScript API
* for queueing and managing sync operations.
*/
class SyncService {
private processing = false;
private unsubscribeConnectivity: (() => void) | null = null;
/**
* Start the sync service - listens for connectivity changes
*/
start(): void {
if (this.unsubscribeConnectivity) {
return; // Already started
}
console.log("[SyncService] Starting...");
// Listen for connectivity changes
this.unsubscribeConnectivity = isServerReachable.subscribe((reachable) => {
if (reachable && !this.processing) {
console.log("[SyncService] Server became reachable, processing queue...");
this.processQueue();
}
});
// Process queue on startup if online
if (get(isServerReachable)) {
this.processQueue();
}
}
/**
* Stop the sync service
*/
stop(): void {
if (this.unsubscribeConnectivity) {
this.unsubscribeConnectivity();
this.unsubscribeConnectivity = null;
}
}
/**
* Queue a mutation for sync to server
*
* TRACES: UR-017, UR-025 | DR-014
*/
async queueMutation(
operation: SyncOperation,
@@ -100,20 +61,15 @@ class SyncService {
});
console.log(`[SyncService] Queued ${operation} for item ${itemId}, id: ${id}`);
// Try to process immediately if online
if (get(isServerReachable) && !this.processing) {
this.processQueue();
}
return id;
}
/**
* Queue a favorite toggle
* Also updates local state immediately
*/
async queueFavorite(itemId: string, isFavorite: boolean): Promise<number> {
// Also update local state
// Update local state first
await invoke("storage_toggle_favorite", {
userId: auth.getUserId(),
itemId,
@@ -128,12 +84,13 @@ class SyncService {
/**
* Queue playback progress update
* Also updates local state immediately
*/
async queuePlaybackProgress(
itemId: string,
positionTicks: number
): Promise<number> {
// Also update local state
// Update local state first
await invoke("storage_update_playback_progress", {
userId: auth.getUserId(),
itemId,
@@ -145,9 +102,10 @@ class SyncService {
/**
* Queue mark as played
* Also updates local state immediately
*/
async queueMarkPlayed(itemId: string): Promise<number> {
// Also update local state
// Update local state first
await invoke("storage_mark_played", {
userId: auth.getUserId(),
itemId,
@@ -169,167 +127,18 @@ class SyncService {
}
/**
* Process the sync queue
* Get pending sync items (for debugging/monitoring)
*/
async processQueue(): Promise<void> {
if (this.processing) {
console.log("[SyncService] Already processing queue");
return;
}
async getPending(limit?: number): Promise<SyncQueueItem[]> {
const userId = auth.getUserId();
if (!userId) {
console.log("[SyncService] Not authenticated, skipping queue processing");
return;
return [];
}
if (!get(isServerReachable)) {
console.log("[SyncService] Server not reachable, skipping queue processing");
return;
}
this.processing = true;
console.log("[SyncService] Processing sync queue...");
try {
// Get pending items
const items = await invoke<SyncQueueItem[]>("sync_get_pending", {
userId,
limit: BATCH_SIZE,
});
if (items.length === 0) {
console.log("[SyncService] No pending items in queue");
return;
}
console.log(`[SyncService] Processing ${items.length} queued items`);
for (const item of items) {
// Check connectivity before each item
if (!get(isServerReachable)) {
console.log("[SyncService] Lost connectivity, stopping queue processing");
break;
}
// Check if we've exceeded retries
if (item.retryCount >= MAX_RETRIES) {
console.warn(
`[SyncService] Item ${item.id} exceeded max retries, marking as failed`
);
await invoke("sync_mark_failed", {
id: item.id,
error: "Exceeded maximum retry attempts",
});
continue;
}
await this.processItem(item);
}
// Check if there are more items to process
const remaining = await this.getPendingCount();
if (remaining > 0 && get(isServerReachable)) {
// Process next batch after a short delay
setTimeout(() => this.processQueue(), 100);
}
} catch (error) {
console.error("[SyncService] Error processing queue:", error);
} finally {
this.processing = false;
}
}
/**
* Process a single sync queue item
*/
private async processItem(item: SyncQueueItem): Promise<void> {
console.log(`[SyncService] Processing item ${item.id}: ${item.operation}`);
try {
// Mark as processing
await invoke("sync_mark_processing", { id: item.id });
// Get repository for API calls
const repo = auth.getRepository();
// Execute the operation
switch (item.operation) {
case "mark_favorite":
if (item.itemId) {
await repo.markFavorite(item.itemId);
}
break;
case "unmark_favorite":
if (item.itemId) {
await repo.unmarkFavorite(item.itemId);
}
break;
case "update_progress":
if (item.itemId && item.payload) {
const payload = JSON.parse(item.payload);
await repo.reportPlaybackProgress(item.itemId, payload.positionTicks);
}
break;
case "mark_played":
if (item.itemId) {
// Jellyfin doesn't have a direct "mark played" endpoint,
// we report playback stopped at 100%
const itemData = await repo.getItem(item.itemId);
if (itemData.runTimeTicks) {
await repo.reportPlaybackStopped(item.itemId, itemData.runTimeTicks);
}
}
break;
case "report_playback_start":
if (item.itemId && item.payload) {
const payload = JSON.parse(item.payload);
await repo.reportPlaybackStart(item.itemId, payload.positionTicks);
}
break;
case "report_playback_stopped":
if (item.itemId && item.payload) {
const payload = JSON.parse(item.payload);
await repo.reportPlaybackStopped(item.itemId, payload.positionTicks);
}
break;
default:
console.warn(`[SyncService] Unknown operation: ${item.operation}`);
}
// Mark as completed
await invoke("sync_mark_completed", { id: item.id });
// Also mark local data as synced
if (item.itemId) {
await invoke("storage_mark_synced", {
userId: item.userId,
itemId: item.itemId,
});
}
console.log(`[SyncService] Successfully processed item ${item.id}`);
} catch (error) {
console.error(`[SyncService] Failed to process item ${item.id}:`, error);
// Calculate retry delay with exponential backoff
const retryDelay = BASE_RETRY_DELAY_MS * Math.pow(2, item.retryCount);
// Mark as failed
await invoke("sync_mark_failed", {
id: item.id,
error: error instanceof Error ? error.message : String(error),
});
// Wait before continuing (gives server time to recover if overloaded)
await new Promise((resolve) => setTimeout(resolve, Math.min(retryDelay, 10000)));
}
return invoke<SyncQueueItem[]>("sync_get_pending", {
userId,
limit,
});
}
/**
@@ -343,6 +152,8 @@ class SyncService {
/**
* Clear all sync operations for the current user (called during logout)
*
* TRACES: UR-017 | DR-014
*/
async clearUser(): Promise<void> {
const userId = auth.getUserId();