handle bots as background process;
This commit is contained in:
parent
e668e0b50d
commit
5c54d10556
61
cmd/main.go
61
cmd/main.go
|
|
@ -17,7 +17,9 @@ import (
|
|||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pkg/errors"
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -34,6 +36,19 @@ func main() {
|
|||
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) {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
|
@ -87,26 +102,30 @@ func run(conf domain.Config) {
|
|||
conn,
|
||||
)
|
||||
|
||||
go func() {
|
||||
opts := discord.DiscordOptions{
|
||||
Controller: controller,
|
||||
Config: &conf,
|
||||
}
|
||||
if err := discord.Run(conf, opts); err != nil {
|
||||
log.Fatalf("discord bot cannot be runned: %v", err)
|
||||
}
|
||||
}()
|
||||
Go(ctx,
|
||||
func(ctx context.Context) error {
|
||||
opts := discord.DiscordOptions{
|
||||
Controller: controller,
|
||||
Config: &conf,
|
||||
}
|
||||
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{
|
||||
GitService: gitService,
|
||||
CloudService: cloudService,
|
||||
Coda: codaService,
|
||||
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)
|
||||
}
|
||||
if err := telegram.Run(ctx, opts); err != nil {
|
||||
return errors.Errorf("telegram bot cannot be runned: %v", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue