Skip to content

Commit

Permalink
feat(workers): add timeout when starting
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Nov 29, 2023
1 parent 1063889 commit c0f9000
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions internal/bot/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package bot

import (
"EverythingSuckz/fsb/config"
"context"
"fmt"
"os"
"path/filepath"
"sync"
"time"

"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/sessionMaker"
Expand Down Expand Up @@ -89,6 +91,9 @@ func GetNextWorker() *Worker {

func StartWorkers(log *zap.Logger) {
log.Sugar().Info("Starting workers")
timeOut := 30 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
Workers.Init(log)
if config.ValueOf.UseSessionFile {
log.Sugar().Info("Using session file for workers")
Expand All @@ -99,21 +104,30 @@ func StartWorkers(log *zap.Logger) {
return
}
}
c := make(chan struct{})
c := make(chan struct{}, len(config.ValueOf.MultiTokens))
for i := 0; i < len(config.ValueOf.MultiTokens); i++ {
go func(i int) {
err := Workers.Add(config.ValueOf.MultiTokens[i])
if err != nil {
log.Error("Failed to start worker", zap.Error(err))
return
}
c <- struct{}{}
select {
default:
c <- struct{}{}
case <-ctx.Done():
return
}
}(i)
}
// wait for all workers to start
log.Sugar().Info("Waiting for all workers to start")
for i := 0; i < len(config.ValueOf.MultiTokens); i++ {
<-c
select {
case <-c:
case <-time.After(timeOut):
log.Sugar().Warnf("Timed out waiting for worker %d to start", i)
}
}
}

Expand Down

0 comments on commit c0f9000

Please sign in to comment.