fix(library-update-job): Fix disappearing library update job (#1254)

* fix(library-update-job): Fix disappearing library update job

* extract to shared utils
This commit is contained in:
Cuong-Tran 2025-10-29 09:42:11 +07:00 committed by GitHub
parent 98e4c24939
commit 3e55ea8eac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 12 deletions

View file

@ -23,7 +23,7 @@ import eu.kanade.tachiyomi.util.system.cancelNotification
import eu.kanade.tachiyomi.util.system.isRunning
import eu.kanade.tachiyomi.util.system.setForegroundSafely
import eu.kanade.tachiyomi.util.system.workManager
import kotlinx.coroutines.guava.await
import exh.util.WorkerUtil
import logcat.LogPriority
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.backup.service.BackupPreferences
@ -129,19 +129,13 @@ class BackupCreateJob(private val context: Context, workerParams: WorkerParamete
// KMK -->
/**
* Returns true if a periodic backup job is currently scheduled.
* Returns true if a periodic job is currently scheduled.
* @param context The application context.
* @return True if a periodic backup job is scheduled, false otherwise.
* @return True if a periodic job is scheduled, false otherwise.
* @throws Exception If there is an error retrieving the work info.
*/
suspend fun isPeriodicBackupScheduled(context: Context): Boolean {
val workInfos = context.workManager
.getWorkInfosForUniqueWork(TAG_AUTO)
.await()
return workInfos.any { workInfo ->
!workInfo.state.isFinished
}
return WorkerUtil.isPeriodicJobScheduled(context, TAG_AUTO)
}
// KMK <--
}

View file

@ -44,6 +44,7 @@ import exh.md.utils.MdUtil
import exh.source.LIBRARY_UPDATE_EXCLUDED_SOURCES
import exh.source.MERGED_SOURCE_ID
import exh.source.mangaDexSourceIds
import exh.util.WorkerUtil
import exh.util.nullIfBlank
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.async
@ -889,5 +890,17 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
}
}
}
// KMK -->
/**
* Returns true if a periodic job is currently scheduled.
* @param context The application context.
* @return True if a periodic job is scheduled, false otherwise.
* @throws Exception If there is an error retrieving the work info.
*/
suspend fun isPeriodicUpdateScheduled(context: Context): Boolean {
return WorkerUtil.isPeriodicJobScheduled(context, WORK_NAME_AUTO)
}
// KMK <--
}
}

View file

@ -79,6 +79,7 @@ import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.coil.MangaCoverMetadata
import eu.kanade.tachiyomi.data.connections.discord.DiscordRPCService
import eu.kanade.tachiyomi.data.download.DownloadCache
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.updater.AppUpdateChecker
import eu.kanade.tachiyomi.data.updater.AppUpdateJob
@ -510,6 +511,20 @@ class MainActivity : BaseActivity() {
LaunchedEffect(Unit) {
launchIO {
try {
if (!LibraryUpdateJob.isPeriodicUpdateScheduled(context)) {
LibraryUpdateJob.setupTask(context)
}
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
withContext(Dispatchers.Main) {
Toast.makeText(
context,
stringResource(KMR.strings.job_failed_schedule_update_check, stringResource(MR.strings.unknown_error)),
Toast.LENGTH_LONG,
).show()
}
}
try {
if (!BackupCreateJob.isPeriodicBackupScheduled(context)) {
BackupCreateJob.setupTask(context)
@ -519,7 +534,7 @@ class MainActivity : BaseActivity() {
withContext(Dispatchers.Main) {
Toast.makeText(
context,
stringResource(KMR.strings.job_failed_schedule_update_check, stringResource(MR.strings.unknown_error)),
stringResource(KMR.strings.job_failed_schedule_backup_check, stringResource(MR.strings.unknown_error)),
Toast.LENGTH_LONG,
).show()
}

View file

@ -0,0 +1,24 @@
package exh.util
import android.content.Context
import eu.kanade.tachiyomi.util.system.workManager
import kotlinx.coroutines.guava.await
object WorkerUtil {
/**
* Returns true if a periodic job is currently scheduled.
* @param context The application context.
* @param uniqueWorkName The unique work tag to check for scheduled jobs.
* @return True if a periodic job is scheduled, false otherwise.
* @throws Exception If there is an error retrieving the work info.
*/
suspend fun isPeriodicJobScheduled(context: Context, uniqueWorkName: String): Boolean {
val workInfos = context.workManager
.getWorkInfosForUniqueWork(uniqueWorkName)
.await()
return workInfos.any { workInfo ->
!workInfo.state.isFinished
}
}
}

View file

@ -173,7 +173,8 @@
<string name="current_">Current: %1$s</string>
<string name="migrating_all_entries">Migrating all entries from source</string>
<!-- Misc -->
<string name="job_failed_schedule_update_check">Failed to schedule automatic backup: %s</string>
<string name="job_failed_schedule_update_check">Failed to schedule automatic library update: %s</string>
<string name="job_failed_schedule_backup_check">Failed to schedule automatic backup: %s</string>
<string name="label_to_be_updated">To be updated</string>
<string name="sponsor_me">Sponsor Me</string>
<string name="batch_add_description">Support: MangaDex, E-H, ExH, nH, 8Muses, Tsumino</string>