forked from pivotal-cf/cf-redis-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_controller.go
186 lines (155 loc) · 4.53 KB
/
process_controller.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package redis
import (
"errors"
"fmt"
"net"
"os"
"regexp"
"strconv"
"time"
"code.cloudfoundry.org/lager"
"github.com/BooleanCat/igo/ios/iexec"
"github.com/pivotal-cf/cf-redis-broker/redis/client"
)
const redisStartTimeout time.Duration = 10 * time.Second
type ProcessChecker interface {
Alive(pid int) bool
}
type ProcessKiller interface {
Kill(pid int) error
}
type InstanceInformer interface {
InstancePid(string) (int, error)
}
type OSProcessController struct {
Logger lager.Logger
InstanceInformer InstanceInformer
ProcessChecker ProcessChecker
ProcessKiller ProcessKiller
PingFunc PingServerFunc
WaitUntilConnectableFunc WaitUntilConnectableFunc
RedisServerExecutablePath string
exec iexec.Exec
}
func NewOSProcessController(
logger lager.Logger,
instanceInformer InstanceInformer,
processChecker ProcessChecker,
processKiller ProcessKiller,
pingFunc PingServerFunc,
waitUntilConnectableFunc WaitUntilConnectableFunc,
redisServerExecutablePath string,
) *OSProcessController {
return &OSProcessController{
Logger: logger,
InstanceInformer: instanceInformer,
ProcessChecker: processChecker,
ProcessKiller: processKiller,
PingFunc: pingFunc,
WaitUntilConnectableFunc: waitUntilConnectableFunc,
RedisServerExecutablePath: redisServerExecutablePath,
exec: iexec.New(),
}
}
func PingServer(instance *Instance) error {
client, err := client.Connect(
client.Host(instance.Host),
client.Port(instance.Port),
client.Password(instance.Password),
)
if err != nil {
return err
}
return client.Ping()
}
type PingServerFunc func(instance *Instance) error
type WaitUntilConnectableFunc func(address *net.TCPAddr, timeout time.Duration) error
func (controller *OSProcessController) StartAndWaitUntilReady(instance *Instance, configPath, instanceDataDir, logfilePath string, timeout time.Duration) error {
instanceCommandArgs := []string{
configPath,
"--dir", instanceDataDir,
"--logfile", logfilePath,
}
return controller.StartAndWaitUntilReadyWithConfig(instance, instanceCommandArgs, timeout)
}
func (controller *OSProcessController) StartAndWaitUntilReadyWithConfig(instance *Instance, instanceCommandArgs []string, timeout time.Duration) error {
executable := "redis-server"
if controller.RedisServerExecutablePath != "" {
executable = controller.RedisServerExecutablePath
}
err := controller.exec.Command(executable, instanceCommandArgs...).Run()
if err != nil {
return fmt.Errorf("redis failed to start: %s", err)
}
return controller.WaitUntilConnectableFunc(instance.Address(), timeout)
}
func (controller *OSProcessController) Kill(instance *Instance) error {
pid, err := controller.InstanceInformer.InstancePid(instance.ID)
if err != nil {
return err
}
return controller.ProcessKiller.Kill(pid)
}
func (controller *OSProcessController) EnsureRunning(instance *Instance, configPath, instanceDataDir, pidfilePath, logfilePath string) error {
pid, err := controller.InstanceInformer.InstancePid(instance.ID)
if err == nil && controller.ProcessChecker.Alive(pid) {
pingErr := controller.PingFunc(instance)
if pingErr == nil {
controller.Logger.Info(
"redis instance already running",
lager.Data{"instance": instance.ID},
)
return nil
}
controller.Logger.Error(
"Failed to PING redis-server",
err,
lager.Data{"instance": instance.ID},
)
deleteErr := os.Remove(pidfilePath)
if deleteErr != nil {
controller.Logger.Error(
"failed to delete stale pidfile",
err,
lager.Data{"instance": instance.ID},
)
return deleteErr
}
controller.Logger.Info(
"removed stale pidfile",
lager.Data{"instance": instance.ID},
)
return controller.StartAndWaitUntilReady(instance, configPath, instanceDataDir, logfilePath, redisStartTimeout)
}
if err != nil {
controller.Logger.Error(
"redis instance has no pidfile",
err,
lager.Data{
"instance": instance.ID,
},
)
}
controller.Logger.Info(
"redis instance is not running",
lager.Data{
"instance": instance.ID,
},
)
return controller.StartAndWaitUntilReady(instance, configPath, instanceDataDir, logfilePath, redisStartTimeout)
}
func getRedisPort(name string) (int, error) {
regex, err := regexp.Compile("\\d+$")
if err != nil {
return 0, err
}
portStr := regex.FindString(name)
if portStr == "" {
return 0, errors.New("failed to get port")
}
port, err := strconv.Atoi(string(portStr))
if err != nil {
return 0, err
}
return port, nil
}