High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
fix: do not run multiple engines on cold startup (#22518)
shenlong27665801e9ee
diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.ktindex 5a3b0e1f3d..4474c63e09 100644--- a/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.kt+++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.kt@@ -1,8 +1,11 @@package app.alextran.immichimport android.app.Application+import android.os.Handler+import android.os.Looperimport androidx.work.Configurationimport androidx.work.WorkManager+import app.alextran.immich.background.BackgroundEngineLockimport app.alextran.immich.background.BackgroundWorkerApiImplclass ImmichApp : Application() {@@ -17,6 +20,11 @@ class ImmichApp : Application() {// As a workaround, we also run a backup check when initializing the applicationContentObserverWorker.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)}}diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.ktindex d8afe32b5c..504267a4e5 100644--- a/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.kt+++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.kt@@ -8,43 +8,46 @@ import java.util.concurrent.atomic.AtomicIntegerprivate const val TAG = "BackgroundEngineLock"class BackgroundEngineLock(context: Context) : BackgroundWorkerLockApi, FlutterPlugin {- private val ctx: Context = context.applicationContext-- companion object {-- private var engineCount = AtomicInteger(0)-- private fun checkAndEnforceBackgroundLock(ctx: Context) {- // work manager task is running while the main app is opened, cancel the worker- if (BackgroundWorkerPreferences(ctx).isLocked() &&- engineCount.get() > 1 &&- BackgroundWorkerApiImpl.isBackgroundWorkerRunning()- ) {- Log.i(TAG, "Background worker is locked, cancelling the background worker")- BackgroundWorkerApiImpl.cancelBackgroundWorker(ctx)- }- }- }+ private val ctx: Context = context.applicationContext- override fun lock() {- BackgroundWorkerPreferences(ctx).setLocked(true)- checkAndEnforceBackgroundLock(ctx)- Log.i(TAG, "Background worker is locked")- }+ companion object {- override fun unlock() {- BackgroundWorkerPreferences(ctx).setLocked(false)- Log.i(TAG, "Background worker is unlocked")- }+ private var engineCount = AtomicInteger(0)- override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {- checkAndEnforceBackgroundLock(binding.applicationContext)- engineCount.incrementAndGet()- Log.i(TAG, "Flutter engine attached. Attached Engines count: $engineCount")- }+ val connectEngines: Int+ get() = engineCount.get()- override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {- engineCount.decrementAndGet()- Log.i(TAG, "Flutter engine detached. Attached Engines count: $engineCount")+ private fun checkAndEnforceBackgroundLock(ctx: Context) {+ // work manager task is running while the main app is opened, cancel the worker+ if (BackgroundWorkerPreferences(ctx).isLocked() &&+ connectEngines > 1 &&+ BackgroundWorkerApiImpl.isBackgroundWorkerRunning()+ ) {+ Log.i(TAG, "Background worker is locked, cancelling the background worker")+ BackgroundWorkerApiImpl.cancelBackgroundWorker(ctx)+ }}+ }++ override fun lock() {+ BackgroundWorkerPreferences(ctx).setLocked(true)+ checkAndEnforceBackgroundLock(ctx)+ Log.i(TAG, "Background worker is locked")+ }++ override fun unlock() {+ BackgroundWorkerPreferences(ctx).setLocked(false)+ Log.i(TAG, "Background worker is unlocked")+ }++ override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {+ checkAndEnforceBackgroundLock(binding.applicationContext)+ engineCount.incrementAndGet()+ Log.i(TAG, "Flutter engine attached. Attached Engines count: $engineCount")+ }++ override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {+ engineCount.decrementAndGet()+ Log.i(TAG, "Flutter engine detached. Attached Engines count: $engineCount")+ }}