main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. )
  27. func showVersion() {
  28. fmt.Println("Version: ", Version)
  29. fmt.Println("GitCommit:", GitCommit)
  30. }
  31. func TaskInit() {
  32. reminder.GenerateReminder()
  33. reminder.SendUrgeMsg()
  34. return
  35. cronTask := cron.New()
  36. // 添加定时生成账单,每天凌晨1点
  37. _ = cronTask.AddFunc("0 0 1 * * ?", func() {
  38. bill.GenerateBill()
  39. })
  40. // 添加催缴缓存任务,每天凌晨5点
  41. _ = cronTask.AddFunc("0 0 5 * * ?", func() {
  42. reminder.GenerateReminder()
  43. })
  44. // 添加催缴消息任务, 每天8点
  45. _ = cronTask.AddFunc("0 0 8 * * ?", func() {
  46. reminder.SendUrgeMsg()
  47. })
  48. cronTask.Start()
  49. }
  50. func prepare(filename string) {
  51. // 加载配置
  52. err := parser.LoadConfig(filename)
  53. if err != nil {
  54. fmt.Printf("get conf failed, err: %+v\n\n", err)
  55. os.Exit(1)
  56. }
  57. // 注册处理函数
  58. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  59. parser.Register(
  60. //parser.MysqlHandler,
  61. parser.LoggerHandler,
  62. parser.RedisHandler,
  63. parser.EtcdHandler,
  64. )
  65. // 执行注册的处理函数
  66. parser.Handle()
  67. }
  68. func run() {
  69. conns := pb.SetupClients()
  70. for _, conn := range conns {
  71. defer conn.Close()
  72. }
  73. // 初始化定时任务
  74. TaskInit()
  75. // 建立rpc客户端
  76. // 优雅关闭服务器
  77. sigChan := make(chan os.Signal, 1)
  78. // 捕获信号
  79. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  80. sigValue := <-sigChan
  81. log.Printf("Got a signal:%v", sigValue)
  82. // 让tasker安全退出
  83. tasker.SignalNotify(sigValue)
  84. // 不管什么行为,都等待5秒退出
  85. log.Println("Start to shutdown server...")
  86. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  87. defer cancel()
  88. log.Println("Shutdown server finished.")
  89. }
  90. func main() {
  91. defer func() {
  92. database.Close()
  93. }()
  94. flag.Parse()
  95. if *version {
  96. showVersion()
  97. os.Exit(1)
  98. }
  99. prepare(*configFile)
  100. run()
  101. return
  102. }