Skip to content

Commit

Permalink
Fix incorrect synchronization behavior of webdav auto sync (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
littlepenguin66 authored Dec 29, 2024
1 parent 6bc1f4b commit 56ca81d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FolderOpenOutlined, SaveOutlined } from '@ant-design/icons'
import { HStack } from '@renderer/components/Layout'
import { useSettings } from '@renderer/hooks/useSettings'
import { backupToWebdav, restoreFromWebdav } from '@renderer/services/BackupService'
import { backupToWebdav, restoreFromWebdav, startAutoSync, stopAutoSync } from '@renderer/services/BackupService'
import { useAppDispatch } from '@renderer/store'
import {
setWebdavAutoSync,
Expand Down Expand Up @@ -66,6 +66,11 @@ const WebDavSettings: FC = () => {

const onToggleAutoSync = (checked: boolean) => {
dispatch(setWebdavAutoSync(checked))
if (checked) {
startAutoSync()
} else {
stopAutoSync()
}
}

const onSyncIntervalChange = (value: number) => {
Expand Down
75 changes: 55 additions & 20 deletions src/renderer/src/services/BackupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export async function reset() {

// 备份到 webdav
export async function backupToWebdav({ showMessage = true }: { showMessage?: boolean } = {}) {
if (isManualBackupRunning) {
console.log('[Backup] Manual backup already in progress')
return
}
const { webdavHost, webdavUser, webdavPass, webdavPath } = store.getState().settings

const backupData = await getBackupData()
Expand All @@ -83,6 +87,8 @@ export async function backupToWebdav({ showMessage = true }: { showMessage?: boo
title: i18n.t('message.backup.failed'),
content: error.message
})
} finally {
isManualBackupRunning = false
}
}

Expand All @@ -109,40 +115,69 @@ export async function restoreFromWebdav() {
}
}

let syncInterval: NodeJS.Timeout | null = null
let autoSyncStarted = false
let syncTimeout: NodeJS.Timeout | null = null
let isAutoBackupRunning = false
let isManualBackupRunning = false

export function startAutoSync() {
if (autoSyncStarted) {
return
}

const { webdavAutoSync, webdavHost, webdavSyncInterval } = store.getState().settings

if (syncInterval) {
stopAutoSync()
if (!webdavAutoSync || !webdavHost || webdavSyncInterval <= 0) {
console.log('[AutoSync] Invalid sync settings, auto sync disabled')
return
}

if (webdavAutoSync && webdavHost) {
console.log('[AutoSync] Starting auto sync with interval:', webdavSyncInterval, 'minutes')

const performBackup = async () => {
try {
console.log('[AutoSync] Performing backup...')
await backupToWebdav({ showMessage: false })
window.message.success({ content: i18n.t('message.backup.success'), key: 'webdav-sync' })
} catch (error) {
console.error('[AutoSync] Backup failed:', error)
window.message.error({ content: i18n.t('message.backup.failed'), key: 'webdav-sync' })
}
autoSyncStarted = true

stopAutoSync()

scheduleNextBackup()

function scheduleNextBackup() {
if (syncTimeout) {
clearTimeout(syncTimeout)
syncTimeout = null
}

syncInterval = setInterval(performBackup, webdavSyncInterval * 60 * 1000)
syncTimeout = setTimeout(performAutoBackup, webdavSyncInterval * 60 * 1000)
console.log(`[AutoSync] Next sync scheduled in ${webdavSyncInterval} minutes`)
}

async function performAutoBackup() {
if (isAutoBackupRunning || isManualBackupRunning) {
console.log('[AutoSync] Backup already in progress, rescheduling')
scheduleNextBackup()
return
}

console.log(`[AutoSync] Sync interval set up: ${webdavSyncInterval} minutes`)
isAutoBackupRunning = true
try {
console.log('[AutoSync] Performing auto backup...')
await backupToWebdav({ showMessage: false })
window.message.success({ content: i18n.t('message.backup.success'), key: 'webdav-auto-sync' })
} catch (error) {
console.error('[AutoSync] Auto backup failed:', error)
window.message.error({ content: i18n.t('message.backup.failed'), key: 'webdav-auto-sync' })
} finally {
isAutoBackupRunning = false
scheduleNextBackup()
}
}
}

export function stopAutoSync() {
if (syncInterval) {
if (syncTimeout) {
console.log('[AutoSync] Stopping auto sync')
clearInterval(syncInterval)
syncInterval = null
clearTimeout(syncTimeout)
syncTimeout = null
}
isAutoBackupRunning = false
autoSyncStarted = false
}

async function getBackupData() {
Expand Down

0 comments on commit 56ca81d

Please sign in to comment.