fix: skip sync if already in progress, clear locks only on startup

This commit is contained in:
Ash Brener 2026-01-28 14:49:31 +02:00
parent b2397f63be
commit 38230547d6

View File

@ -31,6 +31,7 @@ type SyncManagerState = {
syncCount: number; syncCount: number;
errorCount: number; errorCount: number;
hasSuccessfulSync: boolean; hasSuccessfulSync: boolean;
syncInProgress: boolean;
}; };
const state: SyncManagerState = { const state: SyncManagerState = {
@ -40,6 +41,7 @@ const state: SyncManagerState = {
syncCount: 0, syncCount: 0,
errorCount: 0, errorCount: 0,
hasSuccessfulSync: false, hasSuccessfulSync: false,
syncInProgress: false,
}; };
let currentConfig: MoltbotConfig | null = null; let currentConfig: MoltbotConfig | null = null;
@ -55,11 +57,14 @@ async function runSync(): Promise<void> {
const syncConfig = currentConfig.workspace?.sync; const syncConfig = currentConfig.workspace?.sync;
if (!syncConfig?.provider || syncConfig.provider === "off") return; if (!syncConfig?.provider || syncConfig.provider === "off") return;
const logger = currentLogger; // Skip if a sync is already in progress
if (state.syncInProgress) {
currentLogger.info("[workspace-sync] Sync already in progress, skipping");
return;
}
// Clear any stale locks before attempting sync const logger = currentLogger;
// (handles case where prior sync failed and left a lock behind) state.syncInProgress = true;
clearStaleLocks(logger);
try { try {
// Check if rclone is available // Check if rclone is available
@ -133,6 +138,8 @@ async function runSync(): Promise<void> {
logger.error( logger.error(
`[workspace-sync] Periodic sync error: ${err instanceof Error ? err.message : String(err)}`, `[workspace-sync] Periodic sync error: ${err instanceof Error ? err.message : String(err)}`,
); );
} finally {
state.syncInProgress = false;
} }
} }