-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
140 lines (116 loc) · 3.27 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
package main
import (
"fmt"
"log"
"strings"
"github.com/gammazero/workerpool"
)
func main() {
log.SetFlags(log.Lshortfile)
conf, err := readConfig("config.yaml")
if err != nil {
log.Fatalln(err)
}
if err := conf.validate(); err != nil {
log.Fatalln(err)
}
switch conf.Action {
case "pause", "unpause", "empty":
conf.action()
case "info", "check":
conf.info()
}
}
var (
ResultMap = map[string][]string{}
topicNotFoundStr = "topicnotfound"
failStr = "fail"
pausedStr = "paused"
unpausedStr = "unpaused"
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
)
func (c *Config) action() {
var body struct {
Action string `json:"action"`
}
body.Action = c.Action
successCount := 0
wp := workerpool.New(c.Worker)
for _, target := range c.Target {
target := target
wp.Submit(func() {
path := fmt.Sprintf("/api/topics/%s", target)
if err := c.nsqAction(path, body); err != nil {
// log.Println(err)
if strings.Contains(err.Error(), "TOPIC_NOT_FOUND") {
ResultMap[topicNotFoundStr] = append(ResultMap[topicNotFoundStr], target)
} else {
ResultMap[failStr] = append(ResultMap[failStr], fmt.Sprintf("%s: %s", target, err.Error()))
}
return
}
// fmt.Printf("%s is %sd\n", target, body.Action)
successCount++
})
}
wp.StopWait()
// Print Result
fmt.Printf("\nTotal Topic\n%sd : %d\n", body.Action, successCount)
fmt.Printf("Not found : %d\n", len(ResultMap[topicNotFoundStr]))
fmt.Printf("Failed : %d\n", len(ResultMap[failStr]))
printFailedResult()
}
func makeColorStr(color, str string) string {
return fmt.Sprintf("%s%s%s", color, str, colorReset)
}
func (c *Config) info() {
wp := workerpool.New(c.Worker)
for _, target := range c.Target {
target := target
wp.Submit(func() {
path := fmt.Sprintf("/api/topics/%s", target)
resp, err := c.nsqInfo(path)
if err != nil {
// log.Println(err)
if strings.Contains(err.Error(), "TOPIC_NOT_FOUND") {
ResultMap[topicNotFoundStr] = append(ResultMap[topicNotFoundStr], target)
} else {
ResultMap[failStr] = append(ResultMap[failStr], fmt.Sprintf("%s: %s", target, err.Error()))
}
return
}
color := colorGreen
if resp.Paused {
color = colorRed
ResultMap[pausedStr] = append(ResultMap[pausedStr], target)
} else {
ResultMap[unpausedStr] = append(ResultMap[unpausedStr], target)
}
fmt.Printf("%s status pause is %s and depth is %d\n", target, makeColorStr(color, fmt.Sprintf("%t", resp.Paused)), resp.Depth)
})
}
wp.StopWait()
// Print Result
fmt.Printf("\nTotal Topic\n")
fmt.Printf("Unpaused : %d\n", len(ResultMap[unpausedStr]))
fmt.Printf("Paused : %d\n", len(ResultMap[pausedStr]))
fmt.Printf("Not found : %d\n", len(ResultMap[topicNotFoundStr]))
fmt.Printf("Failed : %d\n", len(ResultMap[failStr]))
fmt.Printf("\n\n%s:\n", makeColorStr(colorRed, "List Topic paused"))
for _, data := range ResultMap[pausedStr] {
fmt.Println(data)
}
printFailedResult()
}
func printFailedResult() {
fmt.Printf("\n\n%s:\n", makeColorStr(colorRed, "List Topic not found"))
for _, data := range ResultMap[topicNotFoundStr] {
fmt.Println(data)
}
fmt.Printf("\n\n%s:\n", makeColorStr(colorRed, "List Topic failed"))
for _, data := range ResultMap[failStr] {
fmt.Println(data)
}
}