Keep site jobs across restarts, meter Claude spend, harden admin routes
- internal/jobs replaces explain.Jobs: one registry for every kind of background job, with records in DATA_DIR/jobs. A job's draft is written before its commit, so a failed commit can be saved again (POST /jobs/<kind>/<key>/save) and a restart mid-commit resumes it, without paying Claude twice. - Graceful shutdown: SIGTERM stops new jobs and waits SHUTDOWN_GRACE for running ones while still serving the pages that poll them. - internal/usage records every Claude call's tokens and estimated cost in DATA_DIR/usage.jsonl; CLAUDE_MONTHLY_BUDGET refuses new jobs (429) once a month reaches it. The stats page shows the admin the spend. - auth.SameOrigin refuses cross-origin writes to admin routes. - The LeetCode importer runs the clone's script without the server's secrets in its environment. - A sync that leaves HEAD where it was skips the re-index. - docs_test.go checks README's route table against the routes, and the CLI page against the root README's commands. - The repo is going private: Gitea links on the site are admin-only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
"adam-french.co.uk/usaco-site/internal/claude"
|
||||
"adam-french.co.uk/usaco-site/internal/concept"
|
||||
"adam-french.co.uk/usaco-site/internal/explain"
|
||||
"adam-french.co.uk/usaco-site/internal/jobs"
|
||||
)
|
||||
|
||||
// fakeConceptClaude explains everything as a Fenwick tree. If gate is set,
|
||||
@@ -36,19 +36,19 @@ func (f *fakeConceptClaude) JSON(_ context.Context, req claude.Request, out any)
|
||||
return nil
|
||||
}
|
||||
|
||||
func waitStatus(t *testing.T, r *gin.Engine, path string) explain.Status {
|
||||
func waitStatus(t *testing.T, r *gin.Engine, path string) jobs.Status {
|
||||
t.Helper()
|
||||
for range 400 {
|
||||
rec := do(r, "GET", path, "", true)
|
||||
var s explain.Status
|
||||
var s jobs.Status
|
||||
json.Unmarshal(rec.Body.Bytes(), &s)
|
||||
if s.State != explain.Running {
|
||||
if s.State != jobs.Running {
|
||||
return s
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("%s still running", path)
|
||||
return explain.Status{}
|
||||
return jobs.Status{}
|
||||
}
|
||||
|
||||
// newConcept starts a concept and returns the slug its job saved it under.
|
||||
@@ -61,7 +61,7 @@ func newConcept(t *testing.T, r *gin.Engine, body string) string {
|
||||
var started struct{ ID string }
|
||||
json.Unmarshal(rec.Body.Bytes(), &started)
|
||||
s := waitStatus(t, r, "/usaco/api/concept-jobs/"+started.ID)
|
||||
if s.State != explain.Done {
|
||||
if s.State != jobs.Done {
|
||||
t.Fatalf("job = %+v, want done", s)
|
||||
}
|
||||
res, _ := s.Result.(map[string]any)
|
||||
@@ -172,7 +172,7 @@ func TestConceptRewrite(t *testing.T) {
|
||||
t.Fatalf("second rewrite while running = %d, want 409", rec.Code)
|
||||
}
|
||||
close(fc.gate)
|
||||
if s := waitStatus(t, r, path); s.State != explain.Done {
|
||||
if s := waitStatus(t, r, path); s.State != jobs.Done {
|
||||
t.Fatalf("rewrite job = %+v", s)
|
||||
}
|
||||
for _, want := range []string{"<topic>\nhelp explain fenwick trees", "<previous_version>\n# Fenwick tree", "## Snippet", "<what_to_change>\nstep 2 is wrong"} {
|
||||
|
||||
Reference in New Issue
Block a user