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:
+16
-1
@@ -42,6 +42,7 @@ import (
|
||||
"supermarket/internal/crm"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/fileutil"
|
||||
"supermarket/internal/jobs"
|
||||
"supermarket/internal/legacy"
|
||||
"supermarket/internal/mcpserver"
|
||||
"supermarket/internal/secrets"
|
||||
@@ -207,7 +208,19 @@ func run(log *slog.Logger) error {
|
||||
log.Info("database: migrated", "database", database.Dialect, "from", database.MigratedFrom, "to", db.Schema())
|
||||
}
|
||||
|
||||
if n, err := content.RecoverInterrupted(database); err != nil {
|
||||
// This copy of the app, for the locks it shares with any others.
|
||||
runner, err := jobs.New(database, version, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer runner.Stop() // before the database closes
|
||||
alone, err := runner.Alone()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !alone {
|
||||
log.Info("jobs: other copies of Supermarket are running; work they have under way is left to them")
|
||||
} else if n, err := content.RecoverInterrupted(database); err != nil {
|
||||
return err
|
||||
} else if n > 0 {
|
||||
log.Warn("drafts were being published when Supermarket stopped; marked failed for review", "count", n)
|
||||
@@ -279,6 +292,7 @@ func run(log *slog.Logger) error {
|
||||
Core: c,
|
||||
DataDir: dataDir,
|
||||
Files: files,
|
||||
Jobs: runner,
|
||||
PublicURL: publicURL,
|
||||
Version: version,
|
||||
MCPToken: token,
|
||||
@@ -310,6 +324,7 @@ func run(log *slog.Logger) error {
|
||||
errCh <- fmt.Errorf("MCP socket %s: %w", mcpSocket, err)
|
||||
}
|
||||
}()
|
||||
go runner.CheckIns(ctx)
|
||||
go srv.RunScheduler(ctx)
|
||||
go srv.RunImports(ctx)
|
||||
go srv.RunDiscovery(ctx)
|
||||
|
||||
Reference in New Issue
Block a user