-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
310 lines (234 loc) · 6.94 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"github.com/function61/eventhorizon/config/configfactory"
"github.com/function61/eventhorizon/pubsub/client"
"github.com/function61/eventhorizon/pubsub/server"
"github.com/function61/eventhorizon/pusher"
"github.com/function61/eventhorizon/pusher/transport"
"github.com/function61/eventhorizon/util/clicommon"
"github.com/function61/eventhorizon/writer"
"github.com/function61/eventhorizon/writer/bootstrap"
wtypes "github.com/function61/eventhorizon/writer/types"
"github.com/function61/eventhorizon/writer/writerclient"
"github.com/function61/eventhorizon/writer/writerhttp"
"io"
"log"
"os"
"strconv"
"strings"
"time"
)
func banner() {
log.Println(" _______ _ _ _______ __ _ _______")
log.Println(" |______ \\ / |______ | \\ | | by function61.com")
log.Println(" |______ \\/ |______ | \\_| | ver. TODO")
log.Println(" _ _ _____ ______ _____ ______ _____ __ _")
log.Println(" |_____| | | |_____/ | ____/ | | | \\ |")
log.Println(" | | |_____| | \\_ __|__ /_____ |_____| | \\_|")
log.Println("")
// hat tip: http://www.network-science.de/ascii/
}
func writer_(args []string) error {
if len(args) != 0 {
return usage("(no args)")
}
banner()
if err := clicommon.CheckForS3AccessKeys(); err != nil {
log.Fatalf("main: %s", err.Error())
}
confCtx, err := configfactory.Build()
if err != nil {
log.Printf("main: failed to get discovery file - trying to bootstrap.")
// unable to fetch config from scalablestore. we'll assume that
// scalablestore has _bootstrap flag which means we can run bootstrap
// automatically. if it doesn't have, we can't do anything and we'll panic.
if err := bootstrap.Run(); err != nil {
log.Fatalf("main: bootstrap failed: %s", err.Error())
}
confCtx = configfactory.BuildMust()
}
// start pub/sub server
pubSubServer := server.New(confCtx)
esServer := writer.New(confCtx)
httpCloser := make(chan bool)
httpCloserDone := make(chan bool)
writerhttp.HttpServe(esServer, httpCloser, httpCloserDone, confCtx)
log.Printf("main: waiting for stop signal")
log.Println(clicommon.WaitForInterrupt())
// stop serving HTTP
httpCloser <- true
<-httpCloserDone
// stop the main writer server
esServer.Close()
// stop pub/sub
pubSubServer.Close()
return nil
}
func streamAppend(args []string) error {
if len(args) != 2 {
return usage("<Stream> <Line>")
}
wclient := writerclient.New(configfactory.BuildMust())
req := &wtypes.AppendToStreamRequest{
Stream: args[0],
Lines: []string{args[1]},
}
_, err := wclient.Append(req)
return err
}
func streamSubscribe(args []string) error {
if len(args) != 2 {
return usage("<Stream> <SubscriptionId>")
}
wclient := writerclient.New(configfactory.BuildMust())
req := &wtypes.SubscribeToStreamRequest{
Stream: args[0],
SubscriptionId: args[1],
}
return wclient.SubscribeToStream(req)
}
func streamCreate(args []string) error {
if len(args) != 1 {
return usage("<Stream>")
}
wclient := writerclient.New(configfactory.BuildMust())
req := &wtypes.CreateStreamRequest{
Name: args[0],
}
_, err := wclient.CreateStream(req)
return err
}
func streamUnsubscribe(args []string) error {
if len(args) != 2 {
return usage("<Stream> <SubscriptionId>")
}
wclient := writerclient.New(configfactory.BuildMust())
req := &wtypes.UnsubscribeFromStreamRequest{
Stream: args[0],
SubscriptionId: args[1],
}
return wclient.UnsubscribeFromStream(req)
}
func pubsubSubscribe(args []string) error {
if len(args) != 1 {
return usage("<Topic>")
}
pubSubClient := client.New(configfactory.BuildMust())
pubSubClient.Subscribe(args[0])
go func() {
for {
msg, more := <-pubSubClient.Notifications
if !more {
break
}
log.Printf("Recv: %v", msg)
}
}()
clicommon.WaitForInterrupt()
pubSubClient.Close()
return nil
}
// Imports events into a stream from a file with one line per event.
func streamAppendFromFile(args []string) error {
if len(args) != 2 {
return usage("<Stream> <FilePath>")
}
wclient := writerclient.New(configfactory.BuildMust())
started := time.Now()
linesRead := readLinebatchesFromFile(args[1], func(batch []string) error {
appendRequest := &wtypes.AppendToStreamRequest{
Stream: args[0],
Lines: batch,
}
log.Printf("Appending %d lines", len(batch))
_, err := wclient.Append(appendRequest)
return err
})
log.Printf("Done. Imported %d lines in %s.", linesRead, time.Since(started))
return nil
}
func pusher_(args []string) error {
if len(args) != 2 {
return usage("<StopOnStdinEof> <Target>")
}
if err := clicommon.CheckForS3AccessKeys(); err != nil {
log.Fatalf("main: %s", err.Error())
}
httpTarget := transport.NewHttpJsonTransport(args[1])
psh := pusher.New(configfactory.BuildMust(), httpTarget)
go psh.Run()
stdinEofOrInterrupt := make(chan bool)
go func() {
if args[0] != "y" {
return
}
// this is for when Pusher is started as a child process of the endpoint
// application. it makes zero sense for the child to exist after the parent
// dies, so our parent gives us STDIN pipe which never gets written into
// but is kept open to detect EOF (parent either exited gracefully or
// uncleanly - doesn't matter because the OS cleans up the FDs).
// http://stackoverflow.com/a/42924532
clicommon.WaitForStdinEof()
log.Printf("pusher: EOF encountered")
stdinEofOrInterrupt <- true
}()
go func() {
log.Println(clicommon.WaitForInterrupt())
stdinEofOrInterrupt <- true
}()
<-stdinEofOrInterrupt
psh.Close()
return nil
}
func streamLiveRead(args []string) error {
if len(args) != 2 {
return usage("<Cursor> <LinesToRead>")
}
maxLinesToRead, atoiErr := strconv.Atoi(args[1])
if atoiErr != nil {
return atoiErr
}
wclient := writerclient.New(configfactory.BuildMust())
req := &wtypes.LiveReadInput{
Cursor: args[0],
MaxLinesToRead: maxLinesToRead,
}
reader, _, err := wclient.LiveRead(req)
if err != nil {
return err
}
if _, err := io.Copy(os.Stdout, reader); err != nil {
return err
}
return nil
}
// just a dispatcher to the subcommands
func main() {
mapping := map[string]func([]string) error{
"stream-create": streamCreate,
"stream-append": streamAppend,
"stream-appendfromfile": streamAppendFromFile,
"stream-subscribe": streamSubscribe,
"stream-unsubscribe": streamUnsubscribe,
"stream-liveread": streamLiveRead,
"pubsub-subscribe": pubsubSubscribe,
"pusher": pusher_,
"reader-read": readerRead,
"writer": writer_,
}
if len(os.Args) < 2 {
banner()
log.Fatalf(
"Usage: %s <subcommand>\n\nSubcommands: \n %s",
os.Args[0],
strings.Join(stringKeyedMapToStringSlice(mapping), "\n "))
}
subcommand := os.Args[1]
subcommandFn, exists := mapping[subcommand]
if !exists {
log.Fatalf("Unknown subcommand: %s", subcommand)
}
if err := subcommandFn(os.Args[2:]); err != nil {
log.Fatalf("%s: %s", subcommand, err.Error())
}
}