Skip to content

armgit / ente

Public Imported from GitHub
0

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

[photos] Let background processing wait for refresh

laurenspriemb90743061a61
diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt
index 6522e1e450..a3e29e0dc0 100644
--- a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt
+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt
@@ -44,6 +44,7 @@ internal object BackgroundRuntime {
private var isAllowed: () -> Boolean = { false }
private var isForeground = false
private var active: Run? = null
+ private var waiting: Run? = null
var createEngine: (Context) -> FlutterEngine = { FlutterEngine(it) }
var findCallback: (Long) -> FlutterCallbackInformation? =
FlutterCallbackInformation::lookupCallbackInformation
@@ -121,10 +122,12 @@ internal object BackgroundRuntime {
fun foreground() {
isForeground = true
+ waiting?.let { finishWaiting(it, "stopped", "foreground") }
active?.let { stop(it, "foreground") }
}
fun requestStop(result: MethodChannel.Result? = null) {
+ waiting?.let { finishWaiting(it, "stopped", "requested") }
val run = active
if (run == null) {
result?.success(null)
@@ -159,6 +162,11 @@ internal object BackgroundRuntime {
return
}
if (!enabled) requestStop()
+ waiting
+ ?.takeIf { run ->
+ configurations.none { it.identifier == run.configuration.identifier }
+ }
+ ?.let { finishWaiting(it, "skipped", "disabled") }
scheduler.execute {
try {
val preferences = app.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE)
@@ -191,6 +199,9 @@ internal object BackgroundRuntime {
}
val activeTask = FutureTask {
if (!enabled) requestStop()
+ waiting
+ ?.takeIf { it.configuration.identifier !in selected }
+ ?.let { finishWaiting(it, "skipped", "disabled") }
active?.configuration?.identifier
}
main.post(activeTask)
@@ -292,6 +303,20 @@ internal object BackgroundRuntime {
}
}
+ private fun isEnabled(identifier: String): Boolean {
+ val saved =
+ application
+ ?.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE)
+ ?.getString(CONFIGURATION, null)
+ ?.let(::JSONObject)
+ val tasks = saved?.optJSONArray("tasks") ?: JSONArray()
+ return saved?.optBoolean("enabled") == true &&
+ isAllowed() &&
+ (0 until tasks.length()).any {
+ tasks.getJSONObject(it).getString("identifier") == identifier
+ }
+ }
+
fun start(worker: BackgroundWorker, startedAt: Long, completion: (Boolean) -> Unit) {
onMain {
if (worker.isStopped) return@onMain
@@ -307,27 +332,40 @@ internal object BackgroundRuntime {
}
val enabled =
try {
- val saved =
- application
- ?.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE)
- ?.getString(CONFIGURATION, null)
- ?.let(::JSONObject)
- val tasks = saved?.optJSONArray("tasks") ?: JSONArray()
- saved?.optBoolean("enabled") == true &&
- isAllowed() &&
- (0 until tasks.length()).any {
- tasks.getJSONObject(it).getString("identifier") ==
- configuration.identifier
- }
+ isEnabled(configuration.identifier)
} catch (exception: Exception) {
report(configuration.identifier, "failed", "configuration", exception.message)
completion(false)
return@onMain
}
+ if (
+ enabled &&
+ !isForeground &&
+ waiting == null &&
+ configuration.kind == "processing" &&
+ active?.configuration?.kind == "refresh" &&
+ active?.retiring == false &&
+ active?.stopReason !in setOf("foreground", "requested", "system")
+ ) {
+ val run = Run(worker, configuration, startedAt, completion)
+ waiting = run
+ Log.i(TAG, "${configuration.identifier}: waiting for refresh")
+ configuration.runBudgetMs?.let { budget ->
+ val remaining = budget - elapsed(run)
+ if (remaining <= 0) {
+ finishWaiting(run, "skipped", "waitBudget")
+ } else {
+ run.budgetTimer =
+ Runnable { finishWaiting(run, "skipped", "waitBudget") }
+ .also { main.postDelayed(it, remaining) }
+ }
+ }
+ return@onMain
+ }
val skip =
when {
!enabled -> "disabled"
- active != null -> "busy"
+ active != null || waiting != null -> "busy"
isForeground -> "foreground"
else -> null
}
@@ -340,118 +378,164 @@ internal object BackgroundRuntime {
return@onMain
}
val run = Run(worker, configuration, startedAt, completion)
- active = run
- configuration.runBudgetMs?.let { budget ->
- run.budgetTimer =
- Runnable { if (active === run) stop(run, "budget") }
- .also {
- main.postDelayed(it, (budget - elapsed(run)).coerceAtLeast(0))
- }
- }
- try {
- val loader = FlutterInjector.instance().flutterLoader()
- loader.startInitialization(worker.applicationContext)
- loader.ensureInitializationCompleteAsync(worker.applicationContext, null, main) {
- if (active !== run || run.retiring) return@ensureInitializationCompleteAsync
- if (run.worker.isStopped) {
- retire(run, "stopped", "system")
- return@ensureInitializationCompleteAsync
- }
- run.stopReason?.let {
- retire(run, "stopped", it)
- return@ensureInitializationCompleteAsync
+ startRun(run)
+ }
+ }
+
+ private fun startRun(run: Run) {
+ val worker = run.worker
+ val configuration = run.configuration
+ active = run
+ configuration.runBudgetMs?.let { budget ->
+ run.budgetTimer =
+ Runnable { if (active === run) stop(run, "budget") }
+ .also {
+ main.postDelayed(it, (budget - elapsed(run)).coerceAtLeast(0))
}
- try {
- val callback =
- requireNotNull(findCallback(configuration.callbackHandle)) {
- "Background dispatcher is unavailable"
- }
- val engine = createEngine(worker.applicationContext)
- run.engine = engine
- val channel =
- MethodChannel(
- engine.dartExecutor.binaryMessenger,
- "io.ente.background/worker",
- )
- run.channel = channel
- channel.setMethodCallHandler { call, result ->
- if (active !== run || run.retiring) {
- result.success(null)
- } else
- when (call.method) {
- "ready" -> {
- if (run.ready) {
- result.error(
- "bootstrap",
- "Dispatcher already started",
- null,
- )
- } else {
- run.ready = true
- result.success(
- mapOf(
- "invocation" to run.invocation,
- "identifier" to configuration.identifier,
- "elapsedMs" to elapsed(run),
- "runBudgetMs" to configuration.runBudgetMs,
- "stopReason" to run.stopReason,
- )
+ }
+ try {
+ val loader = FlutterInjector.instance().flutterLoader()
+ loader.startInitialization(worker.applicationContext)
+ loader.ensureInitializationCompleteAsync(worker.applicationContext, null, main) {
+ if (active !== run || run.retiring) return@ensureInitializationCompleteAsync
+ if (run.worker.isStopped) {
+ retire(run, "stopped", "system")
+ return@ensureInitializationCompleteAsync
+ }
+ run.stopReason?.let {
+ retire(run, "stopped", it)
+ return@ensureInitializationCompleteAsync
+ }
+ try {
+ val callback =
+ requireNotNull(findCallback(configuration.callbackHandle)) {
+ "Background dispatcher is unavailable"
+ }
+ val engine = createEngine(worker.applicationContext)
+ run.engine = engine
+ val channel =
+ MethodChannel(
+ engine.dartExecutor.binaryMessenger,
+ "io.ente.background/worker",
+ )
+ run.channel = channel
+ channel.setMethodCallHandler { call, result ->
+ if (active !== run || run.retiring) {
+ result.success(null)
+ } else
+ when (call.method) {
+ "ready" -> {
+ if (run.ready) {
+ result.error(
+ "bootstrap",
+ "Dispatcher already started",
+ null,
+ )
+ } else {
+ run.ready = true
+ result.success(
+ mapOf(
+ "invocation" to run.invocation,
+ "identifier" to configuration.identifier,
+ "elapsedMs" to elapsed(run),
+ "runBudgetMs" to configuration.runBudgetMs,
+ "stopReason" to run.stopReason,
)
- }
+ )
}
- "complete" -> {
- val data = call.arguments as? Map<*, *>
- if (data?.get("invocation") != run.invocation) {
+ }
+ "complete" -> {
+ val data = call.arguments as? Map<*, *>
+ if (data?.get("invocation") != run.invocation) {
+ result.error(
+ "invocation",
+ "Stale background completion",
+ null,
+ )
+ } else {
+ val outcome = data["outcome"] as? String
+ if (
+ outcome !in
+ setOf(
+ "completed",
+ "skipped",
+ "stopped",
+ "failed",
+ )
+ ) {
result.error(
- "invocation",
- "Stale background completion",
+ "outcome",
+ "Unknown task outcome",
null,
)
} else {
- val outcome = data["outcome"] as? String
- if (
- outcome !in
- setOf(
- "completed",
- "skipped",
- "stopped",
- "failed",
- )
- ) {
- result.error(
- "outcome",
- "Unknown task outcome",
- null,
- )
- } else {
- result.success(null)
- retire(
- run,
- outcome!!,
- run.stopReason,
- data["error"] as? String,
- )
- }
+ result.success(null)
+ retire(
+ run,
+ outcome!!,
+ run.stopReason,
+ data["error"] as? String,
+ )
}
}
- else -> result.notImplemented()
}
- }
- engine.dartExecutor.executeDartCallback(
- DartExecutor.DartCallback(
- worker.applicationContext.assets,
- loader.findAppBundlePath(),
- callback,
- )
- )
- } catch (exception: Exception) {
- retire(run, "failed", "bootstrap", exception.message)
+ else -> result.notImplemented()
+ }
}
+ engine.dartExecutor.executeDartCallback(
+ DartExecutor.DartCallback(
+ worker.applicationContext.assets,
+ loader.findAppBundlePath(),
+ callback,
+ )
+ )
+ } catch (exception: Exception) {
+ retire(run, "failed", "bootstrap", exception.message)
+ }
+ }
+ } catch (exception: Exception) {
+ retire(run, "failed", "bootstrap", exception.message)
+ }
+ }
+
+ private fun finishWaiting(run: Run, outcome: String, reason: String) {
+ if (waiting !== run) return
+ waiting = null
+ run.retiring = true
+ run.budgetTimer?.let(main::removeCallbacks)
+ report(run.configuration.identifier, outcome, reason)
+ run.completion(outcome != "failed")
+ }
+
+ private fun startWaiting() {
+ if (active != null) return
+ val run = waiting ?: return
+ val reason =
+ try {
+ when {
+ run.worker.isStopped -> "system"
+ isForeground -> "foreground"
+ !isEnabled(run.configuration.identifier) -> "disabled"
+ run.configuration.runBudgetMs?.let { elapsed(run) >= it } == true ->
+ "waitBudget"
+ else -> null
}
} catch (exception: Exception) {
- retire(run, "failed", "bootstrap", exception.message)
+ finishWaiting(run, "failed", "configuration")
+ return
}
+ if (reason != null) {
+ finishWaiting(run, "skipped", reason)
+ return
}
+ waiting = null
+ run.budgetTimer?.let(main::removeCallbacks)
+ run.budgetTimer = null
+ Log.i(
+ TAG,
+ "${run.configuration.identifier}: refresh finished after ${elapsed(run)}ms waiting",
+ )
+ startRun(run)
}
private fun onMain(action: () -> Unit) {
@@ -460,6 +544,7 @@ internal object BackgroundRuntime {
fun nativeStop(worker: BackgroundWorker) {
main.post {
+ waiting?.takeIf { it.worker === worker }?.let { finishWaiting(it, "stopped", "system") }
active
?.takeIf { it.worker === worker }
?.let {
@@ -521,6 +606,9 @@ internal object BackgroundRuntime {
if (run.teardownFailure == null) {
active = null
retireUnselectedSchedule(run.configuration.identifier)
+ startWaiting()
+ } else {
+ waiting?.let { finishWaiting(it, "failed", "teardown") }
}
if (failure != null) {
report(run.configuration.identifier, "failed", "teardown", failure.message)
diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundWaitingTest.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundWaitingTest.kt
new file mode 100644
index 0000000000..7d8f42e217
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundWaitingTest.kt
@@ -0,0 +1,471 @@
+package io.ente.background
+
+import android.app.Application
+import android.os.Looper
+import androidx.work.Configuration
+import androidx.work.Data
+import androidx.work.ListenableWorker
+import androidx.work.testing.TestListenableWorkerBuilder
+import androidx.work.testing.WorkManagerTestInitHelper
+import io.flutter.FlutterInjector
+import io.flutter.embedding.engine.FlutterEngine
+import io.flutter.embedding.engine.dart.DartExecutor
+import io.flutter.embedding.engine.loader.FlutterLoader
+import io.flutter.plugin.common.BinaryMessenger
+import io.flutter.plugin.common.MethodCall
+import io.flutter.plugin.common.MethodChannel
+import io.flutter.plugin.common.StandardMethodCodec
+import io.flutter.view.FlutterCallbackInformation
+import java.nio.ByteBuffer
+import java.time.Duration
+import org.junit.After
+import org.junit.Assert.*
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentMatchers.*
+import org.mockito.Mockito.*
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.RuntimeEnvironment
+import org.robolectric.Shadows.shadowOf
+import org.robolectric.annotation.Config
+import org.robolectric.annotation.LooperMode
+import org.robolectric.util.ReflectionHelpers
+
+@RunWith(RobolectricTestRunner::class)
+@Config(sdk = [28])
+@LooperMode(LooperMode.Mode.PAUSED)
+class BackgroundWaitingTest {
+ private val app: Application
+ get() = RuntimeEnvironment.getApplication()
+
+ private val startups = ArrayDeque<Runnable>()
+ private val engines = ArrayList<Pair<FlutterEngine, Messenger>>()
+ private val main
+ get() = shadowOf(Looper.getMainLooper())
+
+ private fun policy(budget: Long? = null, grace: Long? = null) =
+ TaskConfiguration(
+ "test.refresh",
+ "refresh",
+ 900_000,
+ 86_400_000,
+ null,
+ false,
+ false,
+ false,
+ budget,
+ grace,
+ 1,
+ )
+
+ private fun configure(vararg policies: TaskConfiguration, enabled: Boolean = true) {
+ val result = Reply()
+ BackgroundRuntime.configure(
+ mapOf(
+ "enabled" to enabled,
+ "callbackHandle" to 1L,
+ "tasks" to
+ policies.map { policy ->
+ val data = org.json.JSONObject(policy.encode())
+ data.keys().asSequence().associateWith { key ->
+ data.get(key).takeUnless { it === org.json.JSONObject.NULL }
+ }
+ },
+ ),
+ result,
+ )
+ awaitReply(result)
+ }
+
+ private fun awaitReply(result: Reply) {
+ val deadline = System.nanoTime() + 10_000_000_000L
+ while (!result.done && System.nanoTime() < deadline) {
+ main.idle()
+ Thread.sleep(5)
+ }
+ assertTrue("Configuration did not finish", result.done)
+ assertNull(result.error)
+ }
+
+ private fun worker(policy: TaskConfiguration): BackgroundWorker =
+ TestListenableWorkerBuilder<BackgroundWorker>(app)
+ .setInputData(Data.Builder().putString("task", policy.encode()).build())
+ .build()
+
+ private fun bootstrap(): Messenger {
+ startups.removeFirst().run()
+ return engines.last().second
+ }
+
+ private fun complete(messenger: Messenger, ready: Map<*, *>, outcome: String = "completed") {
+ messenger.call("complete", mapOf("invocation" to ready["invocation"], "outcome" to outcome))
+ }
+
+ private var allowed = true
+ private val refresh = policy(budget = 60_000)
+ private val processing =
+ policy(budget = 30_000).copy(identifier = "test.processing", kind = "processing")
+
+ @Before
+ fun initialize() {
+ resetRuntime()
+ WorkManagerTestInitHelper.initializeTestWorkManager(app, Configuration.Builder().build())
+ BackgroundRuntime.install(app) { allowed }
+ val loader = mock(FlutterLoader::class.java)
+ doAnswer { invocation ->
+ startups.add(invocation.getArgument(3))
+ null
+ }
+ .`when`(loader)
+ .ensureInitializationCompleteAsync(any(), isNull(), any(), any())
+ `when`(loader.findAppBundlePath()).thenReturn("flutter_assets")
+ FlutterInjector.setInstance(FlutterInjector.Builder().setFlutterLoader(loader).build())
+ BackgroundRuntime.findCallback = { mock(FlutterCallbackInformation::class.java) }
+ BackgroundRuntime.createEngine = {
+ val engine = mock(FlutterEngine::class.java)
+ val executor = mock(DartExecutor::class.java)
+ val messenger = Messenger()
+ `when`(engine.dartExecutor).thenReturn(executor)
+ `when`(executor.binaryMessenger).thenReturn(messenger)
+ engines.add(engine to messenger)
+ engine
+ }
+
+ configure(refresh, processing)
+ }
+
+ @After
+ fun resetRuntime() {
+ ReflectionHelpers.getField<android.os.Handler>(BackgroundRuntime, "main")
+ .removeCallbacksAndMessages(null)
+ ReflectionHelpers.setField(BackgroundRuntime, "application", null)
+ ReflectionHelpers.setField(BackgroundRuntime, "active", null)
+ ReflectionHelpers.setField(BackgroundRuntime, "waiting", null)
+ ReflectionHelpers.setField(BackgroundRuntime, "isForeground", false)
+ FlutterInjector.reset()
+ }
+
+ @Test
+ fun processingCanWaitLongerThanThirtySeconds() {
+ val longRefresh = refresh.copy(runBudgetMs = 600_000)
+ val longProcessing = processing.copy(runBudgetMs = 480_000)
+ configure(longRefresh, longProcessing)
+ worker(longRefresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(longProcessing).startWork()
+ main.idleFor(Duration.ofMinutes(2))
+ assertFalse(pending.isDone)
+ assertTrue(channel.sent.isEmpty())
+ assertTrue(startups.isEmpty())
+ complete(channel, ready)
+ val next = bootstrap()
+ val nextReady = next.call("ready") as Map<*, *>
+ assertEquals(120_000L, nextReady["elapsedMs"])
+ complete(next, nextReady)
+ assertTrue(pending.isDone)
+ }
+
+ @Test
+ fun cancelledWaitTimerCannotFinishReplacement() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val original = worker(processing)
+ val cancelled = original.startWork()
+ main.idleFor(Duration.ofSeconds(5))
+ original.onStopped()
+ main.idle()
+ assertTrue(cancelled.isDone)
+ val replacement = worker(processing).startWork()
+ main.idleFor(Duration.ofSeconds(26))
+ assertFalse(replacement.isDone)
+ assertTrue(startups.isEmpty())
+ complete(channel, ready)
+ val next = bootstrap()
+ val nextReady = next.call("ready") as Map<*, *>
+ assertEquals(26_000L, nextReady["elapsedMs"])
+ complete(next, nextReady)
+ assertTrue(replacement.isDone)
+ }
+
+ @Test
+ fun exhaustedBudgetNeverBootstrapsWaitingProcessing() {
+ val expiredProcessing = processing.copy(runBudgetMs = 0)
+ configure(refresh, expiredProcessing)
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ assertEquals(ListenableWorker.Result.success(), worker(expiredProcessing).startWork().get())
+ assertTrue(channel.sent.isEmpty())
+ complete(channel, ready)
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun processingWaitsForEngineRetirementAndKeepsElapsedTime() {
+ val running = worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ assertFalse(pending.isDone)
+ assertTrue(startups.isEmpty())
+ assertTrue(channel.sent.isEmpty())
+ assertEquals(ListenableWorker.Result.success(), worker(processing).startWork().get())
+ assertEquals(ListenableWorker.Result.success(), worker(refresh).startWork().get())
+ main.idleFor(Duration.ofSeconds(5))
+ doAnswer {
+ assertTrue(startups.isEmpty())
+ assertFalse(pending.isDone)
+ null
+ }
+ .`when`(engines.single().first)
+ .destroy()
+ complete(channel, ready)
+ assertTrue(running.isDone)
+ assertFalse(pending.isDone)
+ val next = bootstrap()
+ val nextReady = next.call("ready") as Map<*, *>
+ assertEquals(processing.identifier, nextReady["identifier"])
+ assertEquals(5_000L, nextReady["elapsedMs"])
+ assertEquals(30_000L, nextReady["runBudgetMs"])
+ assertNull(nextReady["stopReason"])
+ main.idleFor(Duration.ofSeconds(24))
+ assertTrue(next.sent.isEmpty())
+ main.idleFor(Duration.ofSeconds(1))
+ assertEquals("budget", (next.sent.single().arguments as Map<*, *>)["reason"])
+ complete(next, nextReady)
+ assertEquals(ListenableWorker.Result.success(), pending.get())
+ complete(channel, ready)
+ main.idleFor(Duration.ofMinutes(1))
+ assertEquals(2, engines.size)
+ }
+
+ @Test
+ fun queuedBudgetExpiryLeavesRefreshRunning() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ main.idleFor(Duration.ofSeconds(30))
+ assertEquals(ListenableWorker.Result.success(), pending.get())
+ assertTrue(channel.sent.isEmpty())
+ verify(engines.single().first, never()).destroy()
+ complete(channel, ready)
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun stoppedQueuedWorkerCannotStartAfterRefresh() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pendingWorker = worker(processing)
+ val pending = pendingWorker.startWork()
+ pendingWorker.onStopped()
+ main.idle()
+ assertTrue(pending.isDone)
+ assertTrue(channel.sent.isEmpty())
+ complete(channel, ready)
+ pendingWorker.onStopped()
+ main.idleFor(Duration.ofMinutes(1))
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun foregroundRemovesWaitingBeforeRefreshRetires() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ BackgroundRuntime.foreground()
+ assertTrue(pending.isDone)
+ assertEquals("foreground", (channel.sent.single().arguments as Map<*, *>)["reason"])
+ complete(channel, ready)
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun explicitStopClearsWaitingAndWaitsForRefreshRetirement() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ val result = Reply()
+ BackgroundRuntime.requestStop(result)
+ assertTrue(pending.isDone)
+ assertFalse(result.done)
+ assertEquals(ListenableWorker.Result.success(), worker(processing).startWork().get())
+ complete(channel, ready)
+ assertTrue(result.done)
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun disablingBackendRemovesWaiting() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ configure(refresh, processing, enabled = false)
+ assertTrue(pending.isDone)
+ complete(channel, ready)
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun removingProcessingScheduleRemovesWaitingWithoutStoppingRefresh() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ configure(refresh)
+ assertTrue(pending.isDone)
+ assertTrue(channel.sent.isEmpty())
+ complete(channel, ready)
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun flagRevocationIsRecheckedAtHandoff() {
+ worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ allowed = false
+ complete(channel, ready)
+ assertTrue(pending.isDone)
+ assertTrue(startups.isEmpty())
+ }
+
+ @Test
+ fun refreshFailureStillAllowsProcessingAfterCleanup() {
+ val running = worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ complete(channel, ready, "failed")
+ assertEquals(ListenableWorker.Result.failure(), running.get())
+ verify(engines.single().first).destroy()
+ val next = bootstrap()
+ val nextReady = next.call("ready") as Map<*, *>
+ complete(next, nextReady)
+ assertEquals(ListenableWorker.Result.success(), pending.get())
+ }
+
+ @Test
+ fun teardownFailureDoesNotLaunchWaitingProcessing() {
+ val running = worker(refresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ val pending = worker(processing).startWork()
+ doThrow(IllegalStateException("Engine destruction failed"))
+ .`when`(engines.single().first)
+ .destroy()
+ complete(channel, ready)
+ assertEquals(ListenableWorker.Result.failure(), running.get())
+ assertEquals(ListenableWorker.Result.failure(), pending.get())
+ assertTrue(startups.isEmpty())
+ main.idleFor(Duration.ofMinutes(1))
+ assertEquals(1, engines.size)
+ }
+
+ @Test
+ fun refreshBudgetStopDoesNotPreventWaitingProcessingFromStarting() {
+ val shortRefresh = refresh.copy(runBudgetMs = 100)
+ configure(shortRefresh, processing)
+ worker(shortRefresh).startWork()
+ val channel = bootstrap()
+ val ready = channel.call("ready") as Map<*, *>
+ main.idleFor(Duration.ofMillis(100))
+ val pending = worker(processing).startWork()
+ assertFalse(pending.isDone)
+ assertEquals("budget", (channel.sent.single().arguments as Map<*, *>)["reason"])
+ complete(channel, ready)
+ val next = bootstrap()
+ val nextReady = next.call("ready") as Map<*, *>
+ assertNull(nextReady["stopReason"])
+ complete(next, nextReady)
+ assertTrue(pending.isDone)
+ }
+
+ @Test
+ fun processingCanWaitWhileRefreshIsBootstrapping() {
+ val running = worker(refresh).startWork()
+ val pending = worker(processing).startWork()
+ assertEquals(1, startups.size)
+ main.idleFor(Duration.ofSeconds(5))
+ BackgroundRuntime.findCallback = { null }
+ startups.removeFirst().run()
+ assertEquals(ListenableWorker.Result.failure(), running.get())
+ BackgroundRuntime.findCallback = { mock(FlutterCallbackInformation::class.java) }
+ val next = bootstrap()
+ val ready = next.call("ready") as Map<*, *>
+ assertEquals(5_000L, ready["elapsedMs"])
+ complete(next, ready)
+ assertTrue(pending.isDone)
+ }
+
+ private class Reply : MethodChannel.Result {
+ var done = false
+ var error: String? = null
+ var value: Any? = null
+
+ override fun success(result: Any?) {
+ assertFalse("Reply already completed", done)
+ value = result
+ done = true
+ }
+
+ override fun error(code: String, message: String?, details: Any?) {
+ assertFalse("Reply already completed", done)
+ error = "$code: $message"
+ done = true
+ }
+
+ override fun notImplemented() {
+ assertFalse("Reply already completed", done)
+ error = "notImplemented"
+ done = true
+ }
+ }
+
+ private class Messenger : BinaryMessenger {
+ private val codec = StandardMethodCodec.INSTANCE
+ private var handler: BinaryMessenger.BinaryMessageHandler? = null
+ var failHandlerRemoval = false
+ val sent = ArrayList<MethodCall>()
+
+ override fun send(channel: String, message: ByteBuffer?) = send(channel, message, null)
+
+ override fun send(
+ channel: String,
+ message: ByteBuffer?,
+ callback: BinaryMessenger.BinaryReply?,
+ ) {
+ message?.flip()
+ sent.add(codec.decodeMethodCall(requireNotNull(message)))
+ callback?.reply(codec.encodeSuccessEnvelope(null).also { it.flip() })
+ }
+
+ override fun setMessageHandler(
+ channel: String,
+ handler: BinaryMessenger.BinaryMessageHandler?,
+ ) {
+ check(handler != null || !failHandlerRemoval) { "Handler removal failed" }
+ this.handler = handler
+ }
+
+ fun call(method: String, arguments: Any? = null): Any? {
+ var reply: Any? = null
+ handler?.onMessage(
+ codec.encodeMethodCall(MethodCall(method, arguments)).also { it.flip() }
+ ) { encoded ->
+ encoded?.flip()
+ reply = codec.decodeEnvelope(requireNotNull(encoded))
+ }
+ return reply
+ }
+ }
+}
diff --git a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift
index d17fdff380..f22b918525 100644
--- a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift
+++ b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift
@@ -14,6 +14,7 @@ final class BackgroundRuntime: NSObject {
private let observers = NSHashTable<BackgroundManagerPlugin>.weakObjects()
private var registrations: [String: Bool] = [:]
private var active: Run?
+ private var waiting: Run?
private var isForeground = false
private var installed = false
private var reconciling = false
@@ -90,6 +91,7 @@ final class BackgroundRuntime: NSObject {
@objc private func foreground() {
isForeground = true
+ if let run = waiting { finishWaiting(run, outcome: "stopped", reason: "foreground") }
if let run = active { stop(run, reason: "foreground") }
}
@@ -98,6 +100,7 @@ final class BackgroundRuntime: NSObject {
}
func requestStop(result: FlutterResult? = nil) {
+ if let run = waiting { finishWaiting(run, outcome: "stopped", reason: "requested") }
guard let run = active else {
result?(nil)
return
@@ -152,6 +155,9 @@ final class BackgroundRuntime: NSObject {
submitted: configuration.submitted?.filter { identifiers.contains($0.identifier) })
try next.save()
configuration = next
+ if let run = waiting, !identifiers.contains(run.configuration.identifier) {
+ finishWaiting(run, outcome: "skipped", reason: "disabled")
+ }
if !enabled { requestStop() }
configurationResults.append(result)
reconcile()
@@ -260,10 +266,34 @@ final class BackgroundRuntime: NSObject {
identifier: task.identifier, outcome: "failed", reason: "schedule",
error: String(describing: error))
}
+ if isAllowed(), !isForeground, UIApplication.shared.applicationState == .background,
+ policy.kind == "processing", let occupant = active,
+ occupant.configuration.kind == "refresh", !occupant.retiring, waiting == nil,
+ !["foreground", "requested", "system"].contains(occupant.stopReason ?? "")
+ {
+ let run = Run(task: task, configuration: policy, startedAt: startedAt)
+ waiting = run
+ observeExpiration(run)
+ Self.logger.info("\(policy.identifier, privacy: .public): waiting for refresh")
+ if let budget = policy.runBudgetMs {
+ let remaining = budget - elapsed(run)
+ if remaining <= 0 {
+ finishWaiting(run, outcome: "skipped", reason: "waitBudget")
+ } else {
+ let timer = DispatchWorkItem { [weak run] in
+ guard let run else { return }
+ self.finishWaiting(run, outcome: "skipped", reason: "waitBudget")
+ }
+ run.budgetTimer = timer
+ DispatchQueue.main.asyncAfter(deadline: .now() + Double(remaining) / 1000, execute: timer)
+ }
+ }
+ return
+ }
let skip: String?
if !isAllowed() {
skip = "disabled"
- } else if active != nil {
+ } else if active != nil || waiting != nil {
skip = "busy"
} else if isForeground || UIApplication.shared.applicationState != .background {
skip = "foreground"
@@ -276,14 +306,27 @@ final class BackgroundRuntime: NSObject {
return
}
let run = Run(task: task, configuration: policy, startedAt: startedAt)
- active = run
- task.expirationHandler = { [weak run] in
+ start(run)
+ }
+
+ private func observeExpiration(_ run: Run) {
+ run.task.expirationHandler = { [weak run] in
DispatchQueue.main.async {
- guard let run, self.active === run else { return }
- self.stop(run, reason: "system")
- self.retire(run, outcome: "stopped", reason: "expired", success: false)
+ guard let run else { return }
+ if self.waiting === run {
+ self.finishWaiting(run, outcome: "stopped", reason: "expired", success: false)
+ } else if self.active === run {
+ self.stop(run, reason: "system")
+ self.retire(run, outcome: "stopped", reason: "expired", success: false)
+ }
}
}
+ }
+
+ private func start(_ run: Run) {
+ let policy = run.configuration
+ active = run
+ observeExpiration(run)
if let budget = policy.runBudgetMs {
let timer = DispatchWorkItem { [weak run] in
guard let run, self.active === run else { return }
@@ -353,6 +396,45 @@ final class BackgroundRuntime: NSObject {
registrant(engine)
}
+ private func finishWaiting(
+ _ run: Run, outcome: String, reason: String, success: Bool = true
+ ) {
+ guard waiting === run else { return }
+ waiting = nil
+ run.retiring = true
+ run.budgetTimer?.cancel()
+ run.task.expirationHandler = nil
+ run.task.setTaskCompleted(success: success)
+ report(identifier: run.configuration.identifier, outcome: outcome, reason: reason)
+ }
+
+ private func startWaiting() {
+ guard active == nil, let run = waiting else { return }
+ let reason: String?
+ if !configuration.enabled || !isAllowed()
+ || !configuration.tasks.contains(where: { $0.identifier == run.configuration.identifier })
+ {
+ reason = "disabled"
+ } else if isForeground || UIApplication.shared.applicationState != .background {
+ reason = "foreground"
+ } else if let budget = run.configuration.runBudgetMs, elapsed(run) >= budget {
+ reason = "waitBudget"
+ } else {
+ reason = nil
+ }
+ if let reason {
+ finishWaiting(run, outcome: "skipped", reason: reason)
+ return
+ }
+ waiting = nil
+ run.budgetTimer?.cancel()
+ run.budgetTimer = nil
+ Self.logger.info(
+ "\(run.configuration.identifier, privacy: .public): refresh finished after \(self.elapsed(run))ms waiting"
+ )
+ start(run)
+ }
+
private func elapsed(_ run: Run) -> Int64 {
Int64(max(0, ProcessInfo.processInfo.systemUptime - run.startedAt) * 1000)
}
@@ -394,6 +476,7 @@ final class BackgroundRuntime: NSObject {
run.task.setTaskCompleted(
success: success ?? (terminal != "failed" && terminal != "forcedTeardown"))
active = nil
+ startWaiting()
if terminal != "completed" {
report(
identifier: run.configuration.identifier, outcome: terminal, reason: reason, error: error)