mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-15 15:02:49 +01:00
refactor(GoogleDrive): add more logging, also use appdata folder.
Signed-off-by: KaiserBh <kaiserbh@proton.me>
This commit is contained in:
parent
9a53f4c0ab
commit
e04d191dfb
@ -67,40 +67,46 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
|
||||
private val googleDriveService = GoogleDriveService(context)
|
||||
|
||||
override suspend fun beforeSync() {
|
||||
try {
|
||||
googleDriveService.refreshToken()
|
||||
val drive = googleDriveService.driveService ?: throw Exception("Google Drive service not initialized")
|
||||
|
||||
var backoff = 2000L // Start with 2 seconds
|
||||
var backoff = 2000L
|
||||
|
||||
while (true) {
|
||||
val lockFiles = findLockFile(drive) // Fetch the current list of lock files
|
||||
val lockFiles = findLockFile(drive)
|
||||
logcat(LogPriority.DEBUG) { "Found ${lockFiles.size} lock file(s)" }
|
||||
|
||||
when {
|
||||
lockFiles.isEmpty() -> {
|
||||
// No lock file exists, try to create a new one
|
||||
logcat(LogPriority.DEBUG) { "No lock file found, creating a new one" }
|
||||
createLockFile(drive)
|
||||
}
|
||||
lockFiles.size == 1 -> {
|
||||
// Exactly one lock file exists
|
||||
val lockFile = lockFiles.first()
|
||||
val createdTime = Instant.parse(lockFile.createdTime.toString())
|
||||
val ageMinutes = java.time.Duration.between(createdTime, Instant.now()).toMinutes()
|
||||
logcat(LogPriority.DEBUG) { "Lock file age: $ageMinutes minutes" }
|
||||
if (ageMinutes <= 3) {
|
||||
// Lock file is new and presumably held by this process, break the loop to proceed
|
||||
logcat(LogPriority.DEBUG) { "Lock file is new, proceeding with sync" }
|
||||
break
|
||||
} else {
|
||||
// Lock file is old, delete and attempt to create a new one
|
||||
logcat(LogPriority.DEBUG) { "Lock file is old, deleting and creating a new one" }
|
||||
deleteLockFile(drive)
|
||||
createLockFile(drive)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
// More than one lock file exists, likely due to a race condition
|
||||
logcat(LogPriority.DEBUG) { "Multiple lock files found, applying backoff" }
|
||||
delay(backoff) // Apply backoff strategy
|
||||
backoff = (backoff * 2).coerceAtMost(32000L) // Max backoff of 32 seconds
|
||||
backoff = (backoff * 2).coerceAtMost(32000L)
|
||||
logcat(LogPriority.DEBUG) { "Backoff increased to $backoff milliseconds" }
|
||||
}
|
||||
}
|
||||
// The loop continues until it can confirm that there's exactly one new lock file.
|
||||
logcat(LogPriority.DEBUG) { "Loop iteration complete, backoff time: $backoff" }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR) { "Error in GoogleDrive beforeSync: ${e.message}" }
|
||||
}
|
||||
}
|
||||
|
||||
@ -199,9 +205,11 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
|
||||
|
||||
private fun createLockFile(drive: Drive) {
|
||||
try {
|
||||
val fileMetadata = File()
|
||||
fileMetadata.name = lockFileName
|
||||
fileMetadata.mimeType = "text/plain"
|
||||
val fileMetadata = File().apply {
|
||||
name = lockFileName
|
||||
mimeType = "text/plain"
|
||||
parents = listOf("appDataFolder")
|
||||
}
|
||||
|
||||
// Create an empty content to upload as the lock file
|
||||
val emptyContent = ByteArrayContent.fromString("text/plain", "")
|
||||
|
Loading…
Reference in New Issue
Block a user