Sync before library update

This commit is contained in:
Bartu Özen
2025-12-19 20:20:51 +03:00
parent 18816b0e9b
commit 00e265efcd
2 changed files with 25 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoBuf import kotlinx.serialization.protobuf.ProtoBuf
import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.selectAll
@@ -99,6 +100,24 @@ object SyncManager {
return StartSyncResult.SUCCESS 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() { private suspend fun syncData() {
transaction { transaction {
MangaTable.update({ MangaTable.isSyncing eq true }) { MangaTable.update({ MangaTable.isSyncing eq true }) {

View File

@@ -26,8 +26,10 @@ import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit import kotlinx.coroutines.sync.withPermit
import suwayomi.tachidesk.global.impl.sync.SyncManager
import suwayomi.tachidesk.manga.impl.Category import suwayomi.tachidesk.manga.impl.Category
import suwayomi.tachidesk.manga.impl.CategoryManga import suwayomi.tachidesk.manga.impl.CategoryManga
import suwayomi.tachidesk.manga.impl.Chapter import suwayomi.tachidesk.manga.impl.Chapter
@@ -337,6 +339,10 @@ class Updater : IUpdater {
clear: Boolean?, clear: Boolean?,
forceAll: Boolean, forceAll: Boolean,
) { ) {
runBlocking {
SyncManager.ensureSync()
}
saveLastUpdateTimestamp() saveLastUpdateTimestamp()
if (clear == true) { if (clear == true) {