From 00e265efcd347a6c22c087017ef8963095d32269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartu=20=C3=96zen?= Date: Fri, 19 Dec 2025 20:20:51 +0300 Subject: [PATCH] Sync before library update --- .../tachidesk/global/impl/sync/SyncManager.kt | 19 +++++++++++++++++++ .../tachidesk/manga/impl/update/Updater.kt | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/global/impl/sync/SyncManager.kt b/server/src/main/kotlin/suwayomi/tachidesk/global/impl/sync/SyncManager.kt index 519a560c8..a76d6abea 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/global/impl/sync/SyncManager.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/global/impl/sync/SyncManager.kt @@ -8,6 +8,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.combine import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.serialization.Serializable import kotlinx.serialization.protobuf.ProtoBuf import org.jetbrains.exposed.sql.selectAll @@ -99,6 +100,24 @@ object SyncManager { return StartSyncResult.SUCCESS } + suspend fun ensureSync() { + if (!serverConfig.syncYomiEnabled.value) { + return + } + + if (syncMutex.tryLock()) { + // there is no ongoing sync, so start one + try { + syncData() + } finally { + syncMutex.unlock() + } + } else { + // wait for the ongoing sync to finish + syncMutex.withLock {} + } + } + private suspend fun syncData() { transaction { MangaTable.update({ MangaTable.isSyncing eq true }) { diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt index 42f0a0862..d15389f7a 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt @@ -26,8 +26,10 @@ import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.withPermit +import suwayomi.tachidesk.global.impl.sync.SyncManager import suwayomi.tachidesk.manga.impl.Category import suwayomi.tachidesk.manga.impl.CategoryManga import suwayomi.tachidesk.manga.impl.Chapter @@ -337,6 +339,10 @@ class Updater : IUpdater { clear: Boolean?, forceAll: Boolean, ) { + runBlocking { + SyncManager.ensureSync() + } + saveLastUpdateTimestamp() if (clear == true) {