main.go 2.6 KB

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