-
Notifications
You must be signed in to change notification settings - Fork 276
/
main.go
198 lines (164 loc) · 4.31 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
/*
* Copyright 2020-2021 the original author(https://github.com/wj596)
*
* <p>
* 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.
* </p>
*/
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"syscall"
"github.com/juju/errors"
"github.com/siddontang/go-mysql/mysql"
"go-mysql-transfer/global"
"go-mysql-transfer/metrics"
"go-mysql-transfer/service"
"go-mysql-transfer/storage"
"go-mysql-transfer/util/stringutil"
"go-mysql-transfer/web"
)
var (
helpFlag bool
cfgPath string
stockFlag bool
positionFlag bool
statusFlag bool
)
func init() {
flag.BoolVar(&helpFlag, "help", false, "this help")
flag.StringVar(&cfgPath, "config", "app.yml", "application config file")
flag.BoolVar(&stockFlag, "stock", false, "stock data import")
flag.BoolVar(&positionFlag, "position", false, "set dump position")
flag.BoolVar(&statusFlag, "status", false, "display application status")
flag.Usage = usage
}
func main() {
//stockFlag = true
//cfgPath = "D:\\transfer\\app.yml"
//cfgPath = "D:\\transfer\\release_test_luascript.yml"
//cfgPath = "D:\\transfer\\rabbitmq_release_test_lua.yml"
//cfgPath = "D:\\transfer\\rabbitmq_release_test_rule.yml"
// cfgPath = "D:\\transfer\\kafka_release_test_lua.yml"
// cfgPath = "D:\\transfer\\kafka_release_test_rule.yml"
//cfgPath = "D:\\transfer\\rocketmq_release_test_lua.yml"
//cfgPath = "D:\\transfer\\rocketmq_release_test_rule.yml"
//cfgPath = "D:\\transfer\\es7_release_test_lua.yml"
//cfgPath = "D:\\transfer\\es7_release_test_rule.yml"
//cfgPath = "D:\\transfer\\es6_release_test_lua.yml"
//cfgPath = "D:\\transfer\\es6_release_test_rule.yml"
//cfgPath = "D:\\transfer\\redis_release_test_lua.yml"
//cfgPath = "D:\\transfer\\redis_release_test_rule.yml"
//cfgPath = "D:\\transfer\\mongo_release_test_rule.yml"
//cfgPath = "D:\\transfer\\mongo_release_test_lua.yml"
flag.Parse()
if helpFlag {
flag.Usage()
return
}
// 初始化global
err := global.Initialize(cfgPath)
if err != nil {
println(errors.ErrorStack(err))
return
}
if stockFlag {
doStock()
return
}
// 初始化Storage
err = storage.Initialize()
if err != nil {
println(errors.ErrorStack(err))
return
}
if statusFlag {
doStatus()
return
}
if positionFlag {
doPosition()
return
}
err = service.Initialize()
if err != nil {
println(errors.ErrorStack(err))
return
}
if err := metrics.Initialize(); err != nil {
println(errors.ErrorStack(err))
return
}
if err := web.Start(); err != nil {
println(errors.ErrorStack(err))
return
}
service.StartUp() // start application
s := make(chan os.Signal, 1)
signal.Notify(s, os.Kill, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sin := <-s
log.Printf("application stoped,signal: %s \n", sin.String())
web.Close()
service.Close()
storage.Close()
}
func doStock() {
stock := service.NewStockService()
if err := stock.Run(); err != nil {
println(errors.ErrorStack(err))
}
stock.Close()
}
func doStatus() {
ps := storage.NewPositionStorage()
pos, _ := ps.Get()
fmt.Printf("The current dump position is : %s %d \n", pos.Name, pos.Pos)
}
func doPosition() {
others := flag.Args()
if len(others) != 2 {
println("error: please input the binlog's File and Position")
return
}
f := others[0]
p := others[1]
matched, _ := regexp.MatchString(".+\\.\\d+$", f)
if !matched {
println("error: The parameter File must be like: mysql-bin.000001")
return
}
pp, err := stringutil.ToUint32(p)
if nil != err {
println("error: The parameter Position must be number")
return
}
ps := storage.NewPositionStorage()
pos := mysql.Position{
Name: f,
Pos: pp,
}
ps.Save(pos)
fmt.Printf("The current dump position is : %s %d \n", f, pp)
}
func usage() {
fmt.Fprintf(os.Stderr, `version: 1.0.0
Usage: transfer [-c filename] [-s stock]
Options:
`)
flag.PrintDefaults()
}