handle bots as background process;

This commit is contained in:
naudachu 2023-11-22 16:07:27 +05:00
parent e668e0b50d
commit 5c54d10556
1 changed files with 40 additions and 21 deletions

View File

@ -17,7 +17,9 @@ import (
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib" "github.com/jackc/pgx/v5/stdlib"
"github.com/pkg/errors"
migrate "github.com/rubenv/sql-migrate" migrate "github.com/rubenv/sql-migrate"
"golang.org/x/sync/errgroup"
) )
const ( const (
@ -34,6 +36,19 @@ func main() {
run(config) run(config)
} }
func Go(ctx context.Context, fns ...func(context.Context) error) error {
group, ctx := errgroup.WithContext(ctx)
for _, fn := range fns {
fn := fn
group.Go(func() error {
return fn(ctx)
})
}
return group.Wait()
}
func run(conf domain.Config) { func run(conf domain.Config) {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM)
defer cancel() defer cancel()
@ -87,16 +102,18 @@ func run(conf domain.Config) {
conn, conn,
) )
go func() { Go(ctx,
func(ctx context.Context) error {
opts := discord.DiscordOptions{ opts := discord.DiscordOptions{
Controller: controller, Controller: controller,
Config: &conf, Config: &conf,
} }
if err := discord.Run(conf, opts); err != nil { if err := discord.Run(conf, opts); err != nil {
log.Fatalf("discord bot cannot be runned: %v", err) return errors.Errorf("discord bot cannot be runned: %v", err)
} }
}() return nil
},
func(ctx context.Context) error {
opts := telegram.TelegramOptions{ opts := telegram.TelegramOptions{
GitService: gitService, GitService: gitService,
CloudService: cloudService, CloudService: cloudService,
@ -106,7 +123,9 @@ func run(conf domain.Config) {
} }
if err := telegram.Run(ctx, opts); err != nil { if err := telegram.Run(ctx, opts); err != nil {
log.Fatalf("telegram bot cannot be runned: %v", err) return errors.Errorf("telegram bot cannot be runned: %v", err)
defer os.Exit(1)
} }
return nil
},
)
} }