Skip to content

Commit dd9a6cf

Browse files
Copilotkiyarose
andauthored
feat: reduce KV writes - only save on status change or once daily
Agent-Logs-Url: https://github.com/SillyLittleTech/UpMagic/sessions/4b09e790-8b74-410c-bab6-5257f12e7b43 Co-authored-by: kiyarose <75678535+kiyarose@users.noreply.github.com>
1 parent 3696ec8 commit dd9a6cf

2 files changed

Lines changed: 37 additions & 14 deletions

File tree

worker/index.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ async function runHeartbeat(env) {
123123
if (!state.historyByService) state.historyByService = {};
124124

125125
let statusChanged = false;
126+
let needsSave = false;
126127
let notifications = [];
127128
const heartbeatAt = Date.now();
129+
const todayUTC = new Date(heartbeatAt).toISOString().slice(0, 10); // "YYYY-MM-DD"
128130

129131
for (const target of targets) {
130132
if (state.services[target.id]?.isManual) {
@@ -168,25 +170,46 @@ async function runHeartbeat(env) {
168170
status: newStatus,
169171
latency: latency
170172
});
173+
174+
// Internal log every heartbeat (no KV write)
175+
console.log(`[heartbeat] ${target.name}: ${newStatus}${latency !== null ? ` (${latency}ms)` : ''}`);
171176
}
172177

173-
// Fetch maintained issues from ProjectHub
174-
try {
175-
const issuesRes = await fetch('https://api.github.com/repos/SillyLittleTech/projecthub/issues?labels=maintain&state=open', {
176-
headers: {
177-
'User-Agent': 'SLT-Status-Worker',
178-
'Authorization': env.ISSUES_PAT ? `Bearer ${env.ISSUES_PAT}` : ''
178+
// Fetch maintained issues from ProjectHub at most once per day
179+
const lastIssuesFetchDate = state._lastIssuesFetchDay || '';
180+
if (lastIssuesFetchDate !== todayUTC) {
181+
try {
182+
const issuesRes = await fetch('https://api.github.com/repos/SillyLittleTech/projecthub/issues?labels=maintain&state=open', {
183+
headers: {
184+
'User-Agent': 'SLT-Status-Worker',
185+
'Authorization': env.ISSUES_PAT ? `Bearer ${env.ISSUES_PAT}` : ''
186+
}
187+
});
188+
if (issuesRes.ok) {
189+
const issues = await issuesRes.json();
190+
state.maintenance = issues.map(i => ({ title: i.title, url: i.html_url, created_at: i.created_at }));
191+
state._lastIssuesFetchDay = todayUTC;
192+
console.log(`[daily] Fetched ${issues.length} maintenance issue(s) for ${todayUTC}`);
193+
needsSave = true; // persist updated maintenance list
179194
}
180-
});
181-
if (issuesRes.ok) {
182-
const issues = await issuesRes.json();
183-
state.maintenance = issues.map(i => ({ title: i.title, url: i.html_url, created_at: i.created_at }));
195+
} catch (e) {
196+
console.error('Failed to fetch maintenance issues', e);
184197
}
185-
} catch (e) {
186-
console.error('Failed to fetch maintenance issues', e);
187198
}
188199

189-
await saveState(env, state);
200+
// Only write to KV when something changed, or once per day as a daily summary
201+
const lastDailySaveDate = state._lastDailySaveDay || '';
202+
const isDailySave = lastDailySaveDate !== todayUTC;
203+
if (statusChanged || needsSave || isDailySave) {
204+
if (isDailySave) {
205+
state._lastDailySaveDay = todayUTC;
206+
const summary = Object.entries(state.services)
207+
.map(([, svc]) => `${svc.name}: ${svc.status}`)
208+
.join(', ');
209+
console.log(`[daily summary] ${todayUTC}${summary}`);
210+
}
211+
await saveState(env, state);
212+
}
190213
}
191214

192215
async function saveState(env, state) {

wrangler.toml

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

2222
[triggers]
23-
crons = ["* * * * *"] # Run every minute for heartbeat
23+
crons = ["*/5 * * * *"] # Run every 5 minutes for heartbeat

0 commit comments

Comments
 (0)