main.go 1.2 KB

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