main.go 1.2 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/main/resource/conf"
  10. "go-common/app/service/main/resource/http"
  11. rpc "go-common/app/service/main/resource/rpc/server"
  12. grpc "go-common/app/service/main/resource/server/grpc"
  13. "go-common/app/service/main/resource/service"
  14. "go-common/library/log"
  15. "go-common/library/net/trace"
  16. )
  17. func main() {
  18. flag.Parse()
  19. if err := conf.Init(); err != nil {
  20. log.Error("conf.Init() error(%v)", err)
  21. panic(err)
  22. }
  23. // init log
  24. log.Init(conf.Conf.XLog)
  25. trace.Init(conf.Conf.Tracer)
  26. defer trace.Close()
  27. defer log.Close()
  28. log.Info("resource-service start")
  29. // service init
  30. svr := service.New(conf.Conf)
  31. rpcSvr := rpc.New(conf.Conf, svr)
  32. grpcSvr := grpc.New(nil, svr)
  33. http.Init(conf.Conf, svr)
  34. // init signal
  35. c := make(chan os.Signal, 1)
  36. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  37. for {
  38. s := <-c
  39. log.Info("resource-service get a signal %s", s.String())
  40. switch s {
  41. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  42. rpcSvr.Close()
  43. grpcSvr.Shutdown(context.TODO())
  44. time.Sleep(time.Second * 2)
  45. log.Info("resource-service exit")
  46. return
  47. case syscall.SIGHUP:
  48. // TODO reload
  49. default:
  50. return
  51. }
  52. }
  53. }