main.go 1.1 KB

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