main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package main
  4. import (
  5. "context"
  6. "flag"
  7. "fmt"
  8. "log"
  9. "net"
  10. "os"
  11. "os/signal"
  12. "syscall"
  13. "time"
  14. "adm-data/consts"
  15. "adm-data/etcd"
  16. "adm-data/impl"
  17. "adm-data/impl/v1/query"
  18. "adm-data/parser"
  19. "git.getensh.com/common/gopkgsv2/cache"
  20. "git.getensh.com/common/gopkgsv2/database"
  21. "git.getensh.com/common/gopkgsv2/tasker"
  22. "gopkg.in/ini.v1"
  23. "google.golang.org/grpc"
  24. )
  25. var (
  26. configFile = flag.String("config", "/etc/adm/common.yaml", "config file location")
  27. appConf = flag.String("local_config", "/etc/adm-data/app.conf", "config file location")
  28. version = flag.Bool("version", false, "print the version")
  29. GitCommit = "library-import"
  30. Version = "library-import"
  31. )
  32. func showVersion() {
  33. fmt.Println("Version: ", Version)
  34. fmt.Println("GitCommit:", GitCommit)
  35. }
  36. func prepare(filename string) {
  37. // 加载配置
  38. err := parser.LoadConfig(filename)
  39. if err != nil {
  40. fmt.Printf("get conf failed, err: %+v\n\n", err)
  41. os.Exit(1)
  42. }
  43. // 注册处理函数
  44. parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  45. // parser.Register(parser.LoggerHandler)
  46. // 执行注册的处理函数
  47. parser.Handle()
  48. }
  49. func run(discoveryType, etcdAddr, serviceIp, servicePort string) {
  50. if discoveryType == "etcd" {
  51. etcd.Init(etcdAddr, serviceIp, servicePort)
  52. }
  53. // 开始监听
  54. serveAddr := serviceIp + ":" + servicePort
  55. log.Printf("Listening and serving TCP on %s\n", serveAddr)
  56. lis, err := net.Listen("tcp", serveAddr)
  57. if err != nil {
  58. os.Exit(1)
  59. }
  60. s := grpc.NewServer()
  61. defer s.GracefulStop()
  62. impl.Register(s)
  63. go func() {
  64. if err := s.Serve(lis); err != nil {
  65. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  66. }
  67. }()
  68. // redis 订阅
  69. cache.Redis().PSubscribe(query.HandleExportMsg, consts.ExportStyleChannel)
  70. // 优雅关闭服务器
  71. sigChan := make(chan os.Signal, 1)
  72. // 捕获信号
  73. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  74. sigValue := <-sigChan
  75. log.Printf("Got a signal:%v", sigValue)
  76. // 让tasker安全退出
  77. tasker.SignalNotify(sigValue)
  78. // 不管什么行为,都等待5秒退出
  79. log.Println("Start to shutdown server...")
  80. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  81. defer cancel()
  82. log.Println("Shutdown server finished.")
  83. }
  84. func main() {
  85. defer func() {
  86. database.Close()
  87. }()
  88. flag.Parse()
  89. if *version {
  90. showVersion()
  91. os.Exit(1)
  92. }
  93. // 读取app.conf内容
  94. cfg, err := ini.Load(*appConf)
  95. if err != nil {
  96. fmt.Printf("get app.conf failed, err:%+v\n", err)
  97. os.Exit(1)
  98. }
  99. discoveryType := cfg.Section("").Key("discoveryType").String()
  100. serviceIp := cfg.Section("").Key("serviceIp").String()
  101. servicePort := cfg.Section("").Key("servicePort").String()
  102. etcdAddr := cfg.Section("").Key("etcdAddr").String()
  103. prepare(*configFile)
  104. // go utils.Free()
  105. run(discoveryType, etcdAddr, serviceIp, servicePort)
  106. return
  107. }