-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (75 loc) · 2.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"hldsbot/bot"
"hldsbot/hlds"
"os"
"os/signal"
"sync"
"syscall"
"time"
docker "github.com/docker/docker/client"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
})
log.Info().Msg("Starting HLDSBot.")
dockerClient, err := docker.NewClientWithOpts(docker.FromEnv)
if err != nil {
log.Fatal().Err(err).Msg("unable to obtain Docker client")
}
defer func() {
if err := dockerClient.Close(); err != nil {
log.Error().Err(err).Msg("unable to close Docker client")
}
}()
pool, err := hlds.NewPool(
dockerClient, 2, 27015,
os.Getenv("HLDSBOT_BASE_DOWNLOAD_URL"),
)
if err != nil {
log.Fatal().Err(err).Msg("unable to init hlds.Pool")
}
bot, err := bot.New(
os.Getenv("HLDSBOT_DISCORD_TOKEN"),
os.Getenv("HLDSBOT_STEAM_REDIRECT_URL"),
pool,
)
if err != nil {
log.Fatal().Err(err).Msg("unable to init discord bot")
}
dispatch(context.Background(), pool, bot)
log.Info().Msg("HLDSBot shutdown complete. ")
}
// Run all background processes and cleanup everything as soon as one of them
// closes or we're signaled to exit.
func dispatch(ctx context.Context, pool *hlds.Pool, bot *bot.Bot) {
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
wrap(ctx, cancel, pool.Run, &wg)
wrap(ctx, cancel, bot.Run, &wg)
<-ctx.Done()
log.Info().Err(ctx.Err()).Msg("Shutting down.")
wg.Wait()
}
type proc func(context.Context) error
// Wrap a background call with all the inter-cancellation shenanigans.
// If the context is canceled or the proc terminates, the cancelation will be
// propagated to all other procs launched by this func.
func wrap(ctx context.Context, cancel func(), f proc, wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer wg.Done()
if err := f(ctx); err != nil {
log.Error().Err(err).Msg("proc closed unexpectedly")
}
cancel()
}()
}