-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
226 lines (191 loc) · 5.75 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
package main
//TODO:work in progress
import (
"flag"
"fmt"
"os"
"runtime"
conf "github.com/hiromaily/golibs/config"
lg "github.com/hiromaily/golibs/log"
"github.com/hiromaily/golibs/workinprogress/aws/sqs"
)
// WARNING
// この機能はunitestから呼び出し戻り値を取得し、利用しているため、むやみにfmtやlogパッケージから
// 出力しないようにお願いします。
//command line
var (
mode = flag.Int("mode", 1, "")
toml = flag.String("toml", "", "toml file path")
n = flag.Int("n", 1, "amount of sqs messages for setting on SQS")
msg = flag.String("msg", "send message dayo", "text data on sqs messages")
loc = flag.Int("loc", 0, "data for including location")
ot = flag.String("ot", "0", "operationType for sqs messages")
ct = flag.String("ct", "0", "operationType for sqs messages")
debug = flag.Int("debug", 1, "")
)
var usage = `Usage: boom [options...] <url>
Options:
-mode 1:create, 2:purge
-toml tomlファイルのパス
-n SQSに作成するデータ数
-msg sqsにセットするtextメッセージ
-loc 0:何も設定しない、1:位置情報をtrueにする、2:位置情報をfalseにする
-ot sqsの属性値である operationTypeを設定する。0の場合、tomlより取得した値をセット
-ct sqsの属性値である contentTypeを設定する。 0の場合、tomlより取得した値をセット
-debug 1:show something logs.
e.g. for how to use
// mode1 is for specific sqs data
$ ./createsqs -loc 1 -debug 1
`
//TODO:otの値によって処理を分岐せねばならない。
func getContentBody(text string) string {
//get config
/*
conf := conf.GetConf()
//connection info
conInfo := conf.CreateConInfo(conf.Request.Mid)
//encode Json data
if *ot != "2" {
bytesMessage, err := conInfo.CreateSendMessage(text, 2)
if err != nil {
panic(err)
}
return string(bytesMessage)
} else {
bytesMessage, err := conInfo.CreateSendMessageForOpType2(text)
if err != nil {
panic(err)
}
return string(bytesMessage)
}
*/
return ""
}
// Set SQS on AWS
func setSQSData(num int, msg string) {
conf := conf.GetConf()
//conf.Aws.Sqs.QueueName -> test_message
//1.オブジェクト作成
sqs.New()
//2.sqsにqueueがあるかチェック
//3.なければ作成
inputParams := sqs.CreateInputParam(conf.Aws.Sqs.QueueName)
sendMsgRes, err := sqs.CreateNewQueue(inputParams)
if err != nil {
panic(err.Error())
}
fmt.Printf("sendMsgRes : %v \n", sendMsgRes)
//4.deadMessage用も同じく、
//5.なければ作成
inputParamsForDead := sqs.CreateInputParam(conf.Aws.Sqs.DeadQueueName)
deadMsgRes, err := sqs.CreateNewQueue(inputParamsForDead)
if err != nil {
panic(err.Error())
}
fmt.Printf("deadMsgRes : %v \n", deadMsgRes)
//6.メッセージを作成
//body := getContentBody("send message dayo")
body := getContentBody(msg)
acid := ""
if num == 1 {
sendInputParams := sqs.CreateSendMessageInput(sendMsgRes.QueueUrl, &body, &acid, *ot, *ct)
//7.メッセージを送信
sqs.SendMessageToQueue(sendInputParams)
} else {
var bulkCount = num / 10
if bulkCount >= 1 {
for i := 0; i < bulkCount; i++ {
sendBulkProcedure(sendMsgRes.QueueUrl, &body, &acid, 10)
}
if num%10 != 0 {
sendBulkProcedure(sendMsgRes.QueueUrl, &body, &acid, num%10)
}
} else {
sendBulkProcedure(sendMsgRes.QueueUrl, &body, &acid, num)
}
}
}
func sendBulkProcedure(url *string, body *string, acid *string, num int) {
//10通まで
sendInputBatchParams := sqs.CreateSendMessageBatchInput(url, body, acid, *ot, *ct, num)
//メッセージを送信
sqs.SendMultipleMessagesToQueue(sendInputBatchParams)
}
func purgeSQSData() {
sqs.New()
conf := conf.GetConf()
inputParams := sqs.CreateInputParam(conf.Aws.Sqs.QueueName)
sendMsgRes, err := sqs.CreateNewQueue(inputParams)
if err != nil {
panic(err.Error())
}
//fmt.Println(*sendMsgRes.QueueUrl)
sqs.PurgeQueue(sendMsgRes.QueueUrl)
}
func getSQSAttributes() {
sqs.New()
conf := conf.GetConf()
inputParams := sqs.CreateInputParam(conf.Aws.Sqs.QueueName)
sendMsgRes, err := sqs.CreateNewQueue(inputParams)
if err != nil {
panic(err.Error())
}
//check attribute
params := sqs.CreateAttributesParams(sendMsgRes.QueueUrl)
resp, err := sqs.GetQueueAttributes(params)
if err != nil {
panic(err.Error())
}
//fmt.Printf("%s", resp)
//fmt.Printf("%v", resp.Attributes)
//fmt.Printf("%s", *resp.Attributes["ApproximateNumberOfMessages"])
//暫定対応
fmt.Printf("%s,%s", *resp.Attributes["ApproximateNumberOfMessages"], *resp.Attributes["ApproximateNumberOfMessagesNotVisible"])
//ApproximateNumberOfMessages: "3",
}
// handling command line
func handleCmdline() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage, runtime.NumCPU()))
}
flag.Parse()
//log.Debug("flag.NArg(): " + strconv.Itoa(flag.NArg()))
lg.Debug("flag.NArg(): ", flag.NArg())
lg.Debug("mode: ", *mode)
lg.Debug("toml: ", *toml)
lg.Debug("n: ", *n)
lg.Debug("msg: ", *msg)
lg.Debug("loc: ", *loc)
lg.Debug("ot: ", *ot)
lg.Debug("ct: ", *ct)
lg.Debug("debug: ", *debug)
}
//init
func init() {
lg.InitializeLog(lg.DebugStatus, lg.NoDateNoFile, "[SQL]", "", "hiromaily")
//handle command line
handleCmdline()
}
func main() {
//Timer Start
//t := utils.TimeInfo{}
//t.StartTimer()
//get toml path
if *toml != "" {
conf.SetTOMLPath(*toml)
}
//-loc オプション -> 0:何も設定しない、1:位置情報をtrueにする、2:位置情報をfalseにする
//get config
//conf := config.GetConf()
//パラメータに応じて、初期値を設定
if *mode == 1 {
//set sqs data
setSQSData(*n, *msg)
} else if *mode == 2 {
getSQSAttributes()
} else if *mode == 3 {
purgeSQSData()
}
//Timer End
//t.StopTimer()
}