Keep site jobs across restarts, meter Claude spend, harden admin routes
Test and publish the site / test (push) Successful in 2m15s
Test and publish the site / publish (push) Successful in 9m5s

- 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:
2026-10-23 22:10:01 +01:00
co-authored by Claude Opus 5.5
parent 7638d88570
commit cae5ec8e5a
42 changed files with 1849 additions and 264 deletions
+22 -7
View File
@@ -21,6 +21,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
@@ -29,7 +30,9 @@ import (
"adam-french.co.uk/usaco-site/internal/auth"
"adam-french.co.uk/usaco-site/internal/explain"
"adam-french.co.uk/usaco-site/internal/index"
"adam-french.co.uk/usaco-site/internal/jobs"
"adam-french.co.uk/usaco-site/internal/repo"
"adam-french.co.uk/usaco-site/internal/usage"
)
// Repo is the part of repo.Repo the handlers use, so tests can fake it.
@@ -67,13 +70,20 @@ type Server struct {
// RUNNER_URL is not set, which turns only running and linting off.
Runner Runner
// Jobs keeps the background jobs' records (internal/jobs); nil keeps
// them in memory, which the tests use.
Jobs *jobs.Registry
// Usage is the Claude cost ledger and monthly budget; nil tracks
// nothing and allows everything.
Usage *usage.Ledger
idx atomic.Pointer[index.Index]
// Separate, so a review, an explanation and an evaluation of one
// problem can run at the same time.
jobs, reviewJobs, evalJobs explain.Jobs
// New concepts are keyed by a job id (the slug is Claude's to pick),
// rewrites by slug.
newConceptJobs, conceptJobs explain.Jobs
jobsOnce sync.Once
// Separate kinds, so a review, an explanation and an evaluation of one
// problem can run at the same time. New concepts are keyed by a job id
// (the slug is Claude's to pick), rewrites by slug.
explainJobs, reviewJobs, evalJobs, newConceptJobs, conceptJobs *jobs.Kind
}
// SetIndex swaps in a freshly built index. repo.Repo calls it (through
@@ -96,7 +106,10 @@ func (s *Server) Index() *index.Index { return s.idx.Load() }
// Routes registers everything under api.
func (s *Server) Routes(api *gin.RouterGroup) {
admin := api.Group("", auth.Require(s.Checker))
s.initJobs()
// SameOrigin first, so a cross-site request is refused without asking
// the site backend about its cookies.
admin := api.Group("", auth.SameOrigin(), auth.Require(s.Checker))
api.GET("/health", s.health)
api.GET("/me", s.me)
@@ -127,6 +140,8 @@ func (s *Server) Routes(api *gin.RouterGroup) {
admin.POST("/concepts/:slug/explain", s.rewriteConcept)
admin.GET("/concepts/:slug/explain", s.rewriteStatus)
admin.DELETE("/concepts/:slug", s.deleteConcept)
admin.POST("/jobs/:kind/:key/save", s.retrySave)
admin.GET("/usage", s.usage)
api.POST("/hooks/gitea", s.webhook)
}