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,26 +102,30 @@ func run(conf domain.Config) {
conn, conn,
) )
go func() { Go(ctx,
opts := discord.DiscordOptions{ func(ctx context.Context) error {
Controller: controller, opts := discord.DiscordOptions{
Config: &conf, Controller: controller,
} Config: &conf,
if err := discord.Run(conf, opts); err != nil { }
log.Fatalf("discord bot cannot be runned: %v", err) if err := discord.Run(conf, opts); err != nil {
} return errors.Errorf("discord bot cannot be runned: %v", err)
}() }
return nil
},
func(ctx context.Context) error {
opts := telegram.TelegramOptions{
GitService: gitService,
CloudService: cloudService,
Coda: codaService,
AppConfig: &conf,
Controller: controller,
}
opts := telegram.TelegramOptions{ if err := telegram.Run(ctx, opts); err != nil {
GitService: gitService, return errors.Errorf("telegram bot cannot be runned: %v", err)
CloudService: cloudService, }
Coda: codaService, return nil
AppConfig: &conf, },
Controller: controller, )
}
if err := telegram.Run(ctx, opts); err != nil {
log.Fatalf("telegram bot cannot be runned: %v", err)
defer os.Exit(1)
}
} }