main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "adm-task/impl/v1/check"
  6. "adm-task/utils"
  7. "context"
  8. "flag"
  9. "fmt"
  10. "log"
  11. "net"
  12. "os"
  13. "os/signal"
  14. "strings"
  15. "syscall"
  16. "time"
  17. "gopkg.in/ini.v1"
  18. "adm-task/impl"
  19. "adm-task/parser"
  20. "git.getensh.com/common/gopkgsv2/database"
  21. "git.getensh.com/common/gopkgsv2/tasker"
  22. "google.golang.org/grpc"
  23. )
  24. var (
  25. appConfigFile = flag.String("appconfig", "/etc/adm-task/app.conf", "app config file location")
  26. configFile = flag.String("config", "/etc/adm/common.json", "config file location")
  27. version = flag.Bool("version", false, "print the version")
  28. GitCommit = "library-import"
  29. Version = "library-import"
  30. )
  31. func showVersion() {
  32. fmt.Println("Version: ", Version)
  33. fmt.Println("GitCommit:", GitCommit)
  34. }
  35. func prepare(filename string, discoveryType string) {
  36. // 加载配置
  37. if discoveryType == "k8s" {
  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. } else {
  44. // TODO 从etcd读取配置
  45. err := parser.LoadConfig(filename)
  46. if err != nil {
  47. fmt.Printf("get conf failed, err: %+v\n\n", err)
  48. os.Exit(1)
  49. }
  50. }
  51. /*err = parser.LoadConfig("conf/app.yaml")
  52. if err != nil {
  53. fmt.Printf("get conf failed, err: %+v\n\n", err)
  54. os.Exit(1)
  55. }*/
  56. check.SetRunmode(parser.Conf.RunMode)
  57. // 注册处理函数
  58. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  59. parser.Register(parser.LoggerHandler, parser.MysqlHandler)
  60. // 执行注册的处理函数
  61. parser.Handle()
  62. // 加载字段管理,启动定时任务
  63. utils.FieldLoad()
  64. }
  65. func run(etcdAddrs []string, serveAddr string) {
  66. // 开始监听
  67. parser.EctdHandler(etcdAddrs, serveAddr)
  68. //serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.AdmTask.ServiceIp, parser.Conf.Rpc.AdmTask.ServicePort)
  69. log.Printf("Listening and serving TCP on %s\n", serveAddr)
  70. lis, err := net.Listen("tcp", serveAddr)
  71. if err != nil {
  72. os.Exit(1)
  73. }
  74. s := grpc.NewServer()
  75. impl.Register(s)
  76. go func() {
  77. if err := s.Serve(lis); err != nil {
  78. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  79. }
  80. }()
  81. // 优雅关闭服务器
  82. sigChan := make(chan os.Signal, 1)
  83. // 捕获信号
  84. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  85. sigValue := <-sigChan
  86. log.Printf("Got a signal:%v", sigValue)
  87. // 让tasker安全退出
  88. tasker.SignalNotify(sigValue)
  89. // 不管什么行为,都等待5秒退出
  90. log.Println("Start to shutdown server...")
  91. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  92. defer cancel()
  93. s.Stop()
  94. log.Println("Shutdown server finished.")
  95. }
  96. func runPeerToPeer() {
  97. // TODO 支持peer to peer 模式
  98. }
  99. func main() {
  100. defer func() {
  101. database.Close()
  102. }()
  103. flag.Parse()
  104. if *version {
  105. showVersion()
  106. os.Exit(1)
  107. }
  108. cfg, err := ini.Load(*appConfigFile)
  109. if err != nil {
  110. fmt.Printf("Fail to read app config file: %v\n\n", err)
  111. os.Exit(1)
  112. }
  113. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  114. serveAddr := cfg.Section("").Key("serve_addr").String()
  115. discoveryType := cfg.Section("").Key("discovery_type").String()
  116. prepare(*configFile, discoveryType)
  117. // go utils.Free()
  118. if discoveryType == "k8s" {
  119. runPeerToPeer()
  120. } else {
  121. run(etcdAddrs, serveAddr)
  122. }
  123. return
  124. }