123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package main
- import (
- "context"
- "flag"
- "fmt"
- "log"
- "os"
- "os/signal"
- "property-task/impl/v1/bill"
- "property-task/impl/v1/reminder"
- "property-task/pb"
- "syscall"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/tasker"
- "github.com/robfig/cron"
- "property-task/parser"
- )
- var (
- configFile = flag.String("config", "conf/common.yaml", "config file location")
- version = flag.Bool("version", false, "print the version")
- GitCommit = "library-import"
- Version = "library-import"
- Test = flag.Int("test", 0, "test")
- )
- func showVersion() {
- fmt.Println("Version: ", Version)
- fmt.Println("GitCommit:", GitCommit)
- }
- func TaskInit() {
- cronTask := cron.New()
- // 添加定时生成账单,每天凌晨1点
- _ = cronTask.AddFunc("0 0 1 * * ?", func() {
- bill.GenerateBill()
- })
- // 添加催缴缓存任务,每天凌晨5点
- _ = cronTask.AddFunc("0 0 5 * * ?", func() {
- reminder.GenerateReminder()
- })
- // 添加催缴消息任务, 每天9点
- _ = cronTask.AddFunc("0 30 9 * * ?", func() {
- reminder.SendUrgeMsg()
- })
- cronTask.Start()
- }
- func prepare(filename string) {
- // 加载配置
- err := parser.LoadConfig(filename)
- if err != nil {
- fmt.Printf("get conf failed, err: %+v\n\n", err)
- os.Exit(1)
- }
- // 注册处理函数
- // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
- parser.Register(
- //parser.MysqlHandler,
- parser.LoggerHandler,
- parser.RedisHandler,
- parser.EtcdHandler,
- )
- // 执行注册的处理函数
- parser.Handle()
- }
- func run() {
- conns := pb.SetupClients()
- for _, conn := range conns {
- defer conn.Close()
- }
- switch *Test {
- case 1:
- bill.GenerateBill()
- return
- case 2:
- reminder.GenerateReminder()
- return
- case 3:
- reminder.SendUrgeMsg()
- return
- }
- // 初始化定时任务
- TaskInit()
- // 建立rpc客户端
- // 优雅关闭服务器
- sigChan := make(chan os.Signal, 1)
- // 捕获信号
- signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
- sigValue := <-sigChan
- log.Printf("Got a signal:%v", sigValue)
- // 让tasker安全退出
- tasker.SignalNotify(sigValue)
- // 不管什么行为,都等待5秒退出
- log.Println("Start to shutdown server...")
- _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- log.Println("Shutdown server finished.")
- }
- func main() {
- defer func() {
- database.Close()
- }()
- flag.Parse()
- if *version {
- showVersion()
- os.Exit(1)
- }
- prepare(*configFile)
- run()
- return
- }
|