Skip to content

Commit 2f0d993

Browse files
Copilotkiyarose
andauthored
fix: address Copilot review comments with KV-safe periodic save
- Add 15-min periodic save (isPeriodicSave) so latency/history stay fresh without writing every 5-min tick (~96 writes/day vs ~288) - Track _lastIssuesFetchAttemptDay to bound daily retries to once per day regardless of success/failure; log non-2xx HTTP status - Update _lastSaveTime on every save so the 15-min clock resets correctly - Clarify wrangler.toml cron comment: change was intentional to reduce writes Agent-Logs-Url: https://github.com/SillyLittleTech/UpMagic/sessions/9db4878c-bad8-4268-8318-8280f3ca1f11 Co-authored-by: kiyarose <75678535+kiyarose@users.noreply.github.com>
1 parent 7fc94bb commit 2f0d993

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

worker/index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ async function runHeartbeat(env) {
128128
const heartbeatAt = Date.now();
129129
const todayUTC = new Date(heartbeatAt).toISOString().slice(0, 10); // "YYYY-MM-DD"
130130

131+
// Save at most every 15 minutes for latency/history freshness (~96 writes/day on free tier)
132+
const PERIODIC_SAVE_INTERVAL_MS = 15 * 60 * 1000;
133+
const isPeriodicSave = heartbeatAt - (state._lastSaveTime || 0) >= PERIODIC_SAVE_INTERVAL_MS;
134+
131135
for (const target of targets) {
132136
if (state.services[target.id]?.isManual) {
133137
continue;
@@ -175,9 +179,13 @@ async function runHeartbeat(env) {
175179
console.log(`[heartbeat] ${target.name}: ${newStatus}${latency !== null ? ` (${latency}ms)` : ''}`);
176180
}
177181

178-
// Fetch maintained issues from ProjectHub at most once per day
179-
const lastIssuesFetchDate = state._lastIssuesFetchDay || '';
180-
if (lastIssuesFetchDate !== todayUTC) {
182+
// Fetch maintained issues from ProjectHub at most once per day.
183+
// Track attempt day separately from success day so a non-2xx response
184+
// doesn't cause unbounded retries on every heartbeat.
185+
const lastIssuesFetchAttempt = state._lastIssuesFetchAttemptDay || '';
186+
if (lastIssuesFetchAttempt !== todayUTC) {
187+
state._lastIssuesFetchAttemptDay = todayUTC; // record attempt regardless of outcome
188+
needsSave = true; // persist the attempt date so retries are bounded
181189
try {
182190
const issuesRes = await fetch('https://api.github.com/repos/SillyLittleTech/projecthub/issues?labels=maintain&state=open', {
183191
headers: {
@@ -190,17 +198,23 @@ async function runHeartbeat(env) {
190198
state.maintenance = issues.map(i => ({ title: i.title, url: i.html_url, created_at: i.created_at }));
191199
state._lastIssuesFetchDay = todayUTC;
192200
console.log(`[daily] Fetched ${issues.length} maintenance issue(s) for ${todayUTC}`);
193-
needsSave = true; // persist updated maintenance list
201+
} else {
202+
console.error(`[daily] Issues fetch failed: HTTP ${issuesRes.status} for ${todayUTC}`);
194203
}
195204
} catch (e) {
196205
console.error('Failed to fetch maintenance issues', e);
197206
}
198207
}
199208

200-
// Only write to KV when something changed, or once per day as a daily summary
209+
// Only write to KV when something meaningful changed:
210+
// - status change: immediate (for accurate frontend display)
211+
// - periodic (every 15 min): keeps latency/history reasonably fresh (~96 writes/day)
212+
// - daily: ensures a flush even during quiet periods + logs a summary
213+
// - needsSave: issues fetch attempt date or maintenance list must be persisted
201214
const lastDailySaveDate = state._lastDailySaveDay || '';
202215
const isDailySave = lastDailySaveDate !== todayUTC;
203-
if (statusChanged || needsSave || isDailySave) {
216+
if (statusChanged || needsSave || isDailySave || isPeriodicSave) {
217+
state._lastSaveTime = heartbeatAt; // reset the 15-min clock on any save
204218
if (isDailySave) {
205219
state._lastDailySaveDay = todayUTC;
206220
const summary = Object.entries(state.services)

wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ id = "d6179b2f901645de81b9805c4e7ac591"
1717
preview_id = "59e7164e44154d7fa79b2a093b338530"
1818

1919
[triggers]
20-
crons = ["*/5 * * * *"] # Run every 5 minutes for heartbeat
20+
crons = ["*/5 * * * *"] # Every 5 minutes (intentionally relaxed from 1 min to cut KV writes ~5x)

0 commit comments

Comments
 (0)