Fix incorrect synchronization behavior of webdav auto sync (#568)
This commit is contained in:
parent
6bc1f4b640
commit
56ca81d245
@ -1,7 +1,7 @@
|
|||||||
import { FolderOpenOutlined, SaveOutlined } from '@ant-design/icons'
|
import { FolderOpenOutlined, SaveOutlined } from '@ant-design/icons'
|
||||||
import { HStack } from '@renderer/components/Layout'
|
import { HStack } from '@renderer/components/Layout'
|
||||||
import { useSettings } from '@renderer/hooks/useSettings'
|
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 { useAppDispatch } from '@renderer/store'
|
||||||
import {
|
import {
|
||||||
setWebdavAutoSync,
|
setWebdavAutoSync,
|
||||||
@ -66,6 +66,11 @@ const WebDavSettings: FC = () => {
|
|||||||
|
|
||||||
const onToggleAutoSync = (checked: boolean) => {
|
const onToggleAutoSync = (checked: boolean) => {
|
||||||
dispatch(setWebdavAutoSync(checked))
|
dispatch(setWebdavAutoSync(checked))
|
||||||
|
if (checked) {
|
||||||
|
startAutoSync()
|
||||||
|
} else {
|
||||||
|
stopAutoSync()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const onSyncIntervalChange = (value: number) => {
|
const onSyncIntervalChange = (value: number) => {
|
||||||
|
|||||||
@ -59,6 +59,10 @@ export async function reset() {
|
|||||||
|
|
||||||
// 备份到 webdav
|
// 备份到 webdav
|
||||||
export async function backupToWebdav({ showMessage = true }: { showMessage?: boolean } = {}) {
|
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 { webdavHost, webdavUser, webdavPass, webdavPath } = store.getState().settings
|
||||||
|
|
||||||
const backupData = await getBackupData()
|
const backupData = await getBackupData()
|
||||||
@ -83,6 +87,8 @@ export async function backupToWebdav({ showMessage = true }: { showMessage?: boo
|
|||||||
title: i18n.t('message.backup.failed'),
|
title: i18n.t('message.backup.failed'),
|
||||||
content: error.message
|
content: error.message
|
||||||
})
|
})
|
||||||
|
} finally {
|
||||||
|
isManualBackupRunning = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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() {
|
export function startAutoSync() {
|
||||||
|
if (autoSyncStarted) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const { webdavAutoSync, webdavHost, webdavSyncInterval } = store.getState().settings
|
const { webdavAutoSync, webdavHost, webdavSyncInterval } = store.getState().settings
|
||||||
|
|
||||||
if (syncInterval) {
|
if (!webdavAutoSync || !webdavHost || webdavSyncInterval <= 0) {
|
||||||
|
console.log('[AutoSync] Invalid sync settings, auto sync disabled')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
autoSyncStarted = true
|
||||||
|
|
||||||
stopAutoSync()
|
stopAutoSync()
|
||||||
|
|
||||||
|
scheduleNextBackup()
|
||||||
|
|
||||||
|
function scheduleNextBackup() {
|
||||||
|
if (syncTimeout) {
|
||||||
|
clearTimeout(syncTimeout)
|
||||||
|
syncTimeout = null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (webdavAutoSync && webdavHost) {
|
syncTimeout = setTimeout(performAutoBackup, webdavSyncInterval * 60 * 1000)
|
||||||
console.log('[AutoSync] Starting auto sync with interval:', webdavSyncInterval, 'minutes')
|
console.log(`[AutoSync] Next sync scheduled in ${webdavSyncInterval} minutes`)
|
||||||
|
}
|
||||||
|
|
||||||
const performBackup = async () => {
|
async function performAutoBackup() {
|
||||||
|
if (isAutoBackupRunning || isManualBackupRunning) {
|
||||||
|
console.log('[AutoSync] Backup already in progress, rescheduling')
|
||||||
|
scheduleNextBackup()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
isAutoBackupRunning = true
|
||||||
try {
|
try {
|
||||||
console.log('[AutoSync] Performing backup...')
|
console.log('[AutoSync] Performing auto backup...')
|
||||||
await backupToWebdav({ showMessage: false })
|
await backupToWebdav({ showMessage: false })
|
||||||
window.message.success({ content: i18n.t('message.backup.success'), key: 'webdav-sync' })
|
window.message.success({ content: i18n.t('message.backup.success'), key: 'webdav-auto-sync' })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[AutoSync] Backup failed:', error)
|
console.error('[AutoSync] Auto backup failed:', error)
|
||||||
window.message.error({ content: i18n.t('message.backup.failed'), key: 'webdav-sync' })
|
window.message.error({ content: i18n.t('message.backup.failed'), key: 'webdav-auto-sync' })
|
||||||
|
} finally {
|
||||||
|
isAutoBackupRunning = false
|
||||||
|
scheduleNextBackup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syncInterval = setInterval(performBackup, webdavSyncInterval * 60 * 1000)
|
|
||||||
|
|
||||||
console.log(`[AutoSync] Sync interval set up: ${webdavSyncInterval} minutes`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stopAutoSync() {
|
export function stopAutoSync() {
|
||||||
if (syncInterval) {
|
if (syncTimeout) {
|
||||||
console.log('[AutoSync] Stopping auto sync')
|
console.log('[AutoSync] Stopping auto sync')
|
||||||
clearInterval(syncInterval)
|
clearTimeout(syncTimeout)
|
||||||
syncInterval = null
|
syncTimeout = null
|
||||||
}
|
}
|
||||||
|
isAutoBackupRunning = false
|
||||||
|
autoSyncStarted = false
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getBackupData() {
|
async function getBackupData() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user