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:
@@ -479,7 +479,7 @@ func (s *Server) importSource(w http.ResponseWriter, r *http.Request) {
|
||||
// without one.
|
||||
func (s *Server) runImport(ctx context.Context, source string) (*leadsource.Result, error) {
|
||||
// One import per tool at a time, across copies of the app.
|
||||
ctx, release, err := s.jobs().TryLock(ctx, "import:"+source)
|
||||
ctx, release, err := s.jobs().TryLock(ctx, s.lockName("import:"+source))
|
||||
if errors.Is(err, jobs.ErrBusy) {
|
||||
return nil, leadsource.ErrRunning
|
||||
} else if err != nil {
|
||||
@@ -506,7 +506,7 @@ func (s *Server) RunImports(ctx context.Context) {
|
||||
tick := time.NewTicker(10 * time.Minute)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
s.jobDo(ctx, "imports", func(ctx context.Context) {
|
||||
s.jobDo(ctx, "imports", func(ctx context.Context, s *Server) {
|
||||
for _, src := range []string{signals.SourceOrigami, signals.SourceExplee} {
|
||||
last, _ := leadsource.Last(s.DB, src)
|
||||
if last != nil && time.Since(last.At) < leadsource.Every {
|
||||
@@ -587,7 +587,7 @@ func (s *Server) discover(sig *signals.Signal, source string) error {
|
||||
return errors.New("unknown source")
|
||||
}
|
||||
// One search per signal and source at a time, across copies of the app.
|
||||
lctx, release, err := s.jobs().TryLock(context.Background(), fmt.Sprintf("discover:%d:%s", sig.ID, source))
|
||||
lctx, release, err := s.jobs().TryLock(context.Background(), s.lockName(fmt.Sprintf("discover:%d:%s", sig.ID, source)))
|
||||
if errors.Is(err, jobs.ErrBusy) {
|
||||
return discovery.ErrRunning
|
||||
} else if err != nil {
|
||||
@@ -660,9 +660,11 @@ func (s *Server) signalRuns(w http.ResponseWriter, r *http.Request) {
|
||||
// RunDiscovery searches for signals that ask to be searched on a schedule.
|
||||
func (s *Server) RunDiscovery(ctx context.Context) {
|
||||
if s.alone() {
|
||||
if err := discovery.RecoverInterrupted(s.DB); err != nil {
|
||||
s.Log.Error("discovery: recovering interrupted runs", "err", err)
|
||||
}
|
||||
s.eachTeam(ctx, func(_ context.Context, s *Server) {
|
||||
if err := discovery.RecoverInterrupted(s.DB); err != nil {
|
||||
s.Log.Error("discovery: recovering interrupted runs", "err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
tick := time.NewTicker(30 * time.Minute)
|
||||
defer tick.Stop()
|
||||
@@ -672,7 +674,7 @@ func (s *Server) RunDiscovery(ctx context.Context) {
|
||||
return
|
||||
case <-tick.C:
|
||||
}
|
||||
s.jobDo(ctx, "discovery", func(ctx context.Context) {
|
||||
s.jobDo(ctx, "discovery", func(ctx context.Context, s *Server) {
|
||||
list, err := signals.List(s.DB)
|
||||
if err != nil {
|
||||
s.Log.Error("discovery: listing signals", "err", err)
|
||||
|
||||
Reference in New Issue
Block a user