Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Merge main

laurenspriem129fd62af2b2
diff --cc mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index c317ba0aa9,0491131bed..6c551c5c13
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@@ -86,8 -86,7 +89,8 @@@ class MLService
/// Only call this function once at app startup, after that you can directly call [runAllML]
Future<void> init() async {
if (_isInitialized) {
- unawaited(_maybePredownloadLocalModels());
+ _schedulePredownloadLocalModels();
+ scheduleStartupOwnedRemoteHydration();
return;
}
_logger.info("init called");
@@@ -138,8 -137,7 +141,8 @@@
_syncMlControllerStatusForBg();
_isInitialized = true;
- unawaited(_maybePredownloadLocalModels());
+ _schedulePredownloadLocalModels();
+ scheduleStartupOwnedRemoteHydration();
_logger.info('init done');
}
@@@ -176,143 -174,16 +179,153 @@@
}
}
+ void scheduleStartupOwnedRemoteHydration() {
+ if (_hasScheduledStartupOwnedRemoteHydration ||
+ isProcessBg ||
+ !hasGrantedMLConsent ||
+ isOfflineMode ||
+ !localSettings.remoteFetchEnabled) {
+ return;
+ }
+ _hasScheduledStartupOwnedRemoteHydration = true;
+ unawaited(_runStartupOwnedRemoteHydration());
+ }
+
+ Future<void> _runStartupOwnedRemoteHydration() async {
+ if (!hasGrantedMLConsent || isOfflineMode) {
+ return;
+ }
+ try {
+ await fileDataService.syncFDStatus();
+ } catch (e, s) {
+ _logger.warning(
+ "Skipping startup-owned remote ML hydration because FD status refresh failed",
+ e,
+ s,
+ );
+ return;
+ }
+ try {
+ await hydrateRemoteEmbeddingsForOwnedFiles(
+ reason: "startup",
+ skipHydrationIfCandidateFileCountAtMost:
+ _kStartupOwnedRemoteHydrationMissingFileThreshold,
+ );
+ } catch (e, s) {
+ _logger.warning(
+ "Skipping startup-owned remote ML hydration because owned hydration failed",
+ e,
+ s,
+ );
+ }
+ }
+
+ Future<void> hydrateRemoteEmbeddingsForOwnedFiles({
+ required String reason,
+ int? skipHydrationIfCandidateFileCountAtMost,
+ }) async {
+ if (isProcessBg ||
+ isOfflineMode ||
+ !hasGrantedMLConsent ||
+ !localSettings.remoteFetchEnabled) {
+ return;
+ }
+ final existing = _ownedRemoteHydrationFuture;
+ if (existing != null) {
+ _logger.info(
+ "Owned remote ML hydration already running, joining existing run ($reason)",
+ );
+ return existing;
+ }
+ final future = _runOwnedRemoteHydrationSafely(
+ reason: reason,
+ skipHydrationIfCandidateFileCountAtMost:
+ skipHydrationIfCandidateFileCountAtMost,
+ );
+ _ownedRemoteHydrationFuture = future;
+ try {
+ await future;
+ } finally {
+ if (identical(_ownedRemoteHydrationFuture, future)) {
+ _ownedRemoteHydrationFuture = null;
+ }
+ }
+ }
+
+ Future<void> _runOwnedRemoteHydrationSafely({
+ required String reason,
+ int? skipHydrationIfCandidateFileCountAtMost,
+ }) async {
+ try {
+ await _hydrateRemoteEmbeddingsForOwnedFilesInternal(
+ reason: reason,
+ skipHydrationIfCandidateFileCountAtMost:
+ skipHydrationIfCandidateFileCountAtMost,
+ );
+ } catch (e, s) {
+ _logger.warning("Owned remote ML hydration ($reason) failed", e, s);
+ }
+ }
+
+ Future<void> _hydrateRemoteEmbeddingsForOwnedFilesInternal({
+ required String reason,
+ int? skipHydrationIfCandidateFileCountAtMost,
+ }) async {
+ final summary = await hydrateOwnedRemoteMLData(
+ mlDataDB: MLDataDB.instance,
+ skipHydrationIfCandidateFileCountAtMost:
+ skipHydrationIfCandidateFileCountAtMost,
+ );
+ if (summary.candidateFiles == 0) {
+ _logger.info(
+ "Skipping owned remote ML hydration ($reason): no owned files need remote hydration",
+ );
+ return;
+ }
+ if (summary.skippedDueToCandidateThreshold) {
+ _logger.info(
+ "Skipping owned remote ML hydration ($reason): only ${summary.candidateFiles} "
+ "owned files are missing remote ML data (threshold: > "
+ "$skipHydrationIfCandidateFileCountAtMost)",
+ );
+ return;
+ }
+ _logger.info(
+ "Owned remote ML hydration ($reason) finished for ${summary.candidateFiles} files "
+ "(faces hydrated: ${summary.hydratedFaces}, clip hydrated: ${summary.hydratedClips}, "
+ "still pending local ML: ${summary.remainingLocalMl})",
+ );
+ }
+
+ Future<void> _waitForOwnedRemoteHydrationIfRunning() async {
+ final existing = _ownedRemoteHydrationFuture;
+ if (existing == null) {
+ return;
+ }
+ _logger.info(
+ "Waiting for owned remote ML hydration to finish before indexing",
+ );
+ try {
+ await existing;
+ } catch (e, s) {
+ _logger.warning(
+ "Owned remote ML hydration failed while indexing was waiting, continuing",
+ e,
+ s,
+ );
+ }
+ }
+
+ void _schedulePredownloadLocalModels() {
+ if (isProcessBg || _predownloadLocalModelsTimer?.isActive == true) {
+ return;
+ }
+ _predownloadLocalModelsTimer = Timer(_kPredownloadLocalModelsDelay, () {
+ _predownloadLocalModelsTimer = null;
+ unawaited(_maybePredownloadLocalModels());
+ });
+ }
+
bool canFetch() {
if (localSettings.isMLLocalIndexingEnabled) return true;
if (lastRemoteFetch == null) {