Phase 9, part 6: background work shared safely between copies of the app

internal/jobs keeps named locks as leases in the database (schema 31:
locks, instances), taken with one conditional UPDATE on SQLite or Postgres,
renewed while the work runs and lapsing if its copy stops. Each background
loop's turn, sending an email, audits, discovery runs and imports run
under a lock; OAuth sign-ins under way are kept in the database. Copies
check in, and work marked running is recovered at start only when no
other copy is alive. Settings -> About lists the copies and work under
way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 07:56:47 -07:00
co-authored by Claude Opus 5.5
parent ba6114f367
commit c6c4c40600
18 changed files with 873 additions and 150 deletions
+50 -11
View File
@@ -2,6 +2,7 @@ package web
import (
"context"
"encoding/json"
"html"
"net/http"
"net/url"
@@ -391,26 +392,64 @@ func (s *Server) oauthStart(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
s.mu.Lock()
for state, old := range s.pending {
if time.Since(old.Started) > oauthTTL {
delete(s.pending, state)
}
// Kept in the database: the callback may reach another copy of the app.
if err := s.DB.PutJSON(oauthFlowKey+p.State, p); err != nil {
s.serverError(w, err)
return
}
s.pending[p.State] = p
s.mu.Unlock()
s.forgetOldOAuthFlows()
s.Log.Info("oauth: sign-in started", "provider", provider, "redirect_uri", p.RedirectURI)
http.Redirect(w, r, authURL, http.StatusSeeOther)
}
// OAuth sign-ins under way are kept as settings, by state.
const oauthFlowKey = "oauth:flow:"
// takeOAuthFlow reads and removes the sign-in a callback's state names: a
// state is good for one callback, even if two copies of the app get it.
func (s *Server) takeOAuthFlow(state string) *oauth.Pending {
if state == "" {
return nil
}
var p oauth.Pending
if ok, err := s.DB.GetJSON(oauthFlowKey+state, &p); !ok || err != nil {
return nil
}
res, err := s.DB.Exec(`DELETE FROM settings WHERE key = ?`, oauthFlowKey+state)
if err != nil {
return nil
}
if n, _ := res.RowsAffected(); n != 1 {
return nil // the other copy took it
}
return &p
}
// forgetOldOAuthFlows removes sign-ins never finished.
func (s *Server) forgetOldOAuthFlows() {
rows, err := s.DB.Query(`SELECT key, value FROM settings WHERE key LIKE 'oauth:flow:%'`)
if err != nil {
return
}
var old []string
for rows.Next() {
var key, raw string
var p oauth.Pending
if rows.Scan(&key, &raw) == nil && (json.Unmarshal([]byte(raw), &p) != nil || time.Since(p.Started) > oauthTTL) {
old = append(old, key)
}
}
rows.Close()
for _, k := range old {
s.DB.Exec(`DELETE FROM settings WHERE key = ?`, k)
}
}
func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
provider := r.PathValue("provider")
q := r.URL.Query()
s.mu.Lock()
p := s.pending[q.Get("state")]
delete(s.pending, q.Get("state")) // a state is good for one callback
s.mu.Unlock()
p := s.takeOAuthFlow(q.Get("state"))
back, nav := "/settings/accounts", "settings/accounts"
if provider == "google" {
back, nav = "/settings/search", "settings/search"