main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package main
  4. import (
  5. "context"
  6. "flag"
  7. "fmt"
  8. "log"
  9. "net"
  10. "os"
  11. "os/signal"
  12. "smart-enterprise-management/task"
  13. "syscall"
  14. "time"
  15. "smart-enterprise-management/impl"
  16. "smart-enterprise-management/parser"
  17. "github.com/jaryhe/gopkgs/database"
  18. "github.com/jaryhe/gopkgs/tasker"
  19. "google.golang.org/grpc"
  20. "smart-enterprise-management/pb"
  21. )
  22. var (
  23. configFile = flag.String("config", "/etc/smart/common.yaml", "config file location")
  24. version = flag.Bool("version", false, "print the version")
  25. GitCommit = "library-import"
  26. Version = "library-import"
  27. )
  28. func showVersion() {
  29. fmt.Println("Version: ", Version)
  30. fmt.Println("GitCommit:", GitCommit)
  31. }
  32. func prepare(filename string) {
  33. // 加载配置
  34. err := parser.LoadConfig(filename)
  35. if err != nil {
  36. fmt.Printf("get conf failed, err: %+v\n\n", err)
  37. os.Exit(1)
  38. }
  39. // 注册处理函数
  40. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  41. parser.Register(parser.LoggerHandler, parser.MysqlHandler, parser.InfluxdbHandler)
  42. // 执行注册的处理函数
  43. parser.Handle()
  44. }
  45. func run() {
  46. // 开始监听
  47. serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.SmartEnterpriseManagement.ServiceIp, parser.Conf.Rpc.SmartEnterpriseManagement.ServicePort)
  48. log.Printf("Listening and serving TCP on %s\n", serveAddr)
  49. lis, err := net.Listen("tcp", serveAddr)
  50. if err != nil {
  51. os.Exit(1)
  52. }
  53. s := grpc.NewServer()
  54. impl.Register(s)
  55. go func() {
  56. if err := s.Serve(lis); err != nil {
  57. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  58. }
  59. }()
  60. conns := pb.SetupClients()
  61. for _, conn := range conns {
  62. defer conn.Close()
  63. }
  64. task.Task()
  65. // 优雅关闭服务器
  66. sigChan := make(chan os.Signal, 1)
  67. // 捕获信号
  68. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  69. sigValue := <-sigChan
  70. log.Printf("Got a signal:%v", sigValue)
  71. // 让tasker安全退出
  72. tasker.SignalNotify(sigValue)
  73. // 不管什么行为,都等待5秒退出
  74. log.Println("Start to shutdown server...")
  75. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  76. defer cancel()
  77. s.Stop()
  78. log.Println("Shutdown server finished.")
  79. }
  80. func main() {
  81. defer func() {
  82. database.Close()
  83. }()
  84. flag.Parse()
  85. if *version {
  86. showVersion()
  87. os.Exit(1)
  88. }
  89. prepare(*configFile)
  90. //go utils.Free()
  91. run()
  92. return
  93. }