diff --git a/src/renderer/src/pages/settings/DataSettings/WebDavSettings.tsx b/src/renderer/src/pages/settings/DataSettings/WebDavSettings.tsx index 5fb80d48c..e6480a655 100644 --- a/src/renderer/src/pages/settings/DataSettings/WebDavSettings.tsx +++ b/src/renderer/src/pages/settings/DataSettings/WebDavSettings.tsx @@ -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, @@ -66,6 +66,11 @@ const WebDavSettings: FC = () => { const onToggleAutoSync = (checked: boolean) => { dispatch(setWebdavAutoSync(checked)) + if (checked) { + startAutoSync() + } else { + stopAutoSync() + } } const onSyncIntervalChange = (value: number) => { diff --git a/src/renderer/src/services/BackupService.ts b/src/renderer/src/services/BackupService.ts index d655c3985..51672c80f 100644 --- a/src/renderer/src/services/BackupService.ts +++ b/src/renderer/src/services/BackupService.ts @@ -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() @@ -83,6 +87,8 @@ export async function backupToWebdav({ showMessage = true }: { showMessage?: boo title: i18n.t('message.backup.failed'), 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() { + 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() {