main.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "time"
  6. "go-common/app/service/main/spy/conf"
  7. rpc "go-common/app/service/main/spy/rpc/server"
  8. grpc "go-common/app/service/main/spy/server/grpc"
  9. "go-common/app/service/main/spy/server/http"
  10. "go-common/app/service/main/spy/service"
  11. "go-common/library/log"
  12. xrpc "go-common/library/net/rpc"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/os/signal"
  16. "go-common/library/syscall"
  17. )
  18. var (
  19. svr *service.Service
  20. rpcSvr *xrpc.Server
  21. grpcSvr *warden.Server
  22. )
  23. func main() {
  24. flag.Parse()
  25. if err := conf.Init(); err != nil {
  26. log.Error("conf.Init() error(%v)", err)
  27. panic(err)
  28. }
  29. log.Init(conf.Conf.Log)
  30. defer log.Close()
  31. trace.Init(conf.Conf.Tracer)
  32. defer trace.Close()
  33. // service init
  34. svr = service.New(conf.Conf)
  35. http.Init(conf.Conf, svr)
  36. rpcSvr = rpc.New(conf.Conf, svr)
  37. grpcSvr = grpc.New(conf.Conf.GRPC, svr)
  38. log.Info("spy-service start")
  39. // init signal
  40. c := make(chan os.Signal, 1)
  41. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGSTOP)
  42. for {
  43. s := <-c
  44. log.Info("spy-service get a signal %s", s.String())
  45. switch s {
  46. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGINT:
  47. rpcSvr.Close()
  48. time.Sleep(time.Second * 2)
  49. log.Info("spy-service exit")
  50. return
  51. case syscall.SIGHUP:
  52. // TODO reload
  53. default:
  54. return
  55. }
  56. }
  57. }