-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (64 loc) · 1.8 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
package main
import (
"context"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/ably/ably-go/ably"
"github.com/go-redis/redis/v8"
"github.com/go-redsync/redsync/v4"
"github.com/go-redsync/redsync/v4/redis/goredis/v8"
"github.com/joho/godotenv"
"golang.org/x/sync/errgroup"
"hxann.com/tic-tac-toe-worker/shared"
"hxann.com/tic-tac-toe-worker/ticker"
"hxann.com/tic-tac-toe-worker/worker"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Println("Not loading .env file. Assuming env vars are available.")
}
log.Println("Starting")
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
g, gCtx := errgroup.WithContext(ctx)
// Redis
opt, err := redis.ParseURL(os.Getenv("REDIS_URL"))
if err != nil {
panic(err)
}
rdb := redis.NewClient(opt)
gCtx = context.WithValue(gCtx, shared.RedisCtxKey{}, rdb)
// Ably
ablyApiKey := os.Getenv("ABLY_API_KEY")
ablyClient, err := ably.NewRealtime(ably.WithKey(ablyApiKey))
if err != nil {
panic(err)
}
gCtx = context.WithValue(gCtx, shared.AblyCtxKey{}, ablyClient)
pool := goredis.NewPool(rdb)
rs := redsync.New(pool)
gCtx = context.WithValue(gCtx, shared.RedsyncCtxKey{}, rs)
// Starting a worker for listening for messages on the queue.
// A worker spins up multiple goroutines to handle messages.
g.Go(worker.New(gCtx))
// Starting tickers for ticking the rooms
// Getting number of tickers
numTickersStr := os.Getenv("NUMBER_OF_TICKERS")
numTickers, err := strconv.Atoi(numTickersStr)
if err != nil {
log.Println("Could not parse number of tickers. Using default value of 1.")
numTickers = 1
}
// Start tickers
for i := 0; i < numTickers; i++ {
g.Go(ticker.New(gCtx))
}
err = g.Wait()
if err != nil {
log.Println("Error group: ", err)
}
log.Println("Exiting")
}