main.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/main/location/conf"
  10. rpc "go-common/app/service/main/location/rpc/server"
  11. "go-common/app/service/main/location/server/grpc"
  12. "go-common/app/service/main/location/server/http"
  13. "go-common/app/service/main/location/service"
  14. "go-common/library/log"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/rpc/warden/resolver/livezk"
  17. "go-common/library/net/trace"
  18. )
  19. const (
  20. discoveryID = "location.service"
  21. )
  22. func main() {
  23. flag.Parse()
  24. if err := conf.Init(); err != nil {
  25. log.Error("conf.Init() error(%v)", err)
  26. panic(err)
  27. }
  28. // init log
  29. log.Init(conf.Conf.Log)
  30. trace.Init(conf.Conf.Tracer)
  31. defer trace.Close()
  32. defer log.Close()
  33. // service init
  34. svr := service.New(conf.Conf)
  35. rpcSvr := rpc.New(conf.Conf, svr)
  36. // warden init
  37. var err error
  38. var cancelzk context.CancelFunc = func() {}
  39. var grpcSvr *warden.Server
  40. if conf.Conf.WardenServer != nil {
  41. grpcSvr = grpc.New(conf.Conf.WardenServer, svr)
  42. if conf.Conf.LiveZK != nil {
  43. if cancelzk, err = livezk.Register(conf.Conf.LiveZK, conf.Conf.WardenServer.Addr, discoveryID); err != nil {
  44. panic(err)
  45. }
  46. }
  47. }
  48. // http init
  49. http.Init(conf.Conf, svr, rpcSvr)
  50. log.Info("location-service start")
  51. // init signal
  52. c := make(chan os.Signal, 1)
  53. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  54. for s := range c {
  55. log.Info("location-service get a signal %s", s.String())
  56. switch s {
  57. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  58. cancelzk()
  59. if grpcSvr != nil {
  60. grpcSvr.Shutdown(context.Background())
  61. }
  62. rpcSvr.Close()
  63. time.Sleep(2 * time.Second)
  64. log.Info("location-service exit")
  65. return
  66. case syscall.SIGHUP:
  67. // TODO reload
  68. default:
  69. return
  70. }
  71. }
  72. }