Skip to content

Commit 589501d

Browse files
Pigbibiclaude
andcommitted
feat: add cron trigger to keep /api/config KV cache warm
- Add scheduled() handler: runs every 5 min, refreshes currentStrategies cache - Uses existing RUNTIME_SETTINGS_DISPATCH_TOKEN (no user auth needed) - Add [triggers] crons to wrangler.toml.example - Combined with SWR pattern: users never wait for GitHub API Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8cf2c2e commit 589501d

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

web/strategy-switch-console/worker.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ export default {
156156
return json({ ok: false, error: error.message || "unexpected error" }, error.status || 500);
157157
}
158158
},
159+
160+
// Cron trigger: keep the current-strategies KV cache warm so
161+
// users never wait for GitHub API on the /api/config endpoint.
162+
async scheduled(event, env, ctx) {
163+
if (!hasConfigStore(env)) return;
164+
const token = env.RUNTIME_SETTINGS_DISPATCH_TOKEN;
165+
if (!token) return;
166+
167+
try {
168+
const accountConfig = await loadAccountOptionsConfig(env);
169+
const strategies = await loadCurrentStrategiesSafely(accountConfig.options, env);
170+
await writeConfigJson(env, CURRENT_STRATEGIES_CACHE_KEY, {
171+
ts: Date.now(),
172+
data: strategies,
173+
});
174+
} catch {
175+
// Silently skip — next user request will populate cache via SWR
176+
}
177+
},
159178
};
160179

161180
class HttpError extends Error {

web/strategy-switch-console/wrangler.toml.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ RUNTIME_SETTINGS_REF = "main"
2828
# STRATEGY_SWITCH_SCHWAB_REPO = "your-org/CharlesSchwabPlatform"
2929
# STRATEGY_SWITCH_FIRSTRADE_REPO = "your-org/FirstradePlatform"
3030

31+
# Cron trigger: refresh KV cache so /api/config never blocks on GitHub API.
32+
# Runs every 5 minutes. Remove this block if you don't need cache warming.
33+
[triggers]
34+
crons = ["*/5 * * * *"]
35+
3136
# Bind this namespace to enable editable /admin login and account management.
3237
# Without it, /admin is read-only and the Worker falls back to secrets.
3338
# [[kv_namespaces]]

0 commit comments

Comments
 (0)