fix: do not run multiple engines on cold startup (#22518)

fix: do not run multiple engines on app startup

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
shenlong 2025-10-03 20:05:56 +05:30 committed by Chaoscontrol
parent 71f5a49195
commit f219e52143
2 changed files with 43 additions and 32 deletions

View file

@ -1,8 +1,11 @@
package app.alextran.immich package app.alextran.immich
import android.app.Application import android.app.Application
import android.os.Handler
import android.os.Looper
import androidx.work.Configuration import androidx.work.Configuration
import androidx.work.WorkManager import androidx.work.WorkManager
import app.alextran.immich.background.BackgroundEngineLock
import app.alextran.immich.background.BackgroundWorkerApiImpl import app.alextran.immich.background.BackgroundWorkerApiImpl
class ImmichApp : Application() { class ImmichApp : Application() {
@ -17,6 +20,11 @@ class ImmichApp : Application() {
// As a workaround, we also run a backup check when initializing the application // As a workaround, we also run a backup check when initializing the application
ContentObserverWorker.startBackupWorker(context = this, delayMilliseconds = 0) ContentObserverWorker.startBackupWorker(context = this, delayMilliseconds = 0)
BackgroundWorkerApiImpl.enqueueBackgroundWorker(this) Handler(Looper.getMainLooper()).postDelayed({
// We can only check the engine count and not the status of the lock here,
// as the previous start might have been killed without unlocking.
if (BackgroundEngineLock.connectEngines > 0) return@postDelayed
BackgroundWorkerApiImpl.enqueueBackgroundWorker(this)
}, 5000)
} }
} }

View file

@ -8,43 +8,46 @@ import java.util.concurrent.atomic.AtomicInteger
private const val TAG = "BackgroundEngineLock" private const val TAG = "BackgroundEngineLock"
class BackgroundEngineLock(context: Context) : BackgroundWorkerLockApi, FlutterPlugin { class BackgroundEngineLock(context: Context) : BackgroundWorkerLockApi, FlutterPlugin {
private val ctx: Context = context.applicationContext private val ctx: Context = context.applicationContext
companion object { companion object {
private var engineCount = AtomicInteger(0) private var engineCount = AtomicInteger(0)
private fun checkAndEnforceBackgroundLock(ctx: Context) { val connectEngines: Int
// work manager task is running while the main app is opened, cancel the worker get() = engineCount.get()
if (BackgroundWorkerPreferences(ctx).isLocked() &&
engineCount.get() > 1 && private fun checkAndEnforceBackgroundLock(ctx: Context) {
BackgroundWorkerApiImpl.isBackgroundWorkerRunning() // work manager task is running while the main app is opened, cancel the worker
) { if (BackgroundWorkerPreferences(ctx).isLocked() &&
Log.i(TAG, "Background worker is locked, cancelling the background worker") connectEngines > 1 &&
BackgroundWorkerApiImpl.cancelBackgroundWorker(ctx) BackgroundWorkerApiImpl.isBackgroundWorkerRunning()
} ) {
} Log.i(TAG, "Background worker is locked, cancelling the background worker")
BackgroundWorkerApiImpl.cancelBackgroundWorker(ctx)
}
} }
}
override fun lock() { override fun lock() {
BackgroundWorkerPreferences(ctx).setLocked(true) BackgroundWorkerPreferences(ctx).setLocked(true)
checkAndEnforceBackgroundLock(ctx) checkAndEnforceBackgroundLock(ctx)
Log.i(TAG, "Background worker is locked") Log.i(TAG, "Background worker is locked")
} }
override fun unlock() { override fun unlock() {
BackgroundWorkerPreferences(ctx).setLocked(false) BackgroundWorkerPreferences(ctx).setLocked(false)
Log.i(TAG, "Background worker is unlocked") Log.i(TAG, "Background worker is unlocked")
} }
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
checkAndEnforceBackgroundLock(binding.applicationContext) checkAndEnforceBackgroundLock(binding.applicationContext)
engineCount.incrementAndGet() engineCount.incrementAndGet()
Log.i(TAG, "Flutter engine attached. Attached Engines count: $engineCount") Log.i(TAG, "Flutter engine attached. Attached Engines count: $engineCount")
} }
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
engineCount.decrementAndGet() engineCount.decrementAndGet()
Log.i(TAG, "Flutter engine detached. Attached Engines count: $engineCount") Log.i(TAG, "Flutter engine detached. Attached Engines count: $engineCount")
} }
} }