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:
+42
-7
@@ -2,6 +2,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"supermarket/internal/config"
|
||||
"supermarket/internal/jobs"
|
||||
"supermarket/internal/storage"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -27,7 +29,6 @@ import (
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/legacy"
|
||||
"supermarket/internal/metrics"
|
||||
"supermarket/internal/oauth"
|
||||
"supermarket/internal/prospects"
|
||||
"supermarket/internal/secrets"
|
||||
"supermarket/internal/seo"
|
||||
@@ -50,6 +51,7 @@ type Server struct {
|
||||
Core *core.Core // nil without a Core
|
||||
DataDir string
|
||||
Files storage.Store // exports and uploads: a folder or S3; nil in tests
|
||||
Jobs *jobs.Runner // this copy of the app, for locks shared with others; made on first use in tests
|
||||
PublicURL string // e.g. http://localhost:8470, for OAuth redirect URIs
|
||||
Version string
|
||||
MCPToken string
|
||||
@@ -64,8 +66,8 @@ type Server struct {
|
||||
|
||||
pages map[string]*template.Template
|
||||
|
||||
mu sync.Mutex
|
||||
pending map[string]*oauth.Pending // OAuth flows by state
|
||||
mu sync.Mutex
|
||||
jobsOnce sync.Once
|
||||
|
||||
// Reddit: an app-only token for reading rules before sign-in, and the
|
||||
// account's recent posts for the warnings.
|
||||
@@ -73,17 +75,50 @@ type Server struct {
|
||||
redditAppTokenExp time.Time
|
||||
redditHist []reddit.Submission
|
||||
redditHistAt time.Time
|
||||
|
||||
// sending lets one email go out at a time, from the queue or Send now.
|
||||
sending sync.Mutex
|
||||
}
|
||||
|
||||
// jobs is this copy of the app, for the locks it shares with the others.
|
||||
func (s *Server) jobs() *jobs.Runner {
|
||||
s.jobsOnce.Do(func() {
|
||||
if s.Jobs == nil {
|
||||
r, err := jobs.New(s.DB, s.Version, s.Log)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("jobs: %v", err)) // only without a Runner from main: tests
|
||||
}
|
||||
s.Jobs = r
|
||||
}
|
||||
})
|
||||
return s.Jobs
|
||||
}
|
||||
|
||||
// alone says whether no other copy of the app is running: only then is
|
||||
// work marked "running" really interrupted, and safe to mark so.
|
||||
func (s *Server) alone() bool {
|
||||
ok, err := s.jobs().Alone()
|
||||
if err != nil {
|
||||
s.Log.Warn("jobs: looking for other copies", "err", err)
|
||||
return false
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// jobDo runs one turn of a background job under its lock, job:<name>:
|
||||
// with several copies of the app, one of them does it.
|
||||
func (s *Server) jobDo(ctx context.Context, name string, fn func(context.Context)) {
|
||||
if _, err := s.jobs().Do(ctx, "job:"+name, fn); err != nil {
|
||||
s.Log.Warn("jobs: taking the lock", "job", name, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// sendLock is held while an email goes out, from the queue or Send now:
|
||||
// one at a time, across every copy of the app.
|
||||
const sendLock = "outreach:send"
|
||||
|
||||
// Handler builds the routes.
|
||||
func (s *Server) Handler() (http.Handler, error) {
|
||||
if err := s.loadTemplates(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.pending = map[string]*oauth.Pending{}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
static, _ := fs.Sub(staticFS, "static")
|
||||
|
||||
Reference in New Issue
Block a user