main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/admin/ep/saga/conf"
  10. "go-common/app/admin/ep/saga/http"
  11. "go-common/app/admin/ep/saga/server/grpc"
  12. "go-common/app/admin/ep/saga/service"
  13. "go-common/library/log"
  14. )
  15. const (
  16. _durationForClosingServer = 2 // second
  17. )
  18. func main() {
  19. flag.Parse()
  20. if err := conf.Init(); err != nil {
  21. log.Error("conf.Init() error(%v)", err)
  22. panic(err)
  23. }
  24. log.Init(conf.Conf.Log)
  25. defer log.Close()
  26. log.Info("saga-admin start")
  27. s := service.New()
  28. http.Init(s)
  29. grpcsvr, err := grpc.New(nil, s.Wechat())
  30. if err != nil {
  31. panic(err)
  32. }
  33. c := make(chan os.Signal, 1)
  34. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  35. for {
  36. si := <-c
  37. log.Info("saga-admin get a signal %s", si.String())
  38. switch si {
  39. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  40. grpcsvr.Shutdown(context.Background())
  41. log.Info("saga-admin exit")
  42. s.Close()
  43. time.Sleep(_durationForClosingServer * time.Second)
  44. return
  45. case syscall.SIGHUP:
  46. default:
  47. return
  48. }
  49. }
  50. }