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/open/conf"
  10. "go-common/app/service/main/open/server/grpc"
  11. "go-common/app/service/main/open/server/http"
  12. "go-common/app/service/main/open/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. log.Error("conf.Init() error(%v)", err)
  20. panic(err)
  21. }
  22. // init log
  23. log.Init(conf.Conf.Log)
  24. trace.Init(conf.Conf.Tracer)
  25. defer trace.Close()
  26. defer func() {
  27. log.Close()
  28. // wait for a while to guarantee that all log messages are written
  29. time.Sleep(10 * time.Millisecond)
  30. }()
  31. //service init
  32. svc := service.New(conf.Conf)
  33. ws := grpc.New(conf.Conf.WardenServer, svc)
  34. http.Init(conf.Conf, svc)
  35. // init signal
  36. c := make(chan os.Signal, 1)
  37. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  38. for {
  39. s := <-c
  40. log.Info("open-service get a signal %s", s.String())
  41. switch s {
  42. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGINT:
  43. ws.Shutdown(context.Background())
  44. svc.Close()
  45. return
  46. case syscall.SIGHUP:
  47. // TODO reload
  48. default:
  49. return
  50. }
  51. }
  52. }