main.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/main/up/conf"
  10. "go-common/app/service/main/up/server/gorpc"
  11. "go-common/app/service/main/up/server/grpc"
  12. "go-common/app/service/main/up/server/http"
  13. "go-common/app/service/main/up/service"
  14. ecode "go-common/library/ecode/tip"
  15. "go-common/library/log"
  16. "go-common/library/net/trace"
  17. "go-common/library/queue/databus/report"
  18. )
  19. func main() {
  20. flag.Parse()
  21. if err := conf.Init(); err != nil {
  22. log.Error("conf.Init() error(%v)", err)
  23. panic(err)
  24. }
  25. // init log
  26. log.Init(conf.Conf.Xlog)
  27. trace.Init(conf.Conf.Tracer)
  28. defer trace.Close()
  29. defer log.Close()
  30. log.SetFormat("[%D %T] [%L] [%S] %M")
  31. log.Info("up-servicestart")
  32. svr := service.New(conf.Conf)
  33. ecode.Init(conf.Conf.Ecode)
  34. // service init
  35. http.Init(conf.Conf, svr)
  36. rpcSvr := gorpc.New(conf.Conf, svr)
  37. grpcSvr := grpc.New(conf.Conf.GRPCServer, svr)
  38. report.InitManager(conf.Conf.ManagerReport)
  39. // init signal
  40. c := make(chan os.Signal, 1)
  41. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  42. for {
  43. s := <-c
  44. log.Info("up-service get a signal %s", s.String())
  45. switch s {
  46. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGINT:
  47. rpcSvr.Close()
  48. grpcSvr.Shutdown(context.TODO())
  49. time.Sleep(time.Second * 2)
  50. svr.Close()
  51. log.Info("up-service exit")
  52. return
  53. case syscall.SIGHUP:
  54. // TODO reload
  55. default:
  56. return
  57. }
  58. }
  59. }