diff --git a/mobile/lib/constants/constants.dart b/mobile/lib/constants/constants.dart index 1f39640832..b084effd91 100644 --- a/mobile/lib/constants/constants.dart +++ b/mobile/lib/constants/constants.dart @@ -5,9 +5,6 @@ const double downloadFailed = -2; // Number of log entries to retain on app start const int kLogTruncateLimit = 250; -// Duration for background sync -const Duration kBackgroundSyncDuration = Duration(minutes: 1); - // Hash batch limits const int kBatchHashFileLimit = 128; const int kBatchHashSizeLimit = 1024 * 1024 * 1024; // 1GB diff --git a/mobile/lib/domain/services/sync_stream.service.dart b/mobile/lib/domain/services/sync_stream.service.dart index 8169a5477c..e710fba1b4 100644 --- a/mobile/lib/domain/services/sync_stream.service.dart +++ b/mobile/lib/domain/services/sync_stream.service.dart @@ -63,17 +63,25 @@ class SyncStreamService { } } await _syncApiRepository.ack(acks.values.toList()); + _logger.info("$types events processed"); } catch (error, stack) { _logger.warning("Error handling sync events", error, stack); } streamCompleter.completeOnce(); }, - onError: (_) => streamCompleter.completeOnce(), + onError: (error) { + _logger.warning("Error in sync stream for $types", error); + streamCompleter.completeOnce(); + }, // onDone is required to be called in cases where the stream is empty - onDone: () => shouldSkipOnDone ? null : streamCompleter.completeOnce, + onDone: () { + _logger.info("$types stream done"); + if (!shouldSkipOnDone) { + streamCompleter.completeOnce(); + } + }, ); - streamCompleter.future.whenComplete(subscription.cancel); - return await streamCompleter.future; + return await streamCompleter.future.whenComplete(subscription.cancel); } Future syncUsers() => _syncEvent([SyncRequestType.usersV1]); diff --git a/mobile/lib/providers/background_sync.provider.dart b/mobile/lib/providers/background_sync.provider.dart index 1c5dbc38b9..75efebddd8 100644 --- a/mobile/lib/providers/background_sync.provider.dart +++ b/mobile/lib/providers/background_sync.provider.dart @@ -1,7 +1,6 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; -import 'package:immich_mobile/constants/constants.dart'; import 'package:immich_mobile/utils/background_sync.dart'; final backgroundSyncProvider = Provider( - (ref) => BackgroundSyncManager(duration: kBackgroundSyncDuration), + (ref) => BackgroundSyncManager(), ); diff --git a/mobile/lib/utils/background_sync.dart b/mobile/lib/utils/background_sync.dart index ead1c640ab..bfedbf2601 100644 --- a/mobile/lib/utils/background_sync.dart +++ b/mobile/lib/utils/background_sync.dart @@ -5,43 +5,20 @@ import 'dart:async'; import 'package:async/async.dart'; import 'package:immich_mobile/providers/infrastructure/sync_stream.provider.dart'; import 'package:immich_mobile/utils/isolate.dart'; -import 'package:logging/logging.dart'; class BackgroundSyncManager { - final Logger _logger = Logger('BackgroundSyncManager'); - Timer? _timer; - final Duration _duration; - // This allows us to keep synching in the background while allowing ondemand syncs + // This prevents multiple syncs from running at the same time final _userSyncCache = AsyncCache.ephemeral(); final _partnerSyncCache = AsyncCache.ephemeral(); - BackgroundSyncManager({required Duration duration}) : _duration = duration; - - Timer _createTimer() { - return Timer.periodic(_duration, (timer) async { - _logger.info('Background sync started'); - await syncUsers(); - await syncPartners(); - _logger.info('Background sync completed'); - }); - } - - void start() { - _logger.info('Starting Background sync'); - _timer ??= _createTimer(); - } - - void stop() { - _logger.info('Stopping Background sync'); - _timer?.cancel(); - _timer = null; - } + BackgroundSyncManager(); Future syncUsers() => _userSyncCache.fetch( - () async => runInIsolate( + () => runInIsolate( (ref) => ref.read(syncStreamServiceProvider).syncUsers(), ), ); + Future syncPartners() => _partnerSyncCache.fetch( () async => runInIsolate( (ref) => ref.read(syncStreamServiceProvider).syncPartners(),