Phase 13, part 5: each team's jobs, locks, files and sign-ins
Background jobs run for each active team on its own Server and under its own locks, a few teams at a time and from a different team each round, so the work is shared between copies of the app and no team holds up another. Each team's files are under its own prefix, and an OAuth sign-in comes back only to the team that started it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+84
-4
@@ -92,7 +92,8 @@ type state struct {
|
||||
pages map[string]*template.Template
|
||||
jobsOnce sync.Once
|
||||
runner *jobs.Runner
|
||||
setupDone atomic.Bool // the team has its owner (server edition)
|
||||
setupDone atomic.Bool // the team has its owner (server edition)
|
||||
rotation atomic.Uint64 // which team's turn comes first (eachTeam)
|
||||
|
||||
mu sync.Mutex
|
||||
copies map[int64]*Server // each team's Server
|
||||
@@ -177,6 +178,9 @@ func (s *Server) forTeam(team int64) (*Server, error) {
|
||||
cp := *s
|
||||
cp.Team, cp.DB, cp.Secrets = team, d, sec
|
||||
cp.Platform = s.platform()
|
||||
if s.Files != nil {
|
||||
cp.Files = storage.Under(s.Files, fmt.Sprintf("teams/%d", team))
|
||||
}
|
||||
st.mu.Lock()
|
||||
defer st.mu.Unlock()
|
||||
if c := st.copies[team]; c != nil {
|
||||
@@ -218,10 +222,86 @@ func (s *Server) alone() bool {
|
||||
|
||||
// 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)
|
||||
//
|
||||
// On the multi-tenant edition it runs the turn for each active team, on
|
||||
// that team's Server, under the team's own lock (team:<id>:job:<name>), so
|
||||
// copies of the app share the teams between them. A few teams at a time,
|
||||
// starting from another one each round, so one team's long turn doesn't
|
||||
// hold up the others'. fn's Server is the team's.
|
||||
func (s *Server) jobDo(ctx context.Context, name string, fn func(context.Context, *Server)) {
|
||||
if !s.multi() {
|
||||
if _, err := s.jobs().Do(ctx, "job:"+name, func(c context.Context) { fn(c, s) }); err != nil {
|
||||
s.Log.Warn("jobs: taking the lock", "job", name, "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
s.eachTeam(ctx, func(ctx context.Context, ts *Server) {
|
||||
if name == "seo" && !ts.domainProved() {
|
||||
return // nothing crawled or read before the domain is proved
|
||||
}
|
||||
if _, err := ts.jobs().Do(ctx, ts.lockName("job:"+name), func(c context.Context) { fn(c, ts) }); err != nil {
|
||||
ts.Log.Warn("jobs: taking the lock", "job", name, "team", ts.team(), "err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// teamsAtOnce is how many teams' turns run together, in one copy.
|
||||
const teamsAtOnce = 4
|
||||
|
||||
// eachTeam runs fn for every active team (multi-tenant), a few at once,
|
||||
// from a different team each time.
|
||||
func (s *Server) eachTeam(ctx context.Context, fn func(context.Context, *Server)) {
|
||||
if !s.multi() {
|
||||
fn(ctx, s)
|
||||
return
|
||||
}
|
||||
list, err := users.ListTeams(s.platform())
|
||||
if err != nil {
|
||||
s.Log.Error("jobs: listing teams", "err", err)
|
||||
return
|
||||
}
|
||||
var ids []int64
|
||||
for _, t := range list {
|
||||
if t.Status == users.TeamActive {
|
||||
ids = append(ids, t.ID)
|
||||
}
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
start := int(s.st().rotation.Add(1) % uint64(len(ids)))
|
||||
sem := make(chan struct{}, teamsAtOnce)
|
||||
var wg sync.WaitGroup
|
||||
for i := range ids {
|
||||
id := ids[(start+i)%len(ids)]
|
||||
ts, err := s.forTeam(id)
|
||||
if err != nil {
|
||||
s.Log.Error("jobs: opening a team", "team", id, "err", err)
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case sem <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
wg.Wait()
|
||||
return
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
defer func() { <-sem }()
|
||||
fn(ctx, ts)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// lockName is a lock's name for this team: shared by every copy of the
|
||||
// app, and apart from other teams' on the multi-tenant edition.
|
||||
func (s *Server) lockName(name string) string {
|
||||
if !s.multi() {
|
||||
return name
|
||||
}
|
||||
return fmt.Sprintf("team:%d:%s", s.team(), name)
|
||||
}
|
||||
|
||||
// sendLock is held while an email goes out, from the queue or Send now:
|
||||
|
||||
Reference in New Issue
Block a user