-
Notifications
You must be signed in to change notification settings - Fork 575
/
watch.go
137 lines (109 loc) · 3.16 KB
/
watch.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
package cmd
/*
Copyright 2019 - 2022 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"github.com/nsqio/go-nsq"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
)
type TailHandler struct {
topicName string
totalMessages int
messagesShown int
}
var watchCmd = &cobra.Command{
Use: "watch",
Short: "Print watch information for the PostgreSQL Operator",
Long: `WATCH allows you to watch event information for the postgres-operator. For example:
pgo watch --pgo-event-address=localhost:14150 alltopic
pgo watch alltopic`,
Run: func(cmd *cobra.Command, args []string) {
if Namespace == "" {
Namespace = PGONamespace
}
log.Debug("watch called")
watch(args, Namespace)
},
}
var PGOEventAddress string
func init() {
RootCmd.AddCommand(watchCmd)
watchCmd.Flags().StringVarP(&PGOEventAddress, "pgo-event-address", "a", "localhost:14150", "The address (host:port) where the event stream is.")
}
func watch(args []string, ns string) {
log.Debugf("watch called %v", args)
if len(args) == 0 {
log.Fatal("topic is required")
}
topic := args[0]
var totalMessages = 0
var channel string
rand.Seed(time.Now().UnixNano())
channel = fmt.Sprintf("tail%06d#ephemeral", rand.Int()%999999)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
cfg := nsq.NewConfig()
cfg.MaxInFlight = 200
consumers := []*nsq.Consumer{}
log.Printf("Adding consumer for topic: %s", topic)
consumer, err := nsq.NewConsumer(topic, channel, cfg)
if err != nil {
log.Fatal(err)
}
consumer.AddHandler(&TailHandler{topicName: topic, totalMessages: totalMessages})
addrs := make([]string, 1)
if PGOEventAddress != "" {
addrs[0] = PGOEventAddress
err = consumer.ConnectToNSQDs(addrs)
if err != nil {
log.Fatal(err)
}
}
consumers = append(consumers, consumer)
<-sigChan
for _, consumer := range consumers {
consumer.Stop()
}
for _, consumer := range consumers {
<-consumer.StopChan
}
}
func (th *TailHandler) HandleMessage(m *nsq.Message) error {
th.messagesShown++
_, err := os.Stdout.WriteString(th.topicName)
if err != nil {
log.Fatalf("ERROR: failed to write to os.Stdout - %s", err)
}
_, err = os.Stdout.WriteString(" | ")
if err != nil {
log.Fatalf("ERROR: failed to write to os.Stdout - %s", err)
}
_, err = os.Stdout.Write(m.Body)
if err != nil {
log.Fatalf("ERROR: failed to write to os.Stdout - %s", err)
}
_, err = os.Stdout.WriteString("\n")
if err != nil {
log.Fatalf("ERROR: failed to write to os.Stdout - %s", err)
}
if th.totalMessages > 0 && th.messagesShown >= th.totalMessages {
os.Exit(0)
}
return nil
}