@@ -23,9 +23,9 @@ internal class TextToImageTask(
2323 workerParameters : WorkerParameters ,
2424 pushNotificationManager : PushNotificationManager ,
2525 activityIntentProvider : ActivityIntentProvider ,
26- preferenceManager : PreferenceManager ,
2726 observeHordeProcessStatusUseCase : ObserveHordeProcessStatusUseCase ,
2827 observeLocalDiffusionProcessStatusUseCase : ObserveLocalDiffusionProcessStatusUseCase ,
28+ private val preferenceManager : PreferenceManager ,
2929 private val backgroundWorkObserver : BackgroundWorkObserver ,
3030 private val textToImageUseCase : TextToImageUseCase ,
3131 private val fileProviderDescriptor : FileProviderDescriptor ,
@@ -45,12 +45,26 @@ internal class TextToImageTask(
4545 override val genericNotificationId: Int = NOTIFICATION_TEXT_TO_IMAGE_GENERIC
4646
4747 override fun createWork (): Single <Result > {
48+ // Workaround for LocalDiffusion provider:
49+ //
50+ // If LocalDiffusion process previously died, prevent WorkManager to go to infinite
51+ // task repeat loop.
52+ if (preferenceManager.backgroundProcessCount > 0 ) {
53+ handleProcess()
54+ handleError(Throwable (" Background process count > 0" ))
55+ compositeDisposable.clear()
56+ return Single .just(Result .failure())
57+ }
58+
59+ preferenceManager.backgroundProcessCount++
4860 handleStart()
4961 backgroundWorkObserver.refreshStatus()
5062 backgroundWorkObserver.dismissResult()
63+
5164 return try {
5265 val file = File (fileProviderDescriptor.workCacheDirPath, Constants .FILE_TEXT_TO_IMAGE )
5366 if (! file.exists()) {
67+ preferenceManager.backgroundProcessCount--
5468 handleError(Throwable (" File is null." ))
5569 compositeDisposable.clear()
5670 return Single .just(Result .failure())
@@ -60,6 +74,7 @@ internal class TextToImageTask(
6074 val payload = bytes.toTextToImagePayload()
6175
6276 if (payload == null ) {
77+ preferenceManager.backgroundProcessCount--
6378 handleError(Throwable (" Payload is null." ))
6479 compositeDisposable.clear()
6580 return Single .just(Result .failure())
@@ -71,15 +86,18 @@ internal class TextToImageTask(
7186 textToImageUseCase(payload)
7287 .doOnSubscribe { handleProcess() }
7388 .map { result ->
89+ preferenceManager.backgroundProcessCount--
7490 handleSuccess(result)
7591 Result .success()
7692 }
7793 .onErrorReturn { t ->
94+ preferenceManager.backgroundProcessCount--
7895 handleError(t)
7996 Result .failure()
8097 }
8198 .doFinally { compositeDisposable.clear() }
8299 } catch (e: Exception ) {
100+ preferenceManager.backgroundProcessCount--
83101 handleError(e)
84102 compositeDisposable.clear()
85103 Single .just(Result .failure())
0 commit comments