fix: reduce external API calls ~10x by optimizing sync intervals

Root cause: background calendar sync every 2min with forceRefresh:true
+ task sync every 5min + cache stale threshold of 2min = 12,500+
Google Calendar + 8,800 Google Tasks API calls/day for a single user.

Changes:
- Calendar background sync: 2min → 15min
- Task sync polling: 5min → 15min
- Cache stale threshold: 2min → 15min
- Remove forceRefresh:true from background sync (let staleness check
  decide naturally)
- Remove redundant 8s re-fetch after background sync
- User actions (create/update/delete) still sync immediately

Expected reduction: ~10-15x fewer external API calls.

v1.68.1

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
mARTin 2026-03-24 12:35:22 +01:00
parent 13bd90bd2a
commit ff3d458b6d
3 changed files with 10 additions and 15 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "my-weekly-todo-list", "name": "my-weekly-todo-list",
"version": "1.68.0", "version": "1.68.1",
"description": "A web-based weekly task management application that organizes to-dos and calendar events in a single, intuitive weekly view", "description": "A web-based weekly task management application that organizes to-dos and calendar events in a single, intuitive weekly view",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -2830,7 +2830,7 @@ export default function WeeklyView() {
} }
}, []); }, []);
// Periodic pull-sync from Google Tasks (every 2 minutes) // Periodic pull-sync from external task providers (every 15 minutes)
useEffect(() => { useEffect(() => {
if (!session) return; if (!session) return;
const interval = setInterval( const interval = setInterval(
@ -2843,16 +2843,14 @@ export default function WeeklyView() {
console.log( console.log(
`[SYNC] Pulled ${data.updated} updates, ${data.deleted} deletions, ${data.created || 0} new tasks`, `[SYNC] Pulled ${data.updated} updates, ${data.deleted} deletions, ${data.created || 0} new tasks`,
); );
fetchTasks(); // Reload to reflect changes fetchTasks();
} }
} }
} catch (e) { } catch (e) {
console.error("[SYNC] Task sync error:", e); console.error("[SYNC] Task sync error:", e);
setSyncError("Task sync failed");
setTimeout(() => setSyncError(null), 10000);
} }
}, },
5 * 60 * 1000, 15 * 60 * 1000,
); );
return () => clearInterval(interval); return () => clearInterval(interval);
}, [session]); }, [session]);
@ -2895,7 +2893,7 @@ export default function WeeklyView() {
}; };
}, [session]); }, [session]);
// Periodic background calendar cache refresh (every 2 minutes) // Periodic background calendar cache refresh (every 15 minutes)
useEffect(() => { useEffect(() => {
if (!session) return; if (!session) return;
const interval = setInterval( const interval = setInterval(
@ -2912,23 +2910,20 @@ export default function WeeklyView() {
timeMax: new Date( timeMax: new Date(
now.getTime() + 14 * 24 * 60 * 60 * 1000, now.getTime() + 14 * 24 * 60 * 60 * 1000,
).toISOString(), ).toISOString(),
forceRefresh: true,
}), }),
}); });
if (res.ok) { if (res.ok) {
const data = await res.json(); const data = await res.json();
if (data.queued > 0 || data.refreshed > 0) { if (data.queued > 0) {
// Cache was refreshed; re-fetch events after delay // Stale connections are refreshing in background; re-read cache after delay
setTimeout(() => fetchCalendarEvents(), 8000); setTimeout(() => fetchCalendarEvents(), 10000);
} }
} }
} catch (e) { } catch (e) {
console.error("[SYNC] Calendar sync error:", e); console.error("[SYNC] Calendar sync error:", e);
setSyncError("Calendar sync failed");
setTimeout(() => setSyncError(null), 10000);
} }
}, },
2 * 60 * 1000, 15 * 60 * 1000,
); );
return () => clearInterval(interval); return () => clearInterval(interval);
}, [session, fetchCalendarEvents]); }, [session, fetchCalendarEvents]);

View File

@ -4,7 +4,7 @@
import { prisma } from './prisma'; import { prisma } from './prisma';
import { getCalendarEvents, CalendarEvent, CalendarConnection } from './calendar-events'; import { getCalendarEvents, CalendarEvent, CalendarConnection } from './calendar-events';
const STALE_THRESHOLD_MS = 2 * 60 * 1000; // 2 minutes — match background sync interval const STALE_THRESHOLD_MS = 15 * 60 * 1000; // 15 minutes — reduce API calls to providers
/** /**
* Get the Monday (start of ISO week) for a given date. * Get the Monday (start of ISO week) for a given date.