main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. server "go-common/app/service/main/broadcast/server/grpc"
  10. "go-common/app/service/main/broadcast/server/http"
  11. "go-common/app/service/main/broadcast/service"
  12. "go-common/library/conf/env"
  13. "go-common/library/conf/paladin"
  14. ecode "go-common/library/ecode/tip"
  15. "go-common/library/log"
  16. "go-common/library/naming"
  17. "go-common/library/naming/discovery"
  18. "go-common/library/net/ip"
  19. )
  20. const (
  21. ver = "v1.4.4"
  22. )
  23. func main() {
  24. flag.Parse()
  25. if err := paladin.Init(); err != nil {
  26. panic(err)
  27. }
  28. var (
  29. ac struct {
  30. Discovery *discovery.Config
  31. }
  32. )
  33. if err := paladin.Get("application.toml").UnmarshalTOML(&ac); err != nil {
  34. if err != paladin.ErrNotExist {
  35. panic(err)
  36. }
  37. }
  38. log.Init(nil)
  39. defer log.Close()
  40. log.Info("broadcast-service %s start", ver)
  41. // use internal discovery
  42. dis := discovery.New(ac.Discovery)
  43. // new a service
  44. srv := service.New(dis)
  45. ecode.Init(nil)
  46. http.Init(srv)
  47. // grpc server
  48. rpcSrv, rpcPort := server.New(srv)
  49. rpcSrv.Start()
  50. // register discovery
  51. var (
  52. err error
  53. cancel context.CancelFunc
  54. )
  55. if env.IP == "" {
  56. ipAddr := ip.InternalIP()
  57. // broadcast discovery
  58. ins := &naming.Instance{
  59. Zone: env.Zone,
  60. Env: env.DeployEnv,
  61. Hostname: env.Hostname,
  62. AppID: "push.service.broadcast",
  63. Addrs: []string{
  64. "grpc://" + ipAddr + ":" + rpcPort,
  65. },
  66. }
  67. cancel, err = dis.Register(context.Background(), ins)
  68. if err != nil {
  69. panic(err)
  70. }
  71. }
  72. c := make(chan os.Signal, 1)
  73. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  74. for {
  75. s := <-c
  76. log.Info("broadcast-service get a signal %s", s.String())
  77. switch s {
  78. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  79. log.Info("broadcast-service %s exit", ver)
  80. if cancel != nil {
  81. cancel()
  82. }
  83. rpcSrv.Shutdown(context.Background())
  84. time.Sleep(time.Second * 2)
  85. srv.Close()
  86. return
  87. case syscall.SIGHUP:
  88. default:
  89. return
  90. }
  91. }
  92. }