github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cmd/dm-master/main.go (about) 1 // Copyright 2019 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package main 15 16 import ( 17 "context" 18 "flag" 19 "fmt" 20 "os" 21 "os/signal" 22 "strings" 23 "syscall" 24 25 "github.com/pingcap/errors" 26 "github.com/pingcap/tiflow/dm/ctl/common" 27 "github.com/pingcap/tiflow/dm/master" 28 "github.com/pingcap/tiflow/dm/pkg/encrypt" 29 "github.com/pingcap/tiflow/dm/pkg/log" 30 "github.com/pingcap/tiflow/dm/pkg/terror" 31 "github.com/pingcap/tiflow/dm/pkg/utils" 32 "github.com/pingcap/tiflow/pkg/version" 33 "go.uber.org/zap" 34 ) 35 36 func main() { 37 // 1. parse config 38 cfg := master.NewConfig() 39 err := cfg.Parse(os.Args[1:]) 40 switch errors.Cause(err) { 41 case nil: 42 case flag.ErrHelp: 43 os.Exit(0) 44 default: 45 common.PrintLinesf("parse cmd flags err: %s", terror.Message(err)) 46 os.Exit(2) 47 } 48 49 // 2. init logger 50 err = log.InitLogger(&log.Config{ 51 File: cfg.LogFile, 52 Level: strings.ToLower(cfg.LogLevel), 53 Format: cfg.LogFormat, 54 }) 55 if err != nil { 56 common.PrintLinesf("init logger error %s", terror.Message(err)) 57 os.Exit(2) 58 } 59 encrypt.InitCipher(cfg.SecretKey) 60 61 utils.LogHTTPProxies(true) 62 63 // 3. print process version information 64 version.LogVersionInfo("dm-master") 65 log.L().Info("", zap.Stringer("dm-master config", cfg)) 66 67 // 4. start the server 68 ctx, cancel := context.WithCancel(context.Background()) 69 server := master.NewServer(cfg) 70 err = server.Start(ctx) 71 if err != nil { 72 log.L().Error("fail to start dm-master", zap.Error(err)) 73 os.Exit(2) 74 } 75 76 // 5. wait for stopping the process 77 sc := make(chan os.Signal, 1) 78 signal.Notify(sc, 79 syscall.SIGHUP, 80 syscall.SIGINT, 81 syscall.SIGTERM, 82 syscall.SIGQUIT) 83 go func() { 84 sig := <-sc 85 log.L().Info("got signal to exit", zap.Stringer("signal", sig)) 86 cancel() 87 }() 88 <-ctx.Done() 89 90 // 6. close the server 91 server.Close() 92 log.L().Info("dm-master exit") 93 94 // 7. flush log 95 if syncErr := log.L().Sync(); syncErr != nil { 96 fmt.Fprintln(os.Stderr, "sync log failed", syncErr) 97 os.Exit(1) 98 } 99 }