Skip to content

Commit

Permalink
Add id to backup status
Browse files Browse the repository at this point in the history
Makes it possible to differentiate between backup restore processes.
  • Loading branch information
schroda committed Oct 30, 2023
1 parent 9a9bffa commit a2b7807
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ class BackupMutation {
val (clientMutationId, backup) = input

return future {
val currentBackupStatus = ProtoBackupImport.backupRestoreState.value

GlobalScope.launch {
ProtoBackupImport.restore(backup.content)
}

val status =
withTimeout(10.seconds) {
ProtoBackupImport.backupRestoreState.first {
it != ProtoBackupImport.BackupRestoreState.Idle
it.id != currentBackupStatus.id
}.toStatus()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class BackupRestoreState {
}

data class BackupRestoreStatus(
val id: Long,
val state: BackupRestoreState,
val totalManga: Int,
val mangaProgress: Int,
Expand All @@ -19,24 +20,28 @@ fun ProtoBackupImport.BackupRestoreState.toStatus(): BackupRestoreStatus {
return when (this) {
is ProtoBackupImport.BackupRestoreState.Success ->
BackupRestoreStatus(
id,
state = BackupRestoreState.SUCCESS,
totalManga = 0,
mangaProgress = 0,
)
is ProtoBackupImport.BackupRestoreState.Failure ->
BackupRestoreStatus(
id,
state = BackupRestoreState.FAILURE,
totalManga = 0,
mangaProgress = 0,
)
is ProtoBackupImport.BackupRestoreState.RestoringCategories ->
BackupRestoreStatus(
id,
state = BackupRestoreState.RESTORING_CATEGORIES,
totalManga = totalManga,
mangaProgress = 0,
)
is ProtoBackupImport.BackupRestoreState.RestoringManga ->
BackupRestoreStatus(
id,
state = BackupRestoreState.RESTORING_MANGA,
totalManga = totalManga,
mangaProgress = current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ object ProtoBackupImport : ProtoBackupBase() {

private val backupMutex = Mutex()

sealed class BackupRestoreState {
sealed class BackupRestoreState(val id: Long) {
class Success(id: Long) : BackupRestoreState(id)

data object Success : BackupRestoreState()
class Failure(id: Long) : BackupRestoreState(id)

data object Failure : BackupRestoreState()
class RestoringCategories(id: Long, val totalManga: Int) : BackupRestoreState(id)

data class RestoringCategories(val totalManga: Int) : BackupRestoreState()

data class RestoringManga(val current: Int, val totalManga: Int, val title: String) : BackupRestoreState()
class RestoringManga(id: Long, val current: Int, val totalManga: Int, val title: String) : BackupRestoreState(id)
}

val backupRestoreState = MutableStateFlow<BackupRestoreState>(BackupRestoreState.Idle)
val backupRestoreState = MutableStateFlow<BackupRestoreState>(BackupRestoreState.Success(-1))

suspend fun restore(sourceStream: InputStream): ValidationResult {
return backupMutex.withLock {
val id = System.currentTimeMillis()

try {
performRestore(sourceStream)
performRestore(id, sourceStream)
} catch (e: Exception) {
backupRestoreState.value = BackupRestoreState.Failure
backupRestoreState.value = BackupRestoreState.Failure(id)
ValidationResult(
emptyList(),
emptyList(),
Expand All @@ -79,15 +79,18 @@ object ProtoBackupImport : ProtoBackupBase() {
}
}

private fun performRestore(sourceStream: InputStream): ValidationResult {
private fun performRestore(
id: Long,
sourceStream: InputStream,
): ValidationResult {
val backupString = sourceStream.source().gzip().buffer().use { it.readByteArray() }
val backup = parser.decodeFromByteArray(BackupSerializer, backupString)

val validationResult = validate(backup)

restoreAmount = backup.backupManga.size + 1 // +1 for categories

backupRestoreState.value = BackupRestoreState.RestoringCategories(backup.backupManga.size)
backupRestoreState.value = BackupRestoreState.RestoringCategories(id, backup.backupManga.size)
// Restore categories
if (backup.backupCategories.isNotEmpty()) {
restoreCategories(backup.backupCategories)
Expand All @@ -109,6 +112,7 @@ object ProtoBackupImport : ProtoBackupBase() {
backup.backupManga.forEachIndexed { index, manga ->
backupRestoreState.value =
BackupRestoreState.RestoringManga(
id,
current = index + 1,
totalManga = backup.backupManga.size,
title = manga.title,
Expand All @@ -134,7 +138,7 @@ object ProtoBackupImport : ProtoBackupBase() {
""".trimIndent()
}

backupRestoreState.value = BackupRestoreState.Success
backupRestoreState.value = BackupRestoreState.Success(id)

return validationResult
}
Expand Down

0 comments on commit a2b7807

Please sign in to comment.