Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect synchronization behavior of webdav auto sync #568

Merged
merged 5 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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