forked from contribsys/faktory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
148 lines (134 loc) · 2.82 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
package main
import (
"bufio"
"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"
)
/*
The REPL provides a few admin commands outside of Faktory itself,
noteably the backup and restore commands.
*/
func main() {
opts := cli.ParseArguments()
// 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`)
}
go handleSignals(func() {
if store != nil {
store.Close()
}
os.Exit(0)
})
repl(opts.StorageDirectory, store)
if store != nil {
store.Close()
}
}
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("")
store.Close()
os.Exit(0)
}
fmt.Printf("Error: %s\n", er.Error())
continue
}
line := string(bytes)
cmd := strings.Split(line, " ")
first := cmd[0]
var err error
switch first {
case "exit":
return
case "quit":
return
case "version":
fmt.Printf("Faktory %s, RocksDB %s\n", faktory.Version, gorocksdb.RocksDBVersion())
case "help":
fmt.Println(`Valid commands:
flush
backup
restore *
repair *
version
help
* Requires an immediate restart after running command.
`)
default:
err = db(store, path, line, first)
}
if err != nil {
fmt.Println(err)
}
}
}
func db(store storage.Store, path string, line string, cmd string) error {
switch cmd {
case "flush":
err := store.Flush()
if err == nil {
fmt.Println("OK")
}
return err
case "backup":
err := store.Backup()
if err == nil {
fmt.Println("Backup created")
store.EachBackup(func(x storage.BackupInfo) {
fmt.Printf("%+v\n", x)
})
}
return err
case "repair":
if store != nil {
store.Close()
}
opts := storage.DefaultOptions()
err := gorocksdb.RepairDb(path, opts)
if err == nil {
fmt.Println("Repair complete, restart required")
os.Exit(0)
}
return err
case "restore":
err := store.RestoreFromLatest()
if err == nil {
fmt.Println("Restoration complete, restart required")
os.Exit(0)
}
return err
default:
return fmt.Errorf("Unknown command: %s", line)
}
return nil
}
func handleSignals(fn func()) {
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGTERM)
signal.Notify(signals, os.Interrupt)
for {
sig := <-signals
util.Debugf("Received signal: %v", sig)
fn()
}
}