-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (67 loc) · 1.31 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-redis/redis"
)
const (
interval = 1 * time.Second
keyName = "acme:queue"
)
var (
redisAddress string
client *redis.Client
)
// this pushes new items onto a stack on a random cycle
func main() {
flag.StringVar(&redisAddress, "redis", "localhost:6379", "Connection to redis")
flag.Parse()
rand.Seed(time.Now().Unix())
cancel := make(chan os.Signal)
signal.Notify(cancel, os.Interrupt, syscall.SIGTERM)
err := setupClient()
if err != nil {
fmt.Printf("Failed to connect to Redis on %s due to %s\n", redisAddress, err.Error())
os.Exit(1)
}
ticker := time.NewTicker(interval)
for {
select {
case <-cancel:
fmt.Println("Leaving...")
ticker.Stop()
os.Exit(1)
case <-ticker.C:
fmt.Println("Buying...")
err := buy()
if err != nil {
fmt.Println("Nothing to buy")
}
}
}
}
func setupClient() error {
client = redis.NewClient(&redis.Options{
Addr: redisAddress,
Password: "",
DB: 0,
})
fmt.Printf("Connecting to Redis on %s\n", redisAddress)
_, err := client.Ping().Result()
return err
}
func buy() error {
count := rand.Intn(10)
for i := 0; i < count; i++ {
_, err := client.LPop(keyName).Result()
if err != nil {
return err
}
}
return nil
}