forked from contribsys/faktory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
191 lines (168 loc) · 3.62 KB
/
repl.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
187
188
189
190
191
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/signal"
"strings"
"syscall"
"github.com/contribsys/faktory"
"github.com/contribsys/faktory/cli"
"github.com/contribsys/faktory/storage"
"github.com/contribsys/faktory/util"
"github.com/contribsys/gorocksdb"
)
const helpMsg = `Valid commands:
flush
backup
restore *
repair *
version
help
* Requires an immediate restart after running command.`
var versionMsg = fmt.Sprintf("Faktory %s, RocksDB %s\n", faktory.Version, gorocksdb.RocksDBVersion())
// The REPL provides a few admin commands outside of Faktory itself,
// notably the backup and restore commands.
func main() {
opts := cli.ParseArguments()
args := flag.Args()
interactive := len(args) == 0
// This takes over the default logger in `log` and gives us
// extra powers for adding fields, errors to log output.
util.InitLogger("warn")
store, err := storage.Open("rocksdb", opts.StorageDirectory)
if err != nil {
fmt.Println("Unable to open storage:", err.Error())
fmt.Println(`Run "db repair" to attempt repair`)
}
if interactive {
repl(opts.StorageDirectory, store)
if store != nil {
store.Close()
}
} else {
go handleSignals(func() {
if store != nil {
store.Close()
}
os.Exit(0)
})
err := execute(args, store, opts.StorageDirectory)
if err != nil {
fmt.Println(err)
}
if store != nil {
store.Close()
}
if err != nil {
os.Exit(-1)
}
}
}
func repl(path string, store storage.Store) {
fmt.Printf("Using RocksDB %s at %s\n", gorocksdb.RocksDBVersion(), path)
rdr := bufio.NewReader(os.Stdin)
for {
fmt.Printf("> ")
bytes, _, er := rdr.ReadLine()
if er != nil {
if io.EOF == er {
fmt.Println("")
if store != nil {
store.Close()
}
os.Exit(0)
}
fmt.Printf("Error: %s\n", er.Error())
continue
}
line := string(bytes)
cmd := strings.Split(line, " ")
err := execute(cmd, store, path)
if err != nil {
fmt.Println(err)
}
}
}
func execute(cmd []string, store storage.Store, path string) error {
first := cmd[0]
switch first {
case "exit":
return nil
case "quit":
return nil
case "version":
fmt.Printf(versionMsg)
case "help":
fmt.Println(helpMsg)
case "flush":
return flush(store)
case "backup":
return backup(store)
case "repair":
return repair(store, path)
case "purge":
return purge(store)
case "restore":
return restore(store)
default:
return fmt.Errorf("Unknown command: %v", cmd)
}
return nil
}
func flush(store storage.Store) error {
if err := store.Flush(); err != nil {
return err
}
fmt.Println("OK")
return nil
}
func backup(store storage.Store) error {
if err := store.Backup(); err != nil {
return err
}
fmt.Println("Backup created")
store.EachBackup(func(x storage.BackupInfo) {
fmt.Printf("%+v\n", x)
})
return nil
}
func repair(store storage.Store, path string) error {
if store != nil {
store.Close()
}
opts := storage.DefaultOptions()
if err := gorocksdb.RepairDb(path, opts); err != nil {
return err
}
fmt.Println("Repair complete, restart required")
os.Exit(0)
return nil
}
func purge(store storage.Store) error {
if err := store.PurgeOldBackups(storage.DefaultKeepBackupsCount); err != nil {
return err
}
fmt.Println("OK")
return nil
}
func restore(store storage.Store) error {
if err := store.RestoreFromLatest(); err != nil {
return err
}
fmt.Println("Restoration complete, restart required")
os.Exit(0)
return nil
}
func handleSignals(fn func()) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
signal.Notify(signals, os.Interrupt)
for {
sig := <-signals
util.Debugf("Received signal: %v", sig)
fn()
}
}