|
0 |
package tempredis
|
|
1 |
|
|
2 |
import (
|
|
3 |
"bufio"
|
|
4 |
"bytes"
|
|
5 |
"fmt"
|
|
6 |
"io"
|
|
7 |
"io/ioutil"
|
|
8 |
"os"
|
|
9 |
"os/exec"
|
|
10 |
"strings"
|
|
11 |
"syscall"
|
|
12 |
)
|
|
13 |
|
|
14 |
const (
|
|
15 |
// ready is the string redis-server prints to stdout after starting
|
|
16 |
// successfully.
|
|
17 |
ready = "The server is now ready to accept connections"
|
|
18 |
)
|
|
19 |
|
|
20 |
// Server encapsulates the configuration, starting, and stopping of a single
|
|
21 |
// redis-server process that is reachable via a local Unix socket.
|
|
22 |
type Server struct {
|
|
23 |
dir string
|
|
24 |
config Config
|
|
25 |
cmd *exec.Cmd
|
|
26 |
stdout io.Reader
|
|
27 |
stdoutBuf bytes.Buffer
|
|
28 |
stderr io.Reader
|
|
29 |
}
|
|
30 |
|
|
31 |
// Start initiates a new redis-server process configured with the given
|
|
32 |
// configuration. redis-server will listen on a temporary local Unix socket. An
|
|
33 |
// error is returned if redis-server is unable to successfully start for any
|
|
34 |
// reason.
|
|
35 |
func Start(config Config) (server *Server, err error) {
|
|
36 |
if config == nil {
|
|
37 |
config = Config{}
|
|
38 |
}
|
|
39 |
|
|
40 |
dir, err := ioutil.TempDir(os.TempDir(), "tempredis")
|
|
41 |
if err != nil {
|
|
42 |
return nil, err
|
|
43 |
}
|
|
44 |
|
|
45 |
if _, ok := config["unixsocket"]; !ok {
|
|
46 |
config["unixsocket"] = fmt.Sprintf("%s/%s", dir, "redis.sock")
|
|
47 |
}
|
|
48 |
if _, ok := config["port"]; !ok {
|
|
49 |
config["port"] = "0"
|
|
50 |
}
|
|
51 |
|
|
52 |
server = &Server{
|
|
53 |
dir: dir,
|
|
54 |
config: config,
|
|
55 |
}
|
|
56 |
err = server.start()
|
|
57 |
if err != nil {
|
|
58 |
return server, err
|
|
59 |
}
|
|
60 |
|
|
61 |
// Block until Redis is ready to accept connections.
|
|
62 |
err = server.waitFor(ready)
|
|
63 |
|
|
64 |
return server, err
|
|
65 |
}
|
|
66 |
|
|
67 |
func (s *Server) start() (err error) {
|
|
68 |
if s.cmd != nil {
|
|
69 |
return fmt.Errorf("redis-server has already been started")
|
|
70 |
}
|
|
71 |
|
|
72 |
s.cmd = exec.Command("redis-server", "-")
|
|
73 |
|
|
74 |
stdin, _ := s.cmd.StdinPipe()
|
|
75 |
s.stdout, _ = s.cmd.StdoutPipe()
|
|
76 |
s.stderr, _ = s.cmd.StderrPipe()
|
|
77 |
|
|
78 |
err = s.cmd.Start()
|
|
79 |
if err == nil {
|
|
80 |
err = writeConfig(s.config, stdin)
|
|
81 |
}
|
|
82 |
|
|
83 |
return err
|
|
84 |
}
|
|
85 |
|
|
86 |
func writeConfig(config Config, w io.WriteCloser) (err error) {
|
|
87 |
for key, value := range config {
|
|
88 |
if value == "" {
|
|
89 |
value = "\"\""
|
|
90 |
}
|
|
91 |
_, err = fmt.Fprintf(w, "%s %s\n", key, value)
|
|
92 |
if err != nil {
|
|
93 |
return err
|
|
94 |
}
|
|
95 |
}
|
|
96 |
return w.Close()
|
|
97 |
}
|
|
98 |
|
|
99 |
// waitFor blocks until redis-server prints the given string to stdout.
|
|
100 |
func (s *Server) waitFor(search string) (err error) {
|
|
101 |
var line string
|
|
102 |
|
|
103 |
scanner := bufio.NewScanner(s.stdout)
|
|
104 |
for scanner.Scan() {
|
|
105 |
line = scanner.Text()
|
|
106 |
fmt.Fprintf(&s.stdoutBuf, "%s\n", line)
|
|
107 |
if strings.Contains(line, search) {
|
|
108 |
return nil
|
|
109 |
}
|
|
110 |
}
|
|
111 |
err = scanner.Err()
|
|
112 |
if err == nil {
|
|
113 |
err = io.EOF
|
|
114 |
}
|
|
115 |
return err
|
|
116 |
}
|
|
117 |
|
|
118 |
// Socket returns the full path to the local redis-server Unix socket.
|
|
119 |
func (s *Server) Socket() string {
|
|
120 |
return s.config.Socket()
|
|
121 |
}
|
|
122 |
|
|
123 |
// Stdout blocks until redis-server returns and then returns the full stdout
|
|
124 |
// output.
|
|
125 |
func (s *Server) Stdout() string {
|
|
126 |
io.Copy(&s.stdoutBuf, s.stdout)
|
|
127 |
return s.stdoutBuf.String()
|
|
128 |
}
|
|
129 |
|
|
130 |
// Stderr blocks until redis-server returns and then returns the full stdout
|
|
131 |
// output.
|
|
132 |
func (s *Server) Stderr() string {
|
|
133 |
bytes, _ := ioutil.ReadAll(s.stderr)
|
|
134 |
return string(bytes)
|
|
135 |
}
|
|
136 |
|
|
137 |
// Term gracefully shuts down redis-server. It returns an error if redis-server
|
|
138 |
// fails to terminate.
|
|
139 |
func (s *Server) Term() (err error) {
|
|
140 |
return s.signalAndCleanup(syscall.SIGTERM)
|
|
141 |
}
|
|
142 |
|
|
143 |
// Kill forcefully shuts down redis-server. It returns an error if redis-server
|
|
144 |
// fails to die.
|
|
145 |
func (s *Server) Kill() (err error) {
|
|
146 |
return s.signalAndCleanup(syscall.SIGKILL)
|
|
147 |
}
|
|
148 |
|
|
149 |
func (s *Server) signalAndCleanup(sig syscall.Signal) error {
|
|
150 |
s.cmd.Process.Signal(sig)
|
|
151 |
_, err := s.cmd.Process.Wait()
|
|
152 |
os.RemoveAll(s.dir)
|
|
153 |
return err
|
|
154 |
}
|