main.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/bbq/topic/internal/server/grpc"
  10. "go-common/app/service/bbq/topic/internal/server/http"
  11. "go-common/app/service/bbq/topic/internal/service"
  12. "go-common/library/conf/paladin"
  13. ecode "go-common/library/ecode/tip"
  14. "go-common/library/log"
  15. )
  16. func main() {
  17. flag.Parse()
  18. if err := paladin.Init(); err != nil {
  19. panic(err)
  20. }
  21. log.Init(nil) // debug flag: log.dir={path}
  22. defer log.Close()
  23. log.Info("topic-service start")
  24. ecode.Init(nil)
  25. svc := service.New()
  26. grpcSrv := grpc.New(svc)
  27. httpSrv := http.New(svc)
  28. c := make(chan os.Signal, 1)
  29. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  30. for {
  31. s := <-c
  32. log.Info("get a signal %s", s.String())
  33. switch s {
  34. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  35. ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
  36. grpcSrv.Shutdown(ctx)
  37. httpSrv.Shutdown(ctx)
  38. log.Info("topic-service exit")
  39. svc.Close()
  40. time.Sleep(time.Second)
  41. cancel()
  42. return
  43. case syscall.SIGHUP:
  44. default:
  45. return
  46. }
  47. }
  48. }