main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package main
  4. import (
  5. "context"
  6. "flag"
  7. "fmt"
  8. "log"
  9. "os"
  10. "os/signal"
  11. "property-task/impl/v1/bill"
  12. "property-task/impl/v1/reminder"
  13. "property-task/pb"
  14. "syscall"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/tasker"
  18. "github.com/robfig/cron"
  19. "property-task/parser"
  20. )
  21. var (
  22. configFile = flag.String("config", "conf/common.yaml", "config file location")
  23. version = flag.Bool("version", false, "print the version")
  24. GitCommit = "library-import"
  25. Version = "library-import"
  26. Test = flag.Int("test", 0, "test")
  27. )
  28. func showVersion() {
  29. fmt.Println("Version: ", Version)
  30. fmt.Println("GitCommit:", GitCommit)
  31. }
  32. func TaskInit() {
  33. cronTask := cron.New()
  34. // 添加定时生成账单,每天凌晨1点
  35. _ = cronTask.AddFunc("0 0 1 * * ?", func() {
  36. bill.GenerateBill()
  37. })
  38. // 添加催缴缓存任务,每天凌晨5点
  39. _ = cronTask.AddFunc("0 0 5 * * ?", func() {
  40. reminder.GenerateReminder()
  41. })
  42. // 添加催缴消息任务, 每天9点
  43. _ = cronTask.AddFunc("0 30 9 * * ?", func() {
  44. reminder.SendUrgeMsg()
  45. })
  46. cronTask.Start()
  47. }
  48. func prepare(filename string) {
  49. // 加载配置
  50. err := parser.LoadConfig(filename)
  51. if err != nil {
  52. fmt.Printf("get conf failed, err: %+v\n\n", err)
  53. os.Exit(1)
  54. }
  55. // 注册处理函数
  56. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  57. parser.Register(
  58. //parser.MysqlHandler,
  59. parser.LoggerHandler,
  60. parser.RedisHandler,
  61. parser.EtcdHandler,
  62. )
  63. // 执行注册的处理函数
  64. parser.Handle()
  65. }
  66. func run() {
  67. conns := pb.SetupClients()
  68. for _, conn := range conns {
  69. defer conn.Close()
  70. }
  71. switch *Test {
  72. case 1:
  73. bill.GenerateBill()
  74. return
  75. case 2:
  76. reminder.GenerateReminder()
  77. return
  78. case 3:
  79. reminder.SendUrgeMsg()
  80. return
  81. }
  82. // 初始化定时任务
  83. TaskInit()
  84. // 建立rpc客户端
  85. // 优雅关闭服务器
  86. sigChan := make(chan os.Signal, 1)
  87. // 捕获信号
  88. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  89. sigValue := <-sigChan
  90. log.Printf("Got a signal:%v", sigValue)
  91. // 让tasker安全退出
  92. tasker.SignalNotify(sigValue)
  93. // 不管什么行为,都等待5秒退出
  94. log.Println("Start to shutdown server...")
  95. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  96. defer cancel()
  97. log.Println("Shutdown server finished.")
  98. }
  99. func main() {
  100. defer func() {
  101. database.Close()
  102. }()
  103. flag.Parse()
  104. if *version {
  105. showVersion()
  106. os.Exit(1)
  107. }
  108. prepare(*configFile)
  109. run()
  110. return
  111. }