main.go 1.1 KB

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