main.go 1013 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "os/signal"
  6. "syscall"
  7. "time"
  8. "go-common/app/job/main/click/conf"
  9. "go-common/app/job/main/click/http"
  10. "go-common/app/job/main/click/service"
  11. "go-common/library/log"
  12. )
  13. var (
  14. srv *service.Service
  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. log.Init(nil)
  23. defer log.Close()
  24. log.Info("click-job start")
  25. srv = service.New(conf.Conf)
  26. http.Init(conf.Conf, srv)
  27. signalHandler()
  28. }
  29. func signalHandler() {
  30. var (
  31. err error
  32. ch = make(chan os.Signal, 1)
  33. )
  34. signal.Notify(ch, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  35. for {
  36. si := <-ch
  37. switch si {
  38. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  39. log.Info("get a signal %s, stop the consume process", si.String())
  40. if err = srv.Close(); err != nil {
  41. log.Error("srv close consumer error(%v)", err)
  42. }
  43. time.Sleep(time.Second)
  44. return
  45. case syscall.SIGHUP:
  46. default:
  47. return
  48. }
  49. }
  50. }