main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "gopkg.in/ini.v1"
  9. "log"
  10. "net"
  11. "os"
  12. "os/signal"
  13. "property-device/timetask"
  14. "syscall"
  15. "time"
  16. "property-device/impl"
  17. "property-device/parser"
  18. "property-device/pb"
  19. "git.getensh.com/common/gopkgs/database"
  20. "git.getensh.com/common/gopkgs/tasker"
  21. "google.golang.org/grpc"
  22. )
  23. var (
  24. configFile = flag.String("config", "conf/common.yaml", "config file location")
  25. appConf = flag.String("appconf", "conf/app.conf", "app conf file location")
  26. version = flag.Bool("version", false, "print the version")
  27. GitCommit = "library-import"
  28. Version = "library-import"
  29. )
  30. func showVersion() {
  31. fmt.Println("Version: ", Version)
  32. fmt.Println("GitCommit:", GitCommit)
  33. }
  34. func prepare(filename string) {
  35. // 加载配置
  36. err := parser.LoadConfig(filename)
  37. if err != nil {
  38. fmt.Printf("get conf failed, err: %+v\n\n", err)
  39. os.Exit(1)
  40. }
  41. // 注册处理函数
  42. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  43. parser.Register(
  44. parser.MysqlHandler,
  45. parser.LoggerHandler,
  46. parser.RedisHandler,
  47. parser.InfluxdbHandler,
  48. )
  49. // 执行注册的处理函数
  50. parser.Handle()
  51. }
  52. func run() {
  53. // 开始监听
  54. serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.Device.ServiceIp, parser.Conf.Rpc.Device.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. impl.Register(s)
  62. cfg, err := ini.Load(*appConf)
  63. if err != nil {
  64. fmt.Printf("Fail to read file: %v\n\n", err)
  65. os.Exit(1)
  66. }
  67. serviceIp := cfg.Section("").Key("service_ip").String()
  68. if len(parser.Conf.EtcdAddrs) > 0 {
  69. parser.EctdRegister(parser.Conf.EtcdAddrs, parser.Conf.Rpc.Device.ServiceName,
  70. fmt.Sprintf("%s:%v", serviceIp, parser.Conf.Rpc.Device.ServicePort),
  71. parser.Conf.Rpc.Prefix)
  72. }
  73. // 建立rpc客户端
  74. conns := pb.SetupClients()
  75. for _, conn := range conns {
  76. defer conn.Close()
  77. }
  78. go func() {
  79. if err := s.Serve(lis); err != nil {
  80. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  81. }
  82. }()
  83. go timetask.GateStatusTask()
  84. // 优雅关闭服务器
  85. sigChan := make(chan os.Signal, 1)
  86. // 捕获信号
  87. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  88. sigValue := <-sigChan
  89. log.Printf("Got a signal:%v", sigValue)
  90. // 让tasker安全退出
  91. tasker.SignalNotify(sigValue)
  92. // 不管什么行为,都等待5秒退出
  93. log.Println("Start to shutdown server...")
  94. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  95. defer cancel()
  96. s.Stop()
  97. log.Println("Shutdown server finished.")
  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. prepare(*configFile)
  109. run()
  110. return
  111. }