Skip to content

armgit / ente

Public Imported from GitHub
0

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

update

Prateek Sunala3951e2e8774
diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dart
index ec7f0e8f82..74264feaf2 100644
--- a/mobile/apps/photos/lib/main.dart
+++ b/mobile/apps/photos/lib/main.dart
@@ -188,9 +188,9 @@ Future<void> _runMinimally(String taskId, TimeLogger tlog) async {
NetworkClient.instance.getDio(),
packageInfo,
);
- // Initialize ComputeController early in background run so health listeners
- // and checks begin before MLService wiring.
- computeController;
+ // Initialize early so thermal/battery listeners can warm up while the
+ // rest of background services are being initialized.
+ final controller = computeController;
_logger.info("(for debugging) CollectionsService init $tlog");
await CollectionsService.instance.init(prefs);
@@ -227,8 +227,18 @@ Future<void> _runMinimally(String taskId, TimeLogger tlog) async {
if (flagService.enableMLInBackground) {
await MLService.instance.init();
await PersonService.init(entityService, MLDataDB.instance, prefs);
- // Trigger ML via ComputeControlEvent from current controller state.
- computeController.emitCurrentComputeControlEvent();
+ final canRunML = controller.requestCompute(ml: true);
+ if (!canRunML) {
+ _logger.info(
+ "[BG TASK] skipping ML, compute requirements not satisfied",
+ );
+ } else {
+ try {
+ await MLService.instance.runAllML(force: true);
+ } finally {
+ controller.releaseCompute(ml: true);
+ }
+ }
}
_logger.info("[BG TASK] smart albums sync");
await smartAlbumsService.syncSmartAlbums();
diff --git a/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart b/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart
index 38ae6f8b11..c78a463f0f 100644
--- a/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/compute_controller.dart
@@ -55,12 +55,6 @@ class ComputeController {
bool get isDeviceHealthy => _isDeviceHealthy;
- /// Replays the latest compute eligibility state to listeners.
- /// Useful when listeners are attached after initial health checks completed.
- void emitCurrentComputeControlEvent() {
- _fireControlEvent();
- }
-
void _setDeviceHealth(bool healthy) {
if (_isDeviceHealthy == healthy) return;
_isDeviceHealthy = healthy;
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index 7d50a98034..0a38ec2ab4 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -116,7 +116,11 @@ class MLService {
"MLController allowed running ML, faces indexing starting",
);
}
- unawaited(runAllML());
+ // Background start is driven manually from _runMinimally to avoid
+ // duplicate runAllML invocations in the same cycle.
+ if (!isProcessBg) {
+ unawaited(runAllML());
+ }
} else {
_logger.info(
"MLController stopped running ML, faces indexing will be paused (unless it's fetching embeddings)",
@@ -170,6 +174,11 @@ class MLService {
}
Future<void> runAllML({bool force = false}) async {
+ if (_isRunningML) {
+ _logger.info("runAllML called while already running, skipping");
+ return;
+ }
+ bool computeAcquiredInternally = false;
try {
final MLMode mode = isOfflineMode ? MLMode.offline : MLMode.online;
final mlDataDB = _dbForMode(mode);
@@ -177,7 +186,10 @@ class MLService {
_mlControllerStatus = true;
}
if (!_canRunMLFunction(function: "AllML") && !force) return;
- if (!force && !computeController.requestCompute(ml: true)) return;
+ if (!force) {
+ if (!computeController.requestCompute(ml: true)) return;
+ computeAcquiredInternally = true;
+ }
_isRunningML = true;
await sync();
if (_hasModeChanged(mode)) {
@@ -229,8 +241,10 @@ class MLService {
} finally {
_logger.info("ML finished running");
_isRunningML = false;
- if (!isProcessBg) {
+ if (computeAcquiredInternally) {
computeController.releaseCompute(ml: true);
+ }
+ if (!isProcessBg) {
VideoPreviewService.instance.queueFiles();
}
}