-
Notifications
You must be signed in to change notification settings - Fork 4
/
redis.go
150 lines (130 loc) · 3.13 KB
/
redis.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package test
import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
"github.com/fsouza/go-dockerclient"
)
const (
redisChkTimes = 10
redisChkDelay = 1 * time.Second
)
func init() {
RegisterService(Redis, func() Service {
return &redisService{
maxMemory: "64mb",
port: 6379,
}
})
}
type redisService struct {
port int
workDir string
auth string
maxMemory string
container *docker.Container
}
func (s *redisService) Start() (string, error) {
// perform default check
if err := CheckExecutable("redis-server", "redis-cli"); err != nil {
return "", err
}
// booking 1 ports
ports, err := BookPorts(1)
if err != nil {
return "", fmt.Errorf("fail to book ports, err:%v", err)
}
s.port = ports[0]
// prepare tmp dir
s.workDir, err = ioutil.TempDir("", "redis-test")
if err != nil {
return "", fmt.Errorf("fail to prepare tmp dir, err:%v", err)
}
pidFile := filepath.Join(s.workDir, "redis.pid")
logFile := filepath.Join(s.workDir, "redis.log")
cmds := []interface{}{
"--daemonize", "yes",
"--port", s.port,
"--pidfile", pidFile,
"--logfile", logFile,
"--dir", s.workDir,
"--maxmemory", s.maxMemory,
}
if s.auth != "" {
cmds = append(cmds, "--requirepass", s.auth)
}
if err := Exec(s.workDir, nil, nil, "redis-server", cmds...); err != nil {
return "", fmt.Errorf("fail to start redis server, err:%v", err)
}
for i := 0; i < redisChkTimes; i++ {
time.Sleep(redisChkDelay)
if CheckListening(s.port) {
return fmt.Sprintf("localhost:%d", s.port), nil
}
}
// only need region server thrift port
return "", fmt.Errorf("fail to start redis")
}
func (s *redisService) Stop() error {
// close process
return Exec(
s.workDir, nil, nil,
"redis-cli",
"-h", "localhost",
"-p", s.port,
"shutdown")
}
// StartDocker start the service via docker
func (s *redisService) StartDocker(cl *docker.Client) (ipport string, err error) {
Cmds := []string{
"redis-server",
"--port", fmt.Sprintf("%d", s.port),
"--maxmemory", s.maxMemory,
"--maxmemory-policy", "allkeys-lru", // TODO: default to allkeys-lru, will it change
}
if s.auth != "" {
Cmds = append(Cmds, "--requirepass", s.auth)
}
s.container, ipport, err = StartContainer(
cl,
SetImage("redis:3-alpine"),
SetExposedPorts([]string{fmt.Sprintf("%d/tcp", s.port)}),
SetCommand(Cmds),
)
return ipport, err
}
// StopDocker stops the service via docker
func (s *redisService) StopDocker(cl *docker.Client) error {
return RemoveContainer(cl, s.container)
}
func RedisMemory(maxMem string) ServiceOption {
return func(s Service) error {
rs, ok := s.(*redisService)
if !ok {
return fmt.Errorf("can't set redis auth with service %v", s)
}
rs.maxMemory = maxMem
return nil
}
}
func RedisPort(port int) ServiceOption {
return func(s Service) error {
rs, ok := s.(*redisService)
if !ok {
return fmt.Errorf("can't set redis port with service %v", s)
}
rs.port = port
return nil
}
}
func RedisAuth(password string) ServiceOption {
return func(s Service) error {
rs, ok := s.(*redisService)
if !ok {
return fmt.Errorf("can't set redis auth with service %v", s)
}
rs.auth = password
return nil
}
}