main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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-alarm/pb"
  13. "smart-alarm/utils"
  14. "syscall"
  15. "time"
  16. "smart-alarm/impl"
  17. "smart-alarm/parser"
  18. "github.com/jaryhe/gopkgs/database"
  19. "github.com/jaryhe/gopkgs/tasker"
  20. "google.golang.org/grpc"
  21. )
  22. var (
  23. configFile = flag.String("config", "conf/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.RedisHandler, parser.MysqlHandler, parser.InfluxdbHandler)
  42. // 执行注册的处理函数
  43. parser.Handle()
  44. }
  45. // 加载数据到内存
  46. func LoadData() {
  47. //utils.InitAlarmContact()
  48. //utils.InitAlarmRule()
  49. utils.InitAlarmStatistics()
  50. }
  51. func run() {
  52. LoadData()
  53. // 开始监听
  54. serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.SmartAlarm.ServiceIp, parser.Conf.Rpc.SmartAlarm.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. go func() {
  63. if err := s.Serve(lis); err != nil {
  64. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  65. }
  66. }()
  67. // 建立rpc客户端
  68. conns := pb.SetupClients()
  69. for _, conn := range conns {
  70. defer conn.Close()
  71. }
  72. // 优雅关闭服务器
  73. sigChan := make(chan os.Signal, 1)
  74. // 捕获信号
  75. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  76. sigValue := <-sigChan
  77. log.Printf("Got a signal:%v", sigValue)
  78. // 让tasker安全退出
  79. tasker.SignalNotify(sigValue)
  80. // 不管什么行为,都等待5秒退出
  81. log.Println("Start to shutdown server...")
  82. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  83. defer cancel()
  84. s.Stop()
  85. log.Println("Shutdown server finished.")
  86. }
  87. func main() {
  88. defer func() {
  89. database.Close()
  90. }()
  91. flag.Parse()
  92. if *version {
  93. showVersion()
  94. os.Exit(1)
  95. }
  96. prepare(*configFile)
  97. //go utils.Free()
  98. run()
  99. return
  100. }