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:
@@ -13,8 +13,10 @@ import (
|
||||
"time"
|
||||
|
||||
"supermarket/internal/config"
|
||||
"supermarket/internal/content"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/posting"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
@@ -320,3 +322,39 @@ func TestProvingTheDomain(t *testing.T) {
|
||||
t.Errorf("file: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobsRunPerTeam(t *testing.T) {
|
||||
s, _ := newMultiEdition(t)
|
||||
sent := &fakePosts{}
|
||||
s.testPublisher = func(a *posting.Account) content.Publisher { return fakePub{sent, a} }
|
||||
var teams []*users.TeamInfo
|
||||
for _, name := range []string{"Acme", "Bolt", "Calm"} {
|
||||
tm, _, _ := newTeam(t, s, name, strings.ToLower(name)+"@x.example")
|
||||
teams = append(teams, tm)
|
||||
ts, err := s.forTeam(tm.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
acct := &posting.Account{Network: posting.X, Name: "@" + strings.ToLower(name), External: strings.ToLower(name)}
|
||||
posting.Save(ts.DB, ts.Secrets, acct, "t", "", time.Now())
|
||||
content.CreateDrafts(ts.DB, []content.Draft{{Channel: content.ChannelX, Content: name + " news"}})
|
||||
ds, _ := content.ListDrafts(ts.DB, 1, content.StatusDraft)
|
||||
content.Schedule(ts.DB, ds[0].ID, time.Now().Add(time.Minute))
|
||||
}
|
||||
// Calm is suspended: its work waits.
|
||||
users.SetTeamStatus(s.platform(), teams[2].ID, users.TeamSuspended, time.Time{})
|
||||
s.jobDo(context.Background(), "scheduler", func(ctx context.Context, ts *Server) { ts.publishDue(ctx, time.Now().Add(2*time.Minute)) })
|
||||
got := strings.Join(sent.sent, "|")
|
||||
if len(sent.sent) != 2 || !strings.Contains(got, "X @acme: Acme news") || !strings.Contains(got, "X @bolt: Bolt news") {
|
||||
t.Errorf("sent %v", sent.sent)
|
||||
}
|
||||
// Each team's lock is its own.
|
||||
ts, _ := s.forTeam(teams[0].ID)
|
||||
if ts.lockName("job:seo") == s.lockName("job:seo") || !strings.HasPrefix(ts.lockName("job:seo"), "team:") {
|
||||
t.Errorf("lock %s", ts.lockName("job:seo"))
|
||||
}
|
||||
// Its files are under its own prefix.
|
||||
if ts.Files != nil && !strings.Contains(ts.Files.String(), "teams/") {
|
||||
t.Errorf("files %s", ts.Files.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user