Skip to content

Commit

Permalink
fix(workers): fallback to default client
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Dec 13, 2023
1 parent 2c5e873 commit 42a9c36
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
9 changes: 7 additions & 2 deletions cmd/fsb/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ func runApp(cmd *cobra.Command, args []string) {
config.Load(log, cmd)
router := getRouter(log)

_, err := bot.StartClient(log)
mainBot, err := bot.StartClient(log)
if err != nil {
log.Info(err.Error())
return
}
cache.InitCache(log)
bot.StartWorkers(log)
workers, err := bot.StartWorkers(log)
if err != nil {
log.Panic("Failed to start workers", zap.Error(err))
return
}
workers.AddDefaultClient(mainBot, mainBot.Self)
bot.StartUserBot(log)
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
Expand Down
23 changes: 14 additions & 9 deletions internal/bot/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (w *BotWorkers) AddDefaultClient(client *gotgproto.Client, self *tg.User) {
Client: client,
ID: w.starting,
Self: self,
log: w.log,
})
w.log.Sugar().Info("Default bot loaded")
}
Expand Down Expand Up @@ -90,17 +91,20 @@ func GetNextWorker() *Worker {
return worker
}

func StartWorkers(log *zap.Logger) {
log.Sugar().Info("Starting workers")

func StartWorkers(log *zap.Logger) (*BotWorkers, error) {
Workers.Init(log)

if len(config.ValueOf.MultiTokens) == 0 {
Workers.log.Sugar().Info("No worker bot tokens provided, skipping worker initialization")
return Workers, nil
}
Workers.log.Sugar().Info("Starting")
if config.ValueOf.UseSessionFile {
log.Sugar().Info("Using session file for workers")
Workers.log.Sugar().Info("Using session file for workers")
newpath := filepath.Join(".", "sessions")
if err := os.MkdirAll(newpath, os.ModePerm); err != nil {
log.Error("Failed to create sessions directory", zap.Error(err))
return
Workers.log.Error("Failed to create sessions directory", zap.Error(err))
return nil, err
}
}

Expand All @@ -125,18 +129,19 @@ func StartWorkers(log *zap.Logger) {
select {
case err := <-done:
if err != nil {
log.Error("Failed to start worker", zap.Int("Worker Index", i), zap.Error(err))
Workers.log.Error("Failed to start worker", zap.Int("Worker Index", i), zap.Error(err))
} else {
atomic.AddInt32(&successfulStarts, 1)
}
case <-ctx.Done():
log.Error("Timed out starting worker", zap.Int("Worker Index", i))
Workers.log.Error("Timed out starting worker", zap.Int("Worker Index", i))
}
}(i)
}

wg.Wait() // Wait for all goroutines to finish
log.Sugar().Infof("Successfully started %d/%d bots", successfulStarts, totalBots)
Workers.log.Sugar().Infof("Successfully started %d/%d bots", successfulStarts, totalBots)
return Workers, nil
}

func startWorker(l *zap.Logger, botToken string, index int) (*gotgproto.Client, error) {
Expand Down

0 comments on commit 42a9c36

Please sign in to comment.