main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/openplatform/ticket-item/conf"
  10. rpc "go-common/app/service/openplatform/ticket-item/server/grpc"
  11. "go-common/app/service/openplatform/ticket-item/server/http"
  12. "go-common/app/service/openplatform/ticket-item/service"
  13. "go-common/library/log"
  14. "go-common/library/net/trace"
  15. )
  16. func main() {
  17. flag.Parse()
  18. if err := conf.Init(); err != nil {
  19. panic(err)
  20. }
  21. log.Init(conf.Conf.Log)
  22. defer log.Close()
  23. trace.Init(nil)
  24. defer trace.Close()
  25. log.Info("ticket-item start")
  26. svr := service.New(conf.Conf)
  27. // http Service init
  28. http.Init(conf.Conf, svr)
  29. // grpc service init
  30. gsvr := rpc.New(conf.Conf)
  31. // init pprof conf.Conf.Perf
  32. // signal handler
  33. c := make(chan os.Signal, 1)
  34. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  35. for {
  36. s := <-c
  37. ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
  38. defer cancel()
  39. log.Info("ticket-item get a signal %s", s.String())
  40. switch s {
  41. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGINT:
  42. svr.Close()
  43. gsvr.Shutdown(ctx)
  44. log.Info("ticket-item exit")
  45. time.Sleep(time.Second) // 休眠10s
  46. return
  47. case syscall.SIGHUP:
  48. // TODO reload
  49. default:
  50. return
  51. }
  52. }
  53. }